]>
Commit | Line | Data |
---|---|---|
143969f2 SH |
1 | /* |
2 | * Copyright (C)2006 USAGI/WIDE Project | |
ae665a52 | 3 | * |
143969f2 SH |
4 | * This program is free software; you can redistribute it and/or modify |
5 | * it under the terms of the GNU General Public License as published by | |
6 | * the Free Software Foundation; either version 2 of the License, or | |
7 | * (at your option) any later version. | |
ae665a52 | 8 | * |
143969f2 SH |
9 | * This program is distributed in the hope that it will be useful, |
10 | * but WITHOUT ANY WARRANTY; without even the implied warranty of | |
11 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the | |
12 | * GNU General Public License for more details. | |
ae665a52 | 13 | * |
143969f2 | 14 | * You should have received a copy of the GNU General Public License |
4d98ab00 | 15 | * along with this program; if not, see <http://www.gnu.org/licenses>. |
143969f2 SH |
16 | */ |
17 | /* | |
18 | * based on ipneigh.c | |
19 | */ | |
20 | /* | |
21 | * Authors: | |
22 | * Masahide NAKAMURA @USAGI | |
23 | */ | |
24 | ||
25 | #include <stdio.h> | |
26 | #include <stdlib.h> | |
27 | #include <string.h> | |
28 | #include <sys/time.h> | |
5bd9dd49 | 29 | #include <sys/socket.h> |
143969f2 SH |
30 | #include <time.h> |
31 | ||
32 | #include "utils.h" | |
33 | #include "ip_common.h" | |
3c1e087b | 34 | #include "json_print.h" |
143969f2 SH |
35 | |
36 | static struct | |
37 | { | |
38 | int family; | |
56f5daac | 39 | int index; |
143969f2 | 40 | #define NONE_DEV (-1) |
45c2ec9e | 41 | const char *name; |
143969f2 SH |
42 | } filter; |
43 | ||
44 | static void usage(void) __attribute__((noreturn)); | |
45 | ||
46 | static void usage(void) | |
47 | { | |
48 | fprintf(stderr, | |
49 | "Usage: ip ntable change name NAME [ dev DEV ]\n" | |
50 | " [ thresh1 VAL ] [ thresh2 VAL ] [ thresh3 VAL ] [ gc_int MSEC ]\n" | |
51 | " [ PARMS ]\n" | |
52 | "Usage: ip ntable show [ dev DEV ] [ name NAME ]\n" | |
53 | ||
54 | "PARMS := [ base_reachable MSEC ] [ retrans MSEC ] [ gc_stale MSEC ]\n" | |
55 | " [ delay_probe MSEC ] [ queue LEN ]\n" | |
f1fdcfe6 | 56 | " [ app_probes VAL ] [ ucast_probes VAL ] [ mcast_probes VAL ]\n" |
143969f2 SH |
57 | " [ anycast_delay MSEC ] [ proxy_delay MSEC ] [ proxy_queue LEN ]\n" |
58 | " [ locktime MSEC ]\n" | |
59 | ); | |
60 | ||
61 | exit(-1); | |
62 | } | |
63 | ||
64 | static int ipntable_modify(int cmd, int flags, int argc, char **argv) | |
65 | { | |
66 | struct { | |
4806867a | 67 | struct nlmsghdr n; |
143969f2 | 68 | struct ndtmsg ndtm; |
56f5daac | 69 | char buf[1024]; |
d17b136f PS |
70 | } req = { |
71 | .n.nlmsg_len = NLMSG_LENGTH(sizeof(struct ndtmsg)), | |
72 | .n.nlmsg_flags = NLM_F_REQUEST | flags, | |
73 | .n.nlmsg_type = cmd, | |
74 | .ndtm.ndtm_family = preferred_family, | |
75 | }; | |
143969f2 SH |
76 | char *namep = NULL; |
77 | char *threshsp = NULL; | |
78 | char *gc_intp = NULL; | |
d17b136f | 79 | char parms_buf[1024] = {}; |
143969f2 SH |
80 | struct rtattr *parms_rta = (struct rtattr *)parms_buf; |
81 | int parms_change = 0; | |
82 | ||
143969f2 SH |
83 | parms_rta->rta_type = NDTA_PARMS; |
84 | parms_rta->rta_len = RTA_LENGTH(0); | |
85 | ||
86 | while (argc > 0) { | |
87 | if (strcmp(*argv, "name") == 0) { | |
88 | int len; | |
89 | ||
90 | NEXT_ARG(); | |
91 | if (namep) | |
92 | duparg("NAME", *argv); | |
93 | ||
94 | namep = *argv; | |
95 | len = strlen(namep) + 1; | |
96 | addattr_l(&req.n, sizeof(req), NDTA_NAME, namep, len); | |
97 | } else if (strcmp(*argv, "thresh1") == 0) { | |
98 | __u32 thresh1; | |
99 | ||
100 | NEXT_ARG(); | |
101 | threshsp = *argv; | |
102 | ||
103 | if (get_u32(&thresh1, *argv, 0)) | |
104 | invarg("\"thresh1\" value is invalid", *argv); | |
105 | ||
106 | addattr32(&req.n, sizeof(req), NDTA_THRESH1, thresh1); | |
107 | } else if (strcmp(*argv, "thresh2") == 0) { | |
108 | __u32 thresh2; | |
109 | ||
110 | NEXT_ARG(); | |
111 | threshsp = *argv; | |
112 | ||
113 | if (get_u32(&thresh2, *argv, 0)) | |
114 | invarg("\"thresh2\" value is invalid", *argv); | |
115 | ||
116 | addattr32(&req.n, sizeof(req), NDTA_THRESH2, thresh2); | |
117 | } else if (strcmp(*argv, "thresh3") == 0) { | |
118 | __u32 thresh3; | |
119 | ||
120 | NEXT_ARG(); | |
121 | threshsp = *argv; | |
122 | ||
123 | if (get_u32(&thresh3, *argv, 0)) | |
124 | invarg("\"thresh3\" value is invalid", *argv); | |
125 | ||
126 | addattr32(&req.n, sizeof(req), NDTA_THRESH3, thresh3); | |
127 | } else if (strcmp(*argv, "gc_int") == 0) { | |
128 | __u64 gc_int; | |
129 | ||
130 | NEXT_ARG(); | |
131 | gc_intp = *argv; | |
132 | ||
133 | if (get_u64(&gc_int, *argv, 0)) | |
134 | invarg("\"gc_int\" value is invalid", *argv); | |
135 | ||
136 | addattr_l(&req.n, sizeof(req), NDTA_GC_INTERVAL, | |
137 | &gc_int, sizeof(gc_int)); | |
138 | } else if (strcmp(*argv, "dev") == 0) { | |
139 | __u32 ifindex; | |
140 | ||
141 | NEXT_ARG(); | |
142 | ifindex = ll_name_to_index(*argv); | |
fe99adbc SP |
143 | if (!ifindex) |
144 | return nodev(*argv); | |
143969f2 SH |
145 | |
146 | rta_addattr32(parms_rta, sizeof(parms_buf), | |
147 | NDTPA_IFINDEX, ifindex); | |
148 | } else if (strcmp(*argv, "base_reachable") == 0) { | |
149 | __u64 breachable; | |
150 | ||
151 | NEXT_ARG(); | |
152 | ||
153 | if (get_u64(&breachable, *argv, 0)) | |
154 | invarg("\"base_reachable\" value is invalid", *argv); | |
155 | ||
156 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
157 | NDTPA_BASE_REACHABLE_TIME, | |
158 | &breachable, sizeof(breachable)); | |
159 | parms_change = 1; | |
160 | } else if (strcmp(*argv, "retrans") == 0) { | |
161 | __u64 retrans; | |
162 | ||
163 | NEXT_ARG(); | |
164 | ||
165 | if (get_u64(&retrans, *argv, 0)) | |
166 | invarg("\"retrans\" value is invalid", *argv); | |
167 | ||
168 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
169 | NDTPA_RETRANS_TIME, | |
170 | &retrans, sizeof(retrans)); | |
171 | parms_change = 1; | |
172 | } else if (strcmp(*argv, "gc_stale") == 0) { | |
173 | __u64 gc_stale; | |
174 | ||
175 | NEXT_ARG(); | |
176 | ||
177 | if (get_u64(&gc_stale, *argv, 0)) | |
178 | invarg("\"gc_stale\" value is invalid", *argv); | |
179 | ||
180 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
181 | NDTPA_GC_STALETIME, | |
182 | &gc_stale, sizeof(gc_stale)); | |
183 | parms_change = 1; | |
184 | } else if (strcmp(*argv, "delay_probe") == 0) { | |
185 | __u64 delay_probe; | |
186 | ||
187 | NEXT_ARG(); | |
188 | ||
189 | if (get_u64(&delay_probe, *argv, 0)) | |
190 | invarg("\"delay_probe\" value is invalid", *argv); | |
191 | ||
192 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
193 | NDTPA_DELAY_PROBE_TIME, | |
194 | &delay_probe, sizeof(delay_probe)); | |
195 | parms_change = 1; | |
196 | } else if (strcmp(*argv, "queue") == 0) { | |
197 | __u32 queue; | |
198 | ||
199 | NEXT_ARG(); | |
200 | ||
201 | if (get_u32(&queue, *argv, 0)) | |
202 | invarg("\"queue\" value is invalid", *argv); | |
203 | ||
143969f2 SH |
204 | rta_addattr32(parms_rta, sizeof(parms_buf), |
205 | NDTPA_QUEUE_LEN, queue); | |
206 | parms_change = 1; | |
207 | } else if (strcmp(*argv, "app_probes") == 0) { | |
208 | __u32 aprobe; | |
209 | ||
210 | NEXT_ARG(); | |
211 | ||
212 | if (get_u32(&aprobe, *argv, 0)) | |
213 | invarg("\"app_probes\" value is invalid", *argv); | |
214 | ||
215 | rta_addattr32(parms_rta, sizeof(parms_buf), | |
216 | NDTPA_APP_PROBES, aprobe); | |
217 | parms_change = 1; | |
218 | } else if (strcmp(*argv, "ucast_probes") == 0) { | |
219 | __u32 uprobe; | |
220 | ||
221 | NEXT_ARG(); | |
222 | ||
223 | if (get_u32(&uprobe, *argv, 0)) | |
224 | invarg("\"ucast_probes\" value is invalid", *argv); | |
225 | ||
226 | rta_addattr32(parms_rta, sizeof(parms_buf), | |
227 | NDTPA_UCAST_PROBES, uprobe); | |
228 | parms_change = 1; | |
229 | } else if (strcmp(*argv, "mcast_probes") == 0) { | |
230 | __u32 mprobe; | |
231 | ||
232 | NEXT_ARG(); | |
233 | ||
234 | if (get_u32(&mprobe, *argv, 0)) | |
235 | invarg("\"mcast_probes\" value is invalid", *argv); | |
236 | ||
237 | rta_addattr32(parms_rta, sizeof(parms_buf), | |
238 | NDTPA_MCAST_PROBES, mprobe); | |
239 | parms_change = 1; | |
240 | } else if (strcmp(*argv, "anycast_delay") == 0) { | |
241 | __u64 anycast_delay; | |
242 | ||
243 | NEXT_ARG(); | |
244 | ||
245 | if (get_u64(&anycast_delay, *argv, 0)) | |
246 | invarg("\"anycast_delay\" value is invalid", *argv); | |
247 | ||
248 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
249 | NDTPA_ANYCAST_DELAY, | |
250 | &anycast_delay, sizeof(anycast_delay)); | |
251 | parms_change = 1; | |
252 | } else if (strcmp(*argv, "proxy_delay") == 0) { | |
253 | __u64 proxy_delay; | |
254 | ||
255 | NEXT_ARG(); | |
256 | ||
257 | if (get_u64(&proxy_delay, *argv, 0)) | |
258 | invarg("\"proxy_delay\" value is invalid", *argv); | |
259 | ||
260 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
261 | NDTPA_PROXY_DELAY, | |
262 | &proxy_delay, sizeof(proxy_delay)); | |
263 | parms_change = 1; | |
264 | } else if (strcmp(*argv, "proxy_queue") == 0) { | |
265 | __u32 pqueue; | |
266 | ||
267 | NEXT_ARG(); | |
268 | ||
269 | if (get_u32(&pqueue, *argv, 0)) | |
270 | invarg("\"proxy_queue\" value is invalid", *argv); | |
271 | ||
272 | rta_addattr32(parms_rta, sizeof(parms_buf), | |
273 | NDTPA_PROXY_QLEN, pqueue); | |
274 | parms_change = 1; | |
275 | } else if (strcmp(*argv, "locktime") == 0) { | |
276 | __u64 locktime; | |
277 | ||
278 | NEXT_ARG(); | |
279 | ||
280 | if (get_u64(&locktime, *argv, 0)) | |
281 | invarg("\"locktime\" value is invalid", *argv); | |
282 | ||
283 | rta_addattr_l(parms_rta, sizeof(parms_buf), | |
284 | NDTPA_LOCKTIME, | |
285 | &locktime, sizeof(locktime)); | |
286 | parms_change = 1; | |
287 | } else { | |
288 | invarg("unknown", *argv); | |
289 | } | |
290 | ||
291 | argc--; argv++; | |
292 | } | |
293 | ||
294 | if (!namep) | |
295 | missarg("NAME"); | |
296 | if (!threshsp && !gc_intp && !parms_change) { | |
d259f030 | 297 | fprintf(stderr, "Not enough information: changeable attributes required.\n"); |
143969f2 SH |
298 | exit(-1); |
299 | } | |
300 | ||
301 | if (parms_rta->rta_len > RTA_LENGTH(0)) { | |
302 | addattr_l(&req.n, sizeof(req), NDTA_PARMS, RTA_DATA(parms_rta), | |
303 | RTA_PAYLOAD(parms_rta)); | |
304 | } | |
305 | ||
86bf43c7 | 306 | if (rtnl_talk(&rth, &req.n, NULL) < 0) |
143969f2 SH |
307 | exit(2); |
308 | ||
309 | return 0; | |
310 | } | |
311 | ||
312 | static const char *ntable_strtime_delta(__u32 msec) | |
313 | { | |
314 | static char str[32]; | |
d17b136f | 315 | struct timeval now = {}; |
143969f2 SH |
316 | time_t t; |
317 | struct tm *tp; | |
318 | ||
319 | if (msec == 0) | |
320 | goto error; | |
321 | ||
143969f2 SH |
322 | if (gettimeofday(&now, NULL) < 0) { |
323 | perror("gettimeofday"); | |
324 | goto error; | |
325 | } | |
326 | ||
327 | t = now.tv_sec - (msec / 1000); | |
328 | tp = localtime(&t); | |
329 | if (!tp) | |
330 | goto error; | |
331 | ||
332 | strftime(str, sizeof(str), "%Y-%m-%d %T", tp); | |
333 | ||
334 | return str; | |
335 | error: | |
336 | strcpy(str, "(error)"); | |
337 | return str; | |
338 | } | |
339 | ||
3c1e087b | 340 | static void print_ndtconfig(const struct ndt_config *ndtc) |
143969f2 | 341 | { |
143969f2 | 342 | |
3c1e087b SH |
343 | print_uint(PRINT_ANY, "key_length", |
344 | " config key_len %u ", ndtc->ndtc_key_len); | |
345 | print_uint(PRINT_ANY, "entry_size", | |
346 | "entry_size %u ", ndtc->ndtc_entry_size); | |
347 | print_uint(PRINT_ANY, "entries", "entries %u ", ndtc->ndtc_entries); | |
143969f2 | 348 | |
b85076cd | 349 | print_nl(); |
143969f2 | 350 | |
3c1e087b SH |
351 | print_string(PRINT_ANY, "last_flush", |
352 | " last_flush %s ", | |
353 | ntable_strtime_delta(ndtc->ndtc_last_flush)); | |
354 | print_string(PRINT_ANY, "last_rand", | |
355 | "last_rand %s ", | |
356 | ntable_strtime_delta(ndtc->ndtc_last_rand)); | |
143969f2 | 357 | |
b85076cd | 358 | print_nl(); |
143969f2 | 359 | |
3c1e087b SH |
360 | print_uint(PRINT_ANY, "hash_rnd", |
361 | " hash_rnd %u ", ndtc->ndtc_hash_rnd); | |
362 | print_0xhex(PRINT_ANY, "hash_mask", | |
363 | "hash_mask %08x ", ndtc->ndtc_hash_mask); | |
143969f2 | 364 | |
3c1e087b SH |
365 | print_uint(PRINT_ANY, "hash_chain_gc", |
366 | "hash_chain_gc %u ", ndtc->ndtc_hash_chain_gc); | |
367 | print_uint(PRINT_ANY, "proxy_qlen", | |
368 | "proxy_qlen %u ", ndtc->ndtc_proxy_qlen); | |
143969f2 | 369 | |
b85076cd | 370 | print_nl(); |
3c1e087b SH |
371 | } |
372 | ||
373 | static void print_ndtparams(struct rtattr *tpb[]) | |
374 | { | |
375 | ||
376 | if (tpb[NDTPA_IFINDEX]) { | |
377 | __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]); | |
378 | ||
379 | print_string(PRINT_FP, NULL, " dev ", NULL); | |
380 | print_color_string(PRINT_ANY, COLOR_IFNAME, | |
381 | "dev", "%s ", ll_index_to_name(ifindex)); | |
b85076cd | 382 | print_nl(); |
143969f2 SH |
383 | } |
384 | ||
3c1e087b SH |
385 | print_string(PRINT_FP, NULL, " ", NULL); |
386 | if (tpb[NDTPA_REFCNT]) { | |
387 | __u32 refcnt = rta_getattr_u32(tpb[NDTPA_REFCNT]); | |
143969f2 | 388 | |
3c1e087b SH |
389 | print_uint(PRINT_ANY, "refcnt", "refcnt %u ", refcnt); |
390 | } | |
56f5daac | 391 | |
3c1e087b SH |
392 | if (tpb[NDTPA_REACHABLE_TIME]) { |
393 | __u64 reachable = rta_getattr_u64(tpb[NDTPA_REACHABLE_TIME]); | |
394 | ||
4db2ff0d | 395 | print_u64(PRINT_ANY, "reachable", |
3c1e087b | 396 | "reachable %llu ", reachable); |
143969f2 SH |
397 | } |
398 | ||
3c1e087b SH |
399 | if (tpb[NDTPA_BASE_REACHABLE_TIME]) { |
400 | __u64 breachable | |
401 | = rta_getattr_u64(tpb[NDTPA_BASE_REACHABLE_TIME]); | |
143969f2 | 402 | |
4db2ff0d | 403 | print_u64(PRINT_ANY, "base_reachable", |
3c1e087b SH |
404 | "base_reachable %llu ", breachable); |
405 | } | |
143969f2 | 406 | |
3c1e087b SH |
407 | if (tpb[NDTPA_RETRANS_TIME]) { |
408 | __u64 retrans = rta_getattr_u64(tpb[NDTPA_RETRANS_TIME]); | |
56f5daac | 409 | |
4db2ff0d | 410 | print_u64(PRINT_ANY, "retrans", "retrans %llu ", retrans); |
143969f2 | 411 | } |
56f5daac | 412 | |
3c1e087b SH |
413 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
414 | ||
415 | if (tpb[NDTPA_GC_STALETIME]) { | |
416 | __u64 gc_stale = rta_getattr_u64(tpb[NDTPA_GC_STALETIME]); | |
417 | ||
4db2ff0d | 418 | print_u64(PRINT_ANY, "gc_stale", "gc_stale %llu ", gc_stale); |
143969f2 | 419 | } |
56f5daac | 420 | |
3c1e087b SH |
421 | if (tpb[NDTPA_DELAY_PROBE_TIME]) { |
422 | __u64 delay_probe | |
423 | = rta_getattr_u64(tpb[NDTPA_DELAY_PROBE_TIME]); | |
424 | ||
4db2ff0d | 425 | print_u64(PRINT_ANY, "delay_probe", |
3c1e087b | 426 | "delay_probe %llu ", delay_probe); |
143969f2 | 427 | } |
56f5daac | 428 | |
3c1e087b SH |
429 | if (tpb[NDTPA_QUEUE_LEN]) { |
430 | __u32 queue = rta_getattr_u32(tpb[NDTPA_QUEUE_LEN]); | |
431 | ||
432 | print_uint(PRINT_ANY, "queue", "queue %u ", queue); | |
143969f2 SH |
433 | } |
434 | ||
3c1e087b | 435 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
143969f2 | 436 | |
3c1e087b SH |
437 | if (tpb[NDTPA_APP_PROBES]) { |
438 | __u32 aprobe = rta_getattr_u32(tpb[NDTPA_APP_PROBES]); | |
143969f2 | 439 | |
3c1e087b SH |
440 | print_uint(PRINT_ANY, "app_probes", "app_probes %u ", aprobe); |
441 | } | |
143969f2 | 442 | |
3c1e087b SH |
443 | if (tpb[NDTPA_UCAST_PROBES]) { |
444 | __u32 uprobe = rta_getattr_u32(tpb[NDTPA_UCAST_PROBES]); | |
143969f2 | 445 | |
3c1e087b SH |
446 | print_uint(PRINT_ANY, "ucast_probes", |
447 | "ucast_probes %u ", uprobe); | |
448 | } | |
143969f2 | 449 | |
3c1e087b SH |
450 | if (tpb[NDTPA_MCAST_PROBES]) { |
451 | __u32 mprobe = rta_getattr_u32(tpb[NDTPA_MCAST_PROBES]); | |
143969f2 | 452 | |
3c1e087b SH |
453 | print_uint(PRINT_ANY, "mcast_probes", |
454 | "mcast_probes %u ", mprobe); | |
455 | } | |
143969f2 | 456 | |
3c1e087b | 457 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
143969f2 | 458 | |
3c1e087b SH |
459 | if (tpb[NDTPA_ANYCAST_DELAY]) { |
460 | __u64 anycast_delay = rta_getattr_u64(tpb[NDTPA_ANYCAST_DELAY]); | |
143969f2 | 461 | |
4db2ff0d | 462 | print_u64(PRINT_ANY, "anycast_delay", |
3c1e087b | 463 | "anycast_delay %llu ", anycast_delay); |
143969f2 SH |
464 | } |
465 | ||
3c1e087b SH |
466 | if (tpb[NDTPA_PROXY_DELAY]) { |
467 | __u64 proxy_delay = rta_getattr_u64(tpb[NDTPA_PROXY_DELAY]); | |
143969f2 | 468 | |
4db2ff0d | 469 | print_u64(PRINT_ANY, "proxy_delay", |
3c1e087b SH |
470 | "proxy_delay %llu ", proxy_delay); |
471 | } | |
143969f2 | 472 | |
3c1e087b SH |
473 | if (tpb[NDTPA_PROXY_QLEN]) { |
474 | __u32 pqueue = rta_getattr_u32(tpb[NDTPA_PROXY_QLEN]); | |
143969f2 | 475 | |
3c1e087b SH |
476 | print_uint(PRINT_ANY, "proxy_queue", "proxy_queue %u ", pqueue); |
477 | } | |
56f5daac | 478 | |
3c1e087b SH |
479 | if (tpb[NDTPA_LOCKTIME]) { |
480 | __u64 locktime = rta_getattr_u64(tpb[NDTPA_LOCKTIME]); | |
56f5daac | 481 | |
4db2ff0d | 482 | print_u64(PRINT_ANY, "locktime", "locktime %llu ", locktime); |
3c1e087b | 483 | } |
56f5daac | 484 | |
b85076cd | 485 | print_nl(); |
3c1e087b | 486 | } |
56f5daac | 487 | |
3c1e087b SH |
488 | static void print_ndtstats(const struct ndt_stats *ndts) |
489 | { | |
143969f2 | 490 | |
3c1e087b | 491 | print_string(PRINT_FP, NULL, " stats ", NULL); |
143969f2 | 492 | |
4db2ff0d THJ |
493 | print_u64(PRINT_ANY, "allocs", "allocs %llu ", ndts->ndts_allocs); |
494 | print_u64(PRINT_ANY, "destroys", "destroys %llu ", | |
3c1e087b | 495 | ndts->ndts_destroys); |
4db2ff0d | 496 | print_u64(PRINT_ANY, "hash_grows", "hash_grows %llu ", |
3c1e087b | 497 | ndts->ndts_hash_grows); |
143969f2 | 498 | |
3c1e087b | 499 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
56f5daac | 500 | |
4db2ff0d | 501 | print_u64(PRINT_ANY, "res_failed", "res_failed %llu ", |
3c1e087b | 502 | ndts->ndts_res_failed); |
4db2ff0d THJ |
503 | print_u64(PRINT_ANY, "lookups", "lookups %llu ", ndts->ndts_lookups); |
504 | print_u64(PRINT_ANY, "hits", "hits %llu ", ndts->ndts_hits); | |
56f5daac | 505 | |
3c1e087b | 506 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
56f5daac | 507 | |
4db2ff0d | 508 | print_u64(PRINT_ANY, "rcv_probes_mcast", "rcv_probes_mcast %llu ", |
3c1e087b | 509 | ndts->ndts_rcv_probes_mcast); |
4db2ff0d | 510 | print_u64(PRINT_ANY, "rcv_probes_ucast", "rcv_probes_ucast %llu ", |
3c1e087b | 511 | ndts->ndts_rcv_probes_ucast); |
143969f2 | 512 | |
3c1e087b | 513 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
143969f2 | 514 | |
4db2ff0d | 515 | print_u64(PRINT_ANY, "periodic_gc_runs", "periodic_gc_runs %llu ", |
3c1e087b | 516 | ndts->ndts_periodic_gc_runs); |
4db2ff0d | 517 | print_u64(PRINT_ANY, "forced_gc_runs", "forced_gc_runs %llu ", |
3c1e087b | 518 | ndts->ndts_forced_gc_runs); |
143969f2 | 519 | |
b85076cd | 520 | print_nl(); |
3c1e087b | 521 | } |
56f5daac | 522 | |
cd554f2c | 523 | static int print_ntable(struct nlmsghdr *n, void *arg) |
3c1e087b SH |
524 | { |
525 | FILE *fp = (FILE *)arg; | |
526 | struct ndtmsg *ndtm = NLMSG_DATA(n); | |
527 | int len = n->nlmsg_len; | |
528 | struct rtattr *tb[NDTA_MAX+1]; | |
529 | struct rtattr *tpb[NDTPA_MAX+1]; | |
530 | int ret; | |
56f5daac | 531 | |
3c1e087b SH |
532 | if (n->nlmsg_type != RTM_NEWNEIGHTBL) { |
533 | fprintf(stderr, "Not NEIGHTBL: %08x %08x %08x\n", | |
534 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
535 | return 0; | |
536 | } | |
537 | len -= NLMSG_LENGTH(sizeof(*ndtm)); | |
538 | if (len < 0) { | |
539 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
540 | return -1; | |
541 | } | |
56f5daac | 542 | |
3c1e087b SH |
543 | if (preferred_family && preferred_family != ndtm->ndtm_family) |
544 | return 0; | |
143969f2 | 545 | |
3c1e087b SH |
546 | parse_rtattr(tb, NDTA_MAX, NDTA_RTA(ndtm), |
547 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*ndtm))); | |
143969f2 | 548 | |
3c1e087b SH |
549 | if (tb[NDTA_NAME]) { |
550 | const char *name = rta_getattr_str(tb[NDTA_NAME]); | |
551 | ||
552 | if (filter.name && strcmp(filter.name, name)) | |
553 | return 0; | |
554 | } | |
143969f2 | 555 | |
3c1e087b SH |
556 | if (tb[NDTA_PARMS]) { |
557 | parse_rtattr(tpb, NDTPA_MAX, RTA_DATA(tb[NDTA_PARMS]), | |
558 | RTA_PAYLOAD(tb[NDTA_PARMS])); | |
56f5daac | 559 | |
3c1e087b SH |
560 | if (tpb[NDTPA_IFINDEX]) { |
561 | __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]); | |
56f5daac | 562 | |
3c1e087b SH |
563 | if (filter.index && filter.index != ifindex) |
564 | return 0; | |
565 | } else { | |
566 | if (filter.index && filter.index != NONE_DEV) | |
567 | return 0; | |
143969f2 | 568 | } |
3c1e087b | 569 | } |
56f5daac | 570 | |
3c1e087b SH |
571 | open_json_object(NULL); |
572 | print_string(PRINT_ANY, "family", | |
573 | "%s ", family_name(ndtm->ndtm_family)); | |
56f5daac | 574 | |
3c1e087b SH |
575 | if (tb[NDTA_NAME]) { |
576 | const char *name = rta_getattr_str(tb[NDTA_NAME]); | |
143969f2 | 577 | |
3c1e087b | 578 | print_string(PRINT_ANY, "name", "%s ", name); |
143969f2 SH |
579 | } |
580 | ||
b85076cd | 581 | print_nl(); |
143969f2 | 582 | |
3c1e087b SH |
583 | ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] || |
584 | tb[NDTA_GC_INTERVAL]); | |
585 | if (ret) | |
586 | print_string(PRINT_FP, NULL, " ", NULL); | |
143969f2 | 587 | |
3c1e087b SH |
588 | if (tb[NDTA_THRESH1]) { |
589 | __u32 thresh1 = rta_getattr_u32(tb[NDTA_THRESH1]); | |
143969f2 | 590 | |
3c1e087b SH |
591 | print_uint(PRINT_ANY, "thresh1", "thresh1 %u ", thresh1); |
592 | } | |
143969f2 | 593 | |
3c1e087b SH |
594 | if (tb[NDTA_THRESH2]) { |
595 | __u32 thresh2 = rta_getattr_u32(tb[NDTA_THRESH2]); | |
143969f2 | 596 | |
3c1e087b SH |
597 | print_uint(PRINT_ANY, "thresh2", "thresh2 %u ", thresh2); |
598 | } | |
143969f2 | 599 | |
3c1e087b SH |
600 | if (tb[NDTA_THRESH3]) { |
601 | __u32 thresh3 = rta_getattr_u32(tb[NDTA_THRESH3]); | |
143969f2 | 602 | |
3c1e087b SH |
603 | print_uint(PRINT_ANY, "thresh3", "thresh3 %u ", thresh3); |
604 | } | |
143969f2 | 605 | |
3c1e087b SH |
606 | if (tb[NDTA_GC_INTERVAL]) { |
607 | __u64 gc_int = rta_getattr_u64(tb[NDTA_GC_INTERVAL]); | |
143969f2 | 608 | |
4db2ff0d | 609 | print_u64(PRINT_ANY, "gc_interval", "gc_int %llu ", gc_int); |
143969f2 SH |
610 | } |
611 | ||
3c1e087b | 612 | if (ret) |
b85076cd | 613 | print_nl(); |
143969f2 | 614 | |
3c1e087b SH |
615 | if (tb[NDTA_CONFIG] && show_stats) |
616 | print_ndtconfig(RTA_DATA(tb[NDTA_CONFIG])); | |
617 | ||
618 | if (tb[NDTA_PARMS]) | |
619 | print_ndtparams(tpb); | |
620 | ||
621 | if (tb[NDTA_STATS] && show_stats) | |
622 | print_ndtstats(RTA_DATA(tb[NDTA_STATS])); | |
623 | ||
624 | print_string(PRINT_FP, NULL, "\n", ""); | |
625 | close_json_object(); | |
143969f2 | 626 | fflush(fp); |
3c1e087b | 627 | |
143969f2 SH |
628 | return 0; |
629 | } | |
630 | ||
f7b49a3f | 631 | static void ipntable_reset_filter(void) |
143969f2 SH |
632 | { |
633 | memset(&filter, 0, sizeof(filter)); | |
634 | } | |
635 | ||
636 | static int ipntable_show(int argc, char **argv) | |
637 | { | |
638 | ipntable_reset_filter(); | |
639 | ||
640 | filter.family = preferred_family; | |
641 | ||
642 | while (argc > 0) { | |
643 | if (strcmp(*argv, "dev") == 0) { | |
644 | NEXT_ARG(); | |
645 | ||
646 | if (strcmp("none", *argv) == 0) | |
647 | filter.index = NONE_DEV; | |
648 | else if ((filter.index = ll_name_to_index(*argv)) == 0) | |
649 | invarg("\"DEV\" is invalid", *argv); | |
650 | } else if (strcmp(*argv, "name") == 0) { | |
651 | NEXT_ARG(); | |
652 | ||
45c2ec9e | 653 | filter.name = *argv; |
143969f2 SH |
654 | } else |
655 | invarg("unknown", *argv); | |
656 | ||
657 | argc--; argv++; | |
658 | } | |
659 | ||
ff41db8a | 660 | if (rtnl_neightbldump_req(&rth, preferred_family) < 0) { |
143969f2 SH |
661 | perror("Cannot send dump request"); |
662 | exit(1); | |
663 | } | |
664 | ||
3c1e087b | 665 | new_json_obj(json); |
cd70f3f5 | 666 | if (rtnl_dump_filter(&rth, print_ntable, stdout) < 0) { |
143969f2 SH |
667 | fprintf(stderr, "Dump terminated\n"); |
668 | exit(1); | |
669 | } | |
3c1e087b | 670 | delete_json_obj(); |
143969f2 SH |
671 | |
672 | return 0; | |
673 | } | |
674 | ||
675 | int do_ipntable(int argc, char **argv) | |
676 | { | |
677 | ll_init_map(&rth); | |
678 | ||
679 | if (argc > 0) { | |
680 | if (matches(*argv, "change") == 0 || | |
681 | matches(*argv, "chg") == 0) | |
682 | return ipntable_modify(RTM_SETNEIGHTBL, | |
683 | NLM_F_REPLACE, | |
684 | argc-1, argv+1); | |
685 | if (matches(*argv, "show") == 0 || | |
686 | matches(*argv, "lst") == 0 || | |
687 | matches(*argv, "list") == 0) | |
688 | return ipntable_show(argc-1, argv+1); | |
689 | if (matches(*argv, "help") == 0) | |
690 | usage(); | |
691 | } else | |
692 | return ipntable_show(0, NULL); | |
693 | ||
694 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip ntable help\".\n", *argv); | |
695 | exit(-1); | |
696 | } |