History

batman-adv: add B.A.T.M.A.N. IV bat_{orig, neigh}_dump implementations

Matthias Schiffer committed 10 years ago

Commit 024f99c1 parent 85cf8c83 files · +372 −0Browse files

Unified · Split

3 files
  1. include/uapi/linux/batman_adv.h +4 −0
  2. net/batman-adv/bat_iv_ogm.c +366 −0
  3. net/batman-adv/netlink.c +2 −0
include/uapi/linux/batman_adv.h+4−0
@@ -85,6 +85,8 @@ enum batadv_tt_client_flags { · 84 unmodified lines
85 * @BATADV_ATTR_TT_FLAGS: Translation table client flags85 * @BATADV_ATTR_TT_FLAGS: Translation table client flags
86 * @BATADV_ATTR_FLAG_BEST: Flags indicating entry is the best86 * @BATADV_ATTR_FLAG_BEST: Flags indicating entry is the best
87 * @BATADV_ATTR_LAST_SEEN_MSECS: Time in milliseconds since last seen87 * @BATADV_ATTR_LAST_SEEN_MSECS: Time in milliseconds since last seen
88 * @BATADV_ATTR_NEIGH_ADDRESS: Neighbour MAC address
89 * @BATADV_ATTR_TQ: TQ to neighbour
88 * @__BATADV_ATTR_AFTER_LAST: internal use90 * @__BATADV_ATTR_AFTER_LAST: internal use
89 * @NUM_BATADV_ATTR: total number of batadv_nl_attrs available91 * @NUM_BATADV_ATTR: total number of batadv_nl_attrs available
90 * @BATADV_ATTR_MAX: highest attribute number currently defined92 * @BATADV_ATTR_MAX: highest attribute number currently defined
@@ -114,6 +116,8 @@ enum batadv_nl_attrs { · 23 unmodified lines
114 BATADV_ATTR_TT_FLAGS,116 BATADV_ATTR_TT_FLAGS,
115 BATADV_ATTR_FLAG_BEST,117 BATADV_ATTR_FLAG_BEST,
116 BATADV_ATTR_LAST_SEEN_MSECS,118 BATADV_ATTR_LAST_SEEN_MSECS,
119 BATADV_ATTR_NEIGH_ADDRESS,
120 BATADV_ATTR_TQ,
117 /* add attributes above here, update the policy in netlink.c */121 /* add attributes above here, update the policy in netlink.c */
118 __BATADV_ATTR_AFTER_LAST,122 __BATADV_ATTR_AFTER_LAST,
119 NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST,123 NUM_BATADV_ATTR = __BATADV_ATTR_AFTER_LAST,
net/batman-adv/bat_iv_ogm.c+366−0
@@ -35,6 +35,7 @@ · 34 unmodified lines
35#include <linux/list.h>35#include <linux/list.h>
36#include <linux/lockdep.h>36#include <linux/lockdep.h>
37#include <linux/netdevice.h>37#include <linux/netdevice.h>
38#include <linux/netlink.h>
38#include <linux/pkt_sched.h>39#include <linux/pkt_sched.h>
39#include <linux/printk.h>40#include <linux/printk.h>
40#include <linux/random.h>41#include <linux/random.h>
@@ -48,6 +49,9 @@ · 7 unmodified lines
48#include <linux/string.h>49#include <linux/string.h>
49#include <linux/types.h>50#include <linux/types.h>
50#include <linux/workqueue.h>51#include <linux/workqueue.h>
52#include <net/genetlink.h>
53#include <net/netlink.h>
54#include <uapi/linux/batman_adv.h>
51 55
52#include "bat_algo.h"56#include "bat_algo.h"
53#include "bitarray.h"57#include "bitarray.h"
@@ -55,6 +59,7 @@ · 1 unmodified line
55#include "hard-interface.h"59#include "hard-interface.h"
56#include "hash.h"60#include "hash.h"
57#include "log.h"61#include "log.h"
62#include "netlink.h"
58#include "network-coding.h"63#include "network-coding.h"
59#include "originator.h"64#include "originator.h"
60#include "packet.h"65#include "packet.h"
@@ -1947,6 +1952,235 @@ next: · 1886 unmodified lines
1947 seq_puts(seq, "No batman nodes in range ...\n");1952 seq_puts(seq, "No batman nodes in range ...\n");
1948}1953}
1949 1954
1955/**
1956 * batadv_iv_ogm_neigh_get_tq_avg - Get the TQ average for a neighbour on a
1957 * given outgoing interface.
1958 * @neigh_node: Neighbour of interest
1959 * @if_outgoing: Outgoing interface of interest
1960 * @tq_avg: Pointer of where to store the TQ average
1961 *
1962 * Return: False if no average TQ available, otherwise true.
1963 */
1964static bool
1965batadv_iv_ogm_neigh_get_tq_avg(struct batadv_neigh_node *neigh_node,
1966 struct batadv_hard_iface *if_outgoing,
1967 u8 *tq_avg)
1968{
1969 struct batadv_neigh_ifinfo *n_ifinfo;
1970
1971 n_ifinfo = batadv_neigh_ifinfo_get(neigh_node, if_outgoing);
1972 if (!n_ifinfo)
1973 return false;
1974
1975 *tq_avg = n_ifinfo->bat_iv.tq_avg;
1976 batadv_neigh_ifinfo_put(n_ifinfo);
1977
1978 return true;
1979}
1980
1981/**
1982 * batadv_iv_ogm_orig_dump_subentry - Dump an originator subentry into a
1983 * message
1984 * @msg: Netlink message to dump into
1985 * @portid: Port making netlink request
1986 * @seq: Sequence number of netlink message
1987 * @bat_priv: The bat priv with all the soft interface information
1988 * @if_outgoing: Limit dump to entries with this outgoing interface
1989 * @orig_node: Originator to dump
1990 * @neigh_node: Single hops neighbour
1991 * @best: Is the best originator
1992 *
1993 * Return: Error code, or 0 on success
1994 */
1995static int
1996batadv_iv_ogm_orig_dump_subentry(struct sk_buff *msg, u32 portid, u32 seq,
1997 struct batadv_priv *bat_priv,
1998 struct batadv_hard_iface *if_outgoing,
1999 struct batadv_orig_node *orig_node,
2000 struct batadv_neigh_node *neigh_node,
2001 bool best)
2002{
2003 void *hdr;
2004 u8 tq_avg;
2005 unsigned int last_seen_msecs;
2006
2007 last_seen_msecs = jiffies_to_msecs(jiffies - orig_node->last_seen);
2008
2009 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node, if_outgoing, &tq_avg))
2010 return 0;
2011
2012 if (if_outgoing != BATADV_IF_DEFAULT &&
2013 if_outgoing != neigh_node->if_incoming)
2014 return 0;
2015
2016 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2017 NLM_F_MULTI, BATADV_CMD_GET_ORIGINATORS);
2018 if (!hdr)
2019 return -ENOBUFS;
2020
2021 if (nla_put(msg, BATADV_ATTR_ORIG_ADDRESS, ETH_ALEN,
2022 orig_node->orig) ||
2023 nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2024 neigh_node->addr) ||
2025 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2026 neigh_node->if_incoming->net_dev->ifindex) ||
2027 nla_put_u8(msg, BATADV_ATTR_TQ, tq_avg) ||
2028 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2029 last_seen_msecs))
2030 goto nla_put_failure;
2031
2032 if (best && nla_put_flag(msg, BATADV_ATTR_FLAG_BEST))
2033 goto nla_put_failure;
2034
2035 genlmsg_end(msg, hdr);
2036 return 0;
2037
2038 nla_put_failure:
2039 genlmsg_cancel(msg, hdr);
2040 return -EMSGSIZE;
2041}
2042
2043/**
2044 * batadv_iv_ogm_orig_dump_entry - Dump an originator entry into a message
2045 * @msg: Netlink message to dump into
2046 * @portid: Port making netlink request
2047 * @seq: Sequence number of netlink message
2048 * @bat_priv: The bat priv with all the soft interface information
2049 * @if_outgoing: Limit dump to entries with this outgoing interface
2050 * @orig_node: Originator to dump
2051 * @sub_s: Number of sub entries to skip
2052 *
2053 * This function assumes the caller holds rcu_read_lock().
2054 *
2055 * Return: Error code, or 0 on success
2056 */
2057static int
2058batadv_iv_ogm_orig_dump_entry(struct sk_buff *msg, u32 portid, u32 seq,
2059 struct batadv_priv *bat_priv,
2060 struct batadv_hard_iface *if_outgoing,
2061 struct batadv_orig_node *orig_node, int *sub_s)
2062{
2063 struct batadv_neigh_node *neigh_node_best;
2064 struct batadv_neigh_node *neigh_node;
2065 int sub = 0;
2066 bool best;
2067 u8 tq_avg_best;
2068
2069 neigh_node_best = batadv_orig_router_get(orig_node, if_outgoing);
2070 if (!neigh_node_best)
2071 goto out;
2072
2073 if (!batadv_iv_ogm_neigh_get_tq_avg(neigh_node_best, if_outgoing,
2074 &tq_avg_best))
2075 goto out;
2076
2077 if (tq_avg_best == 0)
2078 goto out;
2079
2080 hlist_for_each_entry_rcu(neigh_node, &orig_node->neigh_list, list) {
2081 if (sub++ < *sub_s)
2082 continue;
2083
2084 best = (neigh_node == neigh_node_best);
2085
2086 if (batadv_iv_ogm_orig_dump_subentry(msg, portid, seq,
2087 bat_priv, if_outgoing,
2088 orig_node, neigh_node,
2089 best)) {
2090 batadv_neigh_node_put(neigh_node_best);
2091
2092 *sub_s = sub - 1;
2093 return -EMSGSIZE;
2094 }
2095 }
2096
2097 out:
2098 if (neigh_node_best)
2099 batadv_neigh_node_put(neigh_node_best);
2100
2101 *sub_s = 0;
2102 return 0;
2103}
2104
2105/**
2106 * batadv_iv_ogm_orig_dump_bucket - Dump an originator bucket into a
2107 * message
2108 * @msg: Netlink message to dump into
2109 * @portid: Port making netlink request
2110 * @seq: Sequence number of netlink message
2111 * @bat_priv: The bat priv with all the soft interface information
2112 * @if_outgoing: Limit dump to entries with this outgoing interface
2113 * @head: Bucket to be dumped
2114 * @idx_s: Number of entries to be skipped
2115 * @sub: Number of sub entries to be skipped
2116 *
2117 * Return: Error code, or 0 on success
2118 */
2119static int
2120batadv_iv_ogm_orig_dump_bucket(struct sk_buff *msg, u32 portid, u32 seq,
2121 struct batadv_priv *bat_priv,
2122 struct batadv_hard_iface *if_outgoing,
2123 struct hlist_head *head, int *idx_s, int *sub)
2124{
2125 struct batadv_orig_node *orig_node;
2126 int idx = 0;
2127
2128 rcu_read_lock();
2129 hlist_for_each_entry_rcu(orig_node, head, hash_entry) {
2130 if (idx++ < *idx_s)
2131 continue;
2132
2133 if (batadv_iv_ogm_orig_dump_entry(msg, portid, seq, bat_priv,
2134 if_outgoing, orig_node,
2135 sub)) {
2136 rcu_read_unlock();
2137 *idx_s = idx - 1;
2138 return -EMSGSIZE;
2139 }
2140 }
2141 rcu_read_unlock();
2142
2143 *idx_s = 0;
2144 *sub = 0;
2145 return 0;
2146}
2147
2148/**
2149 * batadv_iv_ogm_orig_dump - Dump the originators into a message
2150 * @msg: Netlink message to dump into
2151 * @cb: Control block containing additional options
2152 * @bat_priv: The bat priv with all the soft interface information
2153 * @if_outgoing: Limit dump to entries with this outgoing interface
2154 */
2155static void
2156batadv_iv_ogm_orig_dump(struct sk_buff *msg, struct netlink_callback *cb,
2157 struct batadv_priv *bat_priv,
2158 struct batadv_hard_iface *if_outgoing)
2159{
2160 struct batadv_hashtable *hash = bat_priv->orig_hash;
2161 struct hlist_head *head;
2162 int bucket = cb->args[0];
2163 int idx = cb->args[1];
2164 int sub = cb->args[2];
2165 int portid = NETLINK_CB(cb->skb).portid;
2166
2167 while (bucket < hash->size) {
2168 head = &hash->table[bucket];
2169
2170 if (batadv_iv_ogm_orig_dump_bucket(msg, portid,
2171 cb->nlh->nlmsg_seq,
2172 bat_priv, if_outgoing, head,
2173 &idx, &sub))
2174 break;
2175
2176 bucket++;
2177 }
2178
2179 cb->args[0] = bucket;
2180 cb->args[1] = idx;
2181 cb->args[2] = sub;
2182}
2183
1950/**2184/**
1951 * batadv_iv_hardif_neigh_print - print a single hop neighbour node2185 * batadv_iv_hardif_neigh_print - print a single hop neighbour node
1952 * @seq: neighbour table seq_file struct2186 * @seq: neighbour table seq_file struct
@@ -2043,6 +2277,136 @@ out: · 90 unmodified lines
2043 return ret;2277 return ret;
2044}2278}
2045 2279
2280/**
2281 * batadv_iv_ogm_neigh_dump_neigh - Dump a neighbour into a netlink message
2282 * @msg: Netlink message to dump into
2283 * @portid: Port making netlink request
2284 * @seq: Sequence number of netlink message
2285 * @hardif_neigh: Neighbour to be dumped
2286 *
2287 * Return: Error code, or 0 on success
2288 */
2289static int
2290batadv_iv_ogm_neigh_dump_neigh(struct sk_buff *msg, u32 portid, u32 seq,
2291 struct batadv_hardif_neigh_node *hardif_neigh)
2292{
2293 void *hdr;
2294 unsigned int last_seen_msecs;
2295
2296 last_seen_msecs = jiffies_to_msecs(jiffies - hardif_neigh->last_seen);
2297
2298 hdr = genlmsg_put(msg, portid, seq, &batadv_netlink_family,
2299 NLM_F_MULTI, BATADV_CMD_GET_NEIGHBORS);
2300 if (!hdr)
2301 return -ENOBUFS;
2302
2303 if (nla_put(msg, BATADV_ATTR_NEIGH_ADDRESS, ETH_ALEN,
2304 hardif_neigh->addr) ||
2305 nla_put_u32(msg, BATADV_ATTR_HARD_IFINDEX,
2306 hardif_neigh->if_incoming->net_dev->ifindex) ||
2307 nla_put_u32(msg, BATADV_ATTR_LAST_SEEN_MSECS,
2308 last_seen_msecs))
2309 goto nla_put_failure;
2310
2311 genlmsg_end(msg, hdr);
2312 return 0;
2313
2314 nla_put_failure:
2315 genlmsg_cancel(msg, hdr);
2316 return -EMSGSIZE;
2317}
2318
2319/**
2320 * batadv_iv_ogm_neigh_dump_hardif - Dump the neighbours of a hard interface
2321 * into a message
2322 * @msg: Netlink message to dump into
2323 * @portid: Port making netlink request
2324 * @seq: Sequence number of netlink message
2325 * @bat_priv: The bat priv with all the soft interface information
2326 * @hard_iface: Hard interface to dump the neighbours for
2327 * @idx_s: Number of entries to skip
2328 *
2329 * This function assumes the caller holds rcu_read_lock().
2330 *
2331 * Return: Error code, or 0 on success
2332 */
2333static int
2334batadv_iv_ogm_neigh_dump_hardif(struct sk_buff *msg, u32 portid, u32 seq,
2335 struct batadv_priv *bat_priv,
2336 struct batadv_hard_iface *hard_iface,
2337 int *idx_s)
2338{
2339 struct batadv_hardif_neigh_node *hardif_neigh;
2340 int idx = 0;
2341
2342 hlist_for_each_entry_rcu(hardif_neigh,
2343 &hard_iface->neigh_list, list) {
2344 if (idx++ < *idx_s)
2345 continue;
2346
2347 if (batadv_iv_ogm_neigh_dump_neigh(msg, portid, seq,
2348 hardif_neigh)) {
2349 *idx_s = idx - 1;
2350 return -EMSGSIZE;
2351 }
2352 }
2353
2354 *idx_s = 0;
2355 return 0;
2356}
2357
2358/**
2359 * batadv_iv_ogm_neigh_dump - Dump the neighbours into a message
2360 * @msg: Netlink message to dump into
2361 * @cb: Control block containing additional options
2362 * @bat_priv: The bat priv with all the soft interface information
2363 * @single_hardif: Limit dump to this hard interfaace
2364 */
2365static void
2366batadv_iv_ogm_neigh_dump(struct sk_buff *msg, struct netlink_callback *cb,
2367 struct batadv_priv *bat_priv,
2368 struct batadv_hard_iface *single_hardif)
2369{
2370 struct batadv_hard_iface *hard_iface;
2371 int i_hardif = 0;
2372 int i_hardif_s = cb->args[0];
2373 int idx = cb->args[1];
2374 int portid = NETLINK_CB(cb->skb).portid;
2375
2376 rcu_read_lock();
2377 if (single_hardif) {
2378 if (i_hardif_s == 0) {
2379 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2380 cb->nlh->nlmsg_seq,
2381 bat_priv,
2382 single_hardif,
2383 &idx) == 0)
2384 i_hardif++;
2385 }
2386 } else {
2387 list_for_each_entry_rcu(hard_iface, &batadv_hardif_list,
2388 list) {
2389 if (hard_iface->soft_iface != bat_priv->soft_iface)
2390 continue;
2391
2392 if (i_hardif++ < i_hardif_s)
2393 continue;
2394
2395 if (batadv_iv_ogm_neigh_dump_hardif(msg, portid,
2396 cb->nlh->nlmsg_seq,
2397 bat_priv,
2398 hard_iface, &idx)) {
2399 i_hardif--;
2400 break;
2401 }
2402 }
2403 }
2404 rcu_read_unlock();
2405
2406 cb->args[0] = i_hardif;
2407 cb->args[1] = idx;
2408}
2409
2046/**2410/**
2047 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors2411 * batadv_iv_ogm_neigh_cmp - compare the metrics of two neighbors
2048 * @neigh1: the first neighbor object of the comparison2412 * @neigh1: the first neighbor object of the comparison
@@ -2330,9 +2694,11 @@ static struct batadv_algo_ops batadv_batman_iv __read_mostly = { · 281 unmodified lines
2330 .cmp = batadv_iv_ogm_neigh_cmp,2694 .cmp = batadv_iv_ogm_neigh_cmp,
2331 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,2695 .is_similar_or_better = batadv_iv_ogm_neigh_is_sob,
2332 .print = batadv_iv_neigh_print,2696 .print = batadv_iv_neigh_print,
2697 .dump = batadv_iv_ogm_neigh_dump,
2333 },2698 },
2334 .orig = {2699 .orig = {
2335 .print = batadv_iv_ogm_orig_print,2700 .print = batadv_iv_ogm_orig_print,
2701 .dump = batadv_iv_ogm_orig_dump,
2336 .free = batadv_iv_ogm_orig_free,2702 .free = batadv_iv_ogm_orig_free,
2337 .add_if = batadv_iv_ogm_orig_add_if,2703 .add_if = batadv_iv_ogm_orig_add_if,
2338 .del_if = batadv_iv_ogm_orig_del_if,2704 .del_if = batadv_iv_ogm_orig_del_if,
net/batman-adv/netlink.c+2−0
@@ -84,6 +84,8 @@ static struct nla_policy batadv_netlink_policy[NUM_BATADV_ATTR] = { · 83 unmodified lines
84 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },84 [BATADV_ATTR_TT_FLAGS] = { .type = NLA_U32 },
85 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },85 [BATADV_ATTR_FLAG_BEST] = { .type = NLA_FLAG },
86 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },86 [BATADV_ATTR_LAST_SEEN_MSECS] = { .type = NLA_U32 },
87 [BATADV_ATTR_NEIGH_ADDRESS] = { .len = ETH_ALEN },
88 [BATADV_ATTR_TQ] = { .type = NLA_U8 },
87};89};
88 90
89/**91/**

Keyboard shortcuts

General

Show keyboard shortcuts
?
Focus the current search
/

Issues and pull requests

Open filters
f

Issues, pull requests and Inbox

Select next item
j↓
Select previous item
k↑
Open selected item
Enter
Dismiss filters or clear selection
Esc

Repository code

Go to file
t