19 #include <netlink-private/netlink.h>
20 #include <netlink/netlink.h>
21 #include <netlink/utils.h>
22 #include <netlink/route/rtnl.h>
23 #include <netlink/route/neightbl.h>
24 #include <netlink/route/link.h>
27 #define NEIGHTBL_ATTR_FAMILY 0x001
28 #define NEIGHTBL_ATTR_STATS 0x002
29 #define NEIGHTBL_ATTR_NAME 0x004
30 #define NEIGHTBL_ATTR_THRESH1 0x008
31 #define NEIGHTBL_ATTR_THRESH2 0x010
32 #define NEIGHTBL_ATTR_THRESH3 0x020
33 #define NEIGHTBL_ATTR_CONFIG 0x040
34 #define NEIGHTBL_ATTR_PARMS 0x080
35 #define NEIGHTBL_ATTR_GC_INTERVAL 0x100
37 #define NEIGHTBLPARM_ATTR_IFINDEX 0x0001
38 #define NEIGHTBLPARM_ATTR_REFCNT 0x0002
39 #define NEIGHTBLPARM_ATTR_QUEUE_LEN 0x0004
40 #define NEIGHTBLPARM_ATTR_APP_PROBES 0x0008
41 #define NEIGHTBLPARM_ATTR_UCAST_PROBES 0x0010
42 #define NEIGHTBLPARM_ATTR_MCAST_PROBES 0x0020
43 #define NEIGHTBLPARM_ATTR_PROXY_QLEN 0x0040
44 #define NEIGHTBLPARM_ATTR_REACHABLE_TIME 0x0080
45 #define NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME 0x0100
46 #define NEIGHTBLPARM_ATTR_RETRANS_TIME 0x0200
47 #define NEIGHTBLPARM_ATTR_GC_STALETIME 0x0400
48 #define NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME 0x0800
49 #define NEIGHTBLPARM_ATTR_ANYCAST_DELAY 0x1000
50 #define NEIGHTBLPARM_ATTR_PROXY_DELAY 0x2000
51 #define NEIGHTBLPARM_ATTR_LOCKTIME 0x4000
53 static struct nl_cache_ops rtnl_neightbl_ops;
54 static struct nl_object_ops neightbl_obj_ops;
57 static int neightbl_compare(
struct nl_object *_a,
struct nl_object *_b,
58 uint32_t attrs,
int flags)
60 struct rtnl_neightbl *a = (
struct rtnl_neightbl *) _a;
61 struct rtnl_neightbl *b = (
struct rtnl_neightbl *) _b;
64 #define NT_DIFF(ATTR, EXPR) ATTR_DIFF(attrs, NEIGHTBL_ATTR_##ATTR, a, b, EXPR)
66 diff |= NT_DIFF(FAMILY, a->nt_family != b->nt_family);
67 diff |= NT_DIFF(NAME, strcmp(a->nt_name, b->nt_name));
68 diff |= NT_DIFF(THRESH1, a->nt_gc_thresh1 != b->nt_gc_thresh1);
69 diff |= NT_DIFF(THRESH2, a->nt_gc_thresh2 != b->nt_gc_thresh2);
70 diff |= NT_DIFF(THRESH3, a->nt_gc_thresh3 != b->nt_gc_thresh3);
71 diff |= NT_DIFF(GC_INTERVAL, a->nt_gc_interval != b->nt_gc_interval);
75 if (!(a->ce_mask & NEIGHTBL_ATTR_PARMS) &&
76 !(b->ce_mask & NEIGHTBL_ATTR_PARMS))
83 #define REQ(F) (fp->ntp_mask & NEIGHTBLPARM_ATTR_##F)
84 #define AVAIL(F) (op->ntp_mask & NEIGHTBLPARM_ATTR_##F)
85 #define _C(F, N) (REQ(F) && (!AVAIL(F) || (op->N != fp->N)))
86 if (_C(IFINDEX, ntp_ifindex) ||
87 _C(QUEUE_LEN, ntp_queue_len) ||
88 _C(APP_PROBES, ntp_app_probes) ||
89 _C(UCAST_PROBES, ntp_ucast_probes) ||
90 _C(MCAST_PROBES, ntp_mcast_probes) ||
91 _C(PROXY_QLEN, ntp_proxy_qlen) ||
92 _C(LOCKTIME, ntp_locktime) ||
93 _C(RETRANS_TIME, ntp_retrans_time) ||
94 _C(BASE_REACHABLE_TIME, ntp_base_reachable_time) ||
95 _C(GC_STALETIME, ntp_gc_stale_time) ||
96 _C(DELAY_PROBE_TIME, ntp_probe_delay) ||
97 _C(ANYCAST_DELAY, ntp_anycast_delay) ||
98 _C(PROXY_DELAY, ntp_proxy_delay))
109 static struct nla_policy neightbl_policy[NDTA_MAX+1] = {
111 .maxlen = NTBLNAMSIZ },
112 [NDTA_THRESH1] = { .type =
NLA_U32 },
113 [NDTA_THRESH2] = { .type =
NLA_U32 },
114 [NDTA_THRESH3] = { .type =
NLA_U32 },
115 [NDTA_GC_INTERVAL] = { .type =
NLA_U32 },
116 [NDTA_CONFIG] = { .minlen =
sizeof(
struct ndt_config) },
117 [NDTA_STATS] = { .minlen =
sizeof(
struct ndt_stats) },
121 static int neightbl_msg_parser(
struct nl_cache_ops *ops,
122 struct sockaddr_nl *who,
struct nlmsghdr *n,
123 struct nl_parser_param *pp)
125 struct rtnl_neightbl *ntbl;
126 struct nlattr *tb[NDTA_MAX + 1];
127 struct rtgenmsg *rtmsg;
130 ntbl = rtnl_neightbl_alloc();
136 ntbl->ce_msgtype = n->nlmsg_type;
139 err =
nlmsg_parse(n,
sizeof(*rtmsg), tb, NDTA_MAX, neightbl_policy);
143 ntbl->nt_family = rtmsg->rtgen_family;
145 if (tb[NDTA_NAME] == NULL) {
146 err = -NLE_MISSING_ATTR;
150 nla_strlcpy(ntbl->nt_name, tb[NDTA_NAME], NTBLNAMSIZ);
151 ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
153 if (tb[NDTA_THRESH1]) {
154 ntbl->nt_gc_thresh1 =
nla_get_u32(tb[NDTA_THRESH1]);
155 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
158 if (tb[NDTA_THRESH2]) {
159 ntbl->nt_gc_thresh2 =
nla_get_u32(tb[NDTA_THRESH2]);
160 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
163 if (tb[NDTA_THRESH3]) {
164 ntbl->nt_gc_thresh3 =
nla_get_u32(tb[NDTA_THRESH3]);
165 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
168 if (tb[NDTA_GC_INTERVAL]) {
169 ntbl->nt_gc_interval =
nla_get_u32(tb[NDTA_GC_INTERVAL]);
170 ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
173 if (tb[NDTA_CONFIG]) {
175 sizeof(ntbl->nt_config));
176 ntbl->ce_mask |= NEIGHTBL_ATTR_CONFIG;
179 if (tb[NDTA_STATS]) {
181 sizeof(ntbl->nt_stats));
182 ntbl->ce_mask |= NEIGHTBL_ATTR_STATS;
185 if (tb[NDTA_PARMS]) {
186 struct nlattr *tbp[NDTPA_MAX + 1];
187 struct rtnl_neightbl_parms *p = &ntbl->nt_parms;
193 #define COPY_ENTRY(name, var) \
194 if (tbp[NDTPA_ ##name]) { \
195 p->ntp_ ##var = nla_get_u32(tbp[NDTPA_ ##name]); \
196 p->ntp_mask |= NEIGHTBLPARM_ATTR_ ##name; \
199 COPY_ENTRY(IFINDEX, ifindex);
200 COPY_ENTRY(REFCNT, refcnt);
201 COPY_ENTRY(QUEUE_LEN, queue_len);
202 COPY_ENTRY(APP_PROBES, app_probes);
203 COPY_ENTRY(UCAST_PROBES, ucast_probes);
204 COPY_ENTRY(MCAST_PROBES, mcast_probes);
205 COPY_ENTRY(PROXY_QLEN, proxy_qlen);
206 COPY_ENTRY(PROXY_DELAY, proxy_delay);
207 COPY_ENTRY(ANYCAST_DELAY, anycast_delay);
208 COPY_ENTRY(LOCKTIME, locktime);
209 COPY_ENTRY(REACHABLE_TIME, reachable_time);
210 COPY_ENTRY(BASE_REACHABLE_TIME, base_reachable_time);
211 COPY_ENTRY(RETRANS_TIME, retrans_time);
212 COPY_ENTRY(GC_STALETIME, gc_stale_time);
213 COPY_ENTRY(DELAY_PROBE_TIME, probe_delay);
216 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
219 err = pp->pp_cb((
struct nl_object *) ntbl, pp);
221 rtnl_neightbl_put(ntbl);
225 static int neightbl_request_update(
struct nl_cache *c,
struct nl_sock *h)
231 static void neightbl_dump_line(
struct nl_object *arg,
struct nl_dump_params *p)
233 struct rtnl_neightbl *ntbl = (
struct rtnl_neightbl *) arg;
235 nl_dump_line(p,
"%s", ntbl->nt_name);
237 if (ntbl->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX) {
238 struct nl_cache *link_cache;
246 ntbl->nt_parms.ntp_ifindex,
248 nl_cache_put(link_cache);
250 nl_dump(p,
"<%u> ", ntbl->nt_parms.ntp_ifindex);
254 if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG)
255 nl_dump(p,
"entries %u ", ntbl->nt_config.ndtc_entries);
257 if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
258 char rt[32], rt2[32];
259 struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
261 nl_dump(p,
"reachable-time %s retransmit-time %s",
262 nl_msec2str(pa->ntp_reachable_time, rt,
sizeof(rt)),
263 nl_msec2str(pa->ntp_retrans_time, rt2,
sizeof(rt2)));
269 static void neightbl_dump_details(
struct nl_object *arg,
struct nl_dump_params *p)
271 char x[32], y[32], z[32];
272 struct rtnl_neightbl *ntbl = (
struct rtnl_neightbl *) arg;
274 neightbl_dump_line(arg, p);
276 if (ntbl->ce_mask & NEIGHTBL_ATTR_CONFIG) {
277 nl_dump_line(p,
" key-len %u entry-size %u last-flush %s\n",
278 ntbl->nt_config.ndtc_key_len,
279 ntbl->nt_config.ndtc_entry_size,
283 nl_dump_line(p,
" gc threshold %u/%u/%u interval %s " \
284 "chain-position %u\n",
285 ntbl->nt_gc_thresh1, ntbl->nt_gc_thresh2,
288 ntbl->nt_config.ndtc_hash_chain_gc);
290 nl_dump_line(p,
" hash-rand 0x%08X/0x%08X last-rand %s\n",
291 ntbl->nt_config.ndtc_hash_rnd,
292 ntbl->nt_config.ndtc_hash_mask,
297 if (ntbl->ce_mask & NEIGHTBL_ATTR_PARMS) {
298 struct rtnl_neightbl_parms *pa = &ntbl->nt_parms;
300 nl_dump_line(p,
" refcnt %u pending-queue-limit %u " \
301 "proxy-delayed-queue-limit %u\n",
306 nl_dump_line(p,
" num-userspace-probes %u num-unicast-probes " \
307 "%u num-multicast-probes %u\n",
309 pa->ntp_ucast_probes,
310 pa->ntp_mcast_probes);
312 nl_dump_line(p,
" min-age %s base-reachable-time %s " \
313 "stale-check-interval %s\n",
319 nl_dump_line(p,
" initial-probe-delay %s answer-delay %s " \
320 "proxy-answer-delay %s\n",
327 static void neightbl_dump_stats(
struct nl_object *arg,
struct nl_dump_params *p)
329 struct rtnl_neightbl *ntbl = (
struct rtnl_neightbl *) arg;
331 neightbl_dump_details(arg, p);
333 if (!(ntbl->ce_mask & NEIGHTBL_ATTR_STATS))
336 nl_dump_line(p,
" " \
337 " lookups %" PRIu64 \
340 " allocations %" PRIu64 \
341 " destroys %" PRIu64 \
343 ntbl->nt_stats.ndts_lookups,
344 ntbl->nt_stats.ndts_hits,
345 ntbl->nt_stats.ndts_res_failed,
346 ntbl->nt_stats.ndts_allocs,
347 ntbl->nt_stats.ndts_destroys);
349 nl_dump_line(p,
" " \
350 " hash-grows %" PRIu64 \
351 " forced-gc-runs %" PRIu64 \
352 " periodic-gc-runs %" PRIu64 \
354 ntbl->nt_stats.ndts_hash_grows,
355 ntbl->nt_stats.ndts_forced_gc_runs,
356 ntbl->nt_stats.ndts_periodic_gc_runs);
358 nl_dump_line(p,
" " \
359 " rcv-unicast-probes %" PRIu64 \
360 " rcv-multicast-probes %" PRIu64 \
362 ntbl->nt_stats.ndts_rcv_probes_ucast,
363 ntbl->nt_stats.ndts_rcv_probes_mcast);
371 struct rtnl_neightbl *rtnl_neightbl_alloc(
void)
376 void rtnl_neightbl_put(
struct rtnl_neightbl *neightbl)
418 const char *name,
int ifindex)
420 struct rtnl_neightbl *nt;
422 if (cache->c_ops != &rtnl_neightbl_ops)
425 nl_list_for_each_entry(nt, &cache->c_items, ce_list) {
426 if (!strcasecmp(nt->nt_name, name) &&
427 ((!ifindex && !nt->nt_parms.ntp_ifindex) ||
428 (ifindex && ifindex == nt->nt_parms.ntp_ifindex))) {
461 struct rtnl_neightbl *tmpl,
462 struct nl_msg **result)
464 struct nl_msg *m, *parms = NULL;
465 struct ndtmsg ndt = {
466 .ndtm_family = old->nt_family,
473 if (
nlmsg_append(m, &ndt,
sizeof(ndt), NLMSG_ALIGNTO) < 0)
474 goto nla_put_failure;
478 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH1)
481 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
484 if (tmpl->ce_mask & NEIGHTBL_ATTR_THRESH2)
487 if (tmpl->ce_mask & NEIGHTBL_ATTR_GC_INTERVAL)
489 tmpl->nt_gc_interval);
491 if (tmpl->ce_mask & NEIGHTBL_ATTR_PARMS) {
492 struct rtnl_neightbl_parms *p = &tmpl->nt_parms;
496 goto nla_put_failure;
498 if (old->nt_parms.ntp_mask & NEIGHTBLPARM_ATTR_IFINDEX)
500 old->nt_parms.ntp_ifindex);
503 if (p->ntp_mask & NEIGHTBLPARM_ATTR_QUEUE_LEN)
504 NLA_PUT_U32(parms, NDTPA_QUEUE_LEN, p->ntp_queue_len);
506 if (p->ntp_mask & NEIGHTBLPARM_ATTR_APP_PROBES)
507 NLA_PUT_U32(parms, NDTPA_APP_PROBES, p->ntp_app_probes);
509 if (p->ntp_mask & NEIGHTBLPARM_ATTR_UCAST_PROBES)
511 p->ntp_ucast_probes);
513 if (p->ntp_mask & NEIGHTBLPARM_ATTR_MCAST_PROBES)
515 p->ntp_mcast_probes);
517 if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_QLEN)
521 if (p->ntp_mask & NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME)
523 p->ntp_base_reachable_time);
525 if (p->ntp_mask & NEIGHTBLPARM_ATTR_RETRANS_TIME)
527 p->ntp_retrans_time);
529 if (p->ntp_mask & NEIGHTBLPARM_ATTR_GC_STALETIME)
531 p->ntp_gc_stale_time);
533 if (p->ntp_mask & NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME)
537 if (p->ntp_mask & NEIGHTBLPARM_ATTR_ANYCAST_DELAY)
539 p->ntp_anycast_delay);
541 if (p->ntp_mask & NEIGHTBLPARM_ATTR_PROXY_DELAY)
545 if (p->ntp_mask & NEIGHTBLPARM_ATTR_LOCKTIME)
546 NLA_PUT_U64(parms, NDTPA_LOCKTIME, p->ntp_locktime);
549 goto nla_put_failure;
578 struct rtnl_neightbl *tmpl)
591 return wait_for_ack(sk);
601 void rtnl_neightbl_set_family(
struct rtnl_neightbl *ntbl,
int family)
603 ntbl->nt_family = family;
604 ntbl->ce_mask |= NEIGHTBL_ATTR_FAMILY;
607 void rtnl_neightbl_set_gc_interval(
struct rtnl_neightbl *ntbl, uint64_t ms)
609 ntbl->nt_gc_interval = ms;
610 ntbl->ce_mask |= NEIGHTBL_ATTR_GC_INTERVAL;
613 void rtnl_neightbl_set_gc_tresh1(
struct rtnl_neightbl *ntbl,
int thresh)
615 ntbl->nt_gc_thresh1 = thresh;
616 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH1;
619 void rtnl_neightbl_set_gc_tresh2(
struct rtnl_neightbl *ntbl,
int thresh)
621 ntbl->nt_gc_thresh2 = thresh;
622 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH2;
625 void rtnl_neightbl_set_gc_tresh3(
struct rtnl_neightbl *ntbl,
int thresh)
627 ntbl->nt_gc_thresh3 = thresh;
628 ntbl->ce_mask |= NEIGHTBL_ATTR_THRESH3;
631 void rtnl_neightbl_set_name(
struct rtnl_neightbl *ntbl,
const char *name)
633 strncpy(ntbl->nt_name, name,
sizeof(ntbl->nt_name) - 1);
634 ntbl->ce_mask |= NEIGHTBL_ATTR_NAME;
637 void rtnl_neightbl_set_dev(
struct rtnl_neightbl *ntbl,
int ifindex)
639 ntbl->nt_parms.ntp_ifindex = ifindex;
640 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_IFINDEX;
641 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
651 ntbl->nt_parms.ntp_queue_len = len;
652 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_QUEUE_LEN;
653 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
663 ntbl->nt_parms.ntp_proxy_qlen = len;
664 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_QLEN;
665 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
675 ntbl->nt_parms.ntp_app_probes = probes;
676 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_APP_PROBES;
677 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
687 ntbl->nt_parms.ntp_ucast_probes = probes;
688 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_UCAST_PROBES;
689 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
699 ntbl->nt_parms.ntp_mcast_probes = probes;
700 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_MCAST_PROBES;
701 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
712 ntbl->nt_parms.ntp_base_reachable_time = ms;
713 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_BASE_REACHABLE_TIME;
714 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
724 ntbl->nt_parms.ntp_retrans_time = ms;
725 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_RETRANS_TIME;
726 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
736 ntbl->nt_parms.ntp_gc_stale_time = ms;
737 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_GC_STALETIME;
738 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
748 ntbl->nt_parms.ntp_probe_delay = ms;
749 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_DELAY_PROBE_TIME;
750 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
760 ntbl->nt_parms.ntp_anycast_delay = ms;
761 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_ANYCAST_DELAY;
762 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
772 ntbl->nt_parms.ntp_proxy_delay = ms;
773 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_PROXY_DELAY;
774 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
784 ntbl->nt_parms.ntp_locktime = ms;
785 ntbl->nt_parms.ntp_mask |= NEIGHTBLPARM_ATTR_LOCKTIME;
786 ntbl->ce_mask |= NEIGHTBL_ATTR_PARMS;
791 static struct nl_object_ops neightbl_obj_ops = {
792 .oo_name =
"route/neightbl",
793 .oo_size =
sizeof(
struct rtnl_neightbl),
799 .oo_compare = neightbl_compare,
802 static struct nl_cache_ops rtnl_neightbl_ops = {
803 .co_name =
"route/neightbl",
804 .co_hdrsize =
sizeof(
struct rtgenmsg),
806 { RTM_NEWNEIGHTBL, NL_ACT_NEW,
"new" },
807 { RTM_SETNEIGHTBL, NL_ACT_SET,
"set" },
808 { RTM_GETNEIGHTBL, NL_ACT_GET,
"get" },
809 END_OF_MSGTYPES_LIST,
811 .co_protocol = NETLINK_ROUTE,
812 .co_request_update = neightbl_request_update,
813 .co_msg_parser = neightbl_msg_parser,
814 .co_obj_ops = &neightbl_obj_ops,
817 static void __init neightbl_init(
void)
822 static void __exit neightbl_exit(
void)