]>
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 | |
3c1e087b | 349 | print_string(PRINT_FP, NULL, "%s", _SL_); |
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 | |
3c1e087b | 358 | print_string(PRINT_FP, NULL, "%s", _SL_); |
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 | |
3c1e087b SH |
370 | print_string(PRINT_FP, NULL, "%s", _SL_); |
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)); | |
382 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
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 | ||
395 | print_uint(PRINT_ANY, "reachable", | |
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 | |
3c1e087b SH |
403 | print_uint(PRINT_ANY, "base_reachable", |
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 | |
3c1e087b | 410 | print_uint(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 | ||
418 | print_uint(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 | ||
425 | print_uint(PRINT_ANY, "delay_probe", | |
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 | |
3c1e087b SH |
462 | print_uint(PRINT_ANY, "anycast_delay", |
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 | |
3c1e087b SH |
469 | print_uint(PRINT_ANY, "proxy_delay", |
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 | |
3c1e087b SH |
482 | print_uint(PRINT_ANY, "locktime", "locktime %llu ", locktime); |
483 | } | |
56f5daac | 484 | |
3c1e087b SH |
485 | print_string(PRINT_FP, NULL, "%s", _SL_); |
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 | |
3c1e087b SH |
493 | print_uint(PRINT_ANY, "allocs", "allocs %llu ", ndts->ndts_allocs); |
494 | print_uint(PRINT_ANY, "destroys", "destroys %llu ", | |
495 | ndts->ndts_destroys); | |
496 | print_uint(PRINT_ANY, "hash_grows", "hash_grows %llu ", | |
497 | ndts->ndts_hash_grows); | |
143969f2 | 498 | |
3c1e087b | 499 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
56f5daac | 500 | |
3c1e087b SH |
501 | print_uint(PRINT_ANY, "res_failed", "res_failed %llu ", |
502 | ndts->ndts_res_failed); | |
503 | print_uint(PRINT_ANY, "lookups", "lookups %llu ", ndts->ndts_lookups); | |
504 | print_uint(PRINT_ANY, "hits", "hits %llu ", ndts->ndts_hits); | |
56f5daac | 505 | |
3c1e087b | 506 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
56f5daac | 507 | |
3c1e087b SH |
508 | print_uint(PRINT_ANY, "rcv_probes_mcast", "rcv_probes_mcast %llu ", |
509 | ndts->ndts_rcv_probes_mcast); | |
510 | print_uint(PRINT_ANY, "rcv_probes_ucast", "rcv_probes_ucast %llu ", | |
511 | ndts->ndts_rcv_probes_ucast); | |
143969f2 | 512 | |
3c1e087b | 513 | print_string(PRINT_FP, NULL, "%s ", _SL_); |
143969f2 | 514 | |
3c1e087b SH |
515 | print_uint(PRINT_ANY, "periodic_gc_runs", "periodic_gc_runs %llu ", |
516 | ndts->ndts_periodic_gc_runs); | |
517 | print_uint(PRINT_ANY, "forced_gc_runs", "forced_gc_runs %llu ", | |
518 | ndts->ndts_forced_gc_runs); | |
143969f2 | 519 | |
3c1e087b SH |
520 | print_string(PRINT_FP, NULL, "%s", _SL_); |
521 | } | |
56f5daac | 522 | |
3c1e087b SH |
523 | static int print_ntable(const struct sockaddr_nl *who, |
524 | struct nlmsghdr *n, void *arg) | |
525 | { | |
526 | FILE *fp = (FILE *)arg; | |
527 | struct ndtmsg *ndtm = NLMSG_DATA(n); | |
528 | int len = n->nlmsg_len; | |
529 | struct rtattr *tb[NDTA_MAX+1]; | |
530 | struct rtattr *tpb[NDTPA_MAX+1]; | |
531 | int ret; | |
56f5daac | 532 | |
3c1e087b SH |
533 | if (n->nlmsg_type != RTM_NEWNEIGHTBL) { |
534 | fprintf(stderr, "Not NEIGHTBL: %08x %08x %08x\n", | |
535 | n->nlmsg_len, n->nlmsg_type, n->nlmsg_flags); | |
536 | return 0; | |
537 | } | |
538 | len -= NLMSG_LENGTH(sizeof(*ndtm)); | |
539 | if (len < 0) { | |
540 | fprintf(stderr, "BUG: wrong nlmsg len %d\n", len); | |
541 | return -1; | |
542 | } | |
56f5daac | 543 | |
3c1e087b SH |
544 | if (preferred_family && preferred_family != ndtm->ndtm_family) |
545 | return 0; | |
143969f2 | 546 | |
3c1e087b SH |
547 | parse_rtattr(tb, NDTA_MAX, NDTA_RTA(ndtm), |
548 | n->nlmsg_len - NLMSG_LENGTH(sizeof(*ndtm))); | |
143969f2 | 549 | |
3c1e087b SH |
550 | if (tb[NDTA_NAME]) { |
551 | const char *name = rta_getattr_str(tb[NDTA_NAME]); | |
552 | ||
553 | if (filter.name && strcmp(filter.name, name)) | |
554 | return 0; | |
555 | } | |
143969f2 | 556 | |
3c1e087b SH |
557 | if (tb[NDTA_PARMS]) { |
558 | parse_rtattr(tpb, NDTPA_MAX, RTA_DATA(tb[NDTA_PARMS]), | |
559 | RTA_PAYLOAD(tb[NDTA_PARMS])); | |
56f5daac | 560 | |
3c1e087b SH |
561 | if (tpb[NDTPA_IFINDEX]) { |
562 | __u32 ifindex = rta_getattr_u32(tpb[NDTPA_IFINDEX]); | |
56f5daac | 563 | |
3c1e087b SH |
564 | if (filter.index && filter.index != ifindex) |
565 | return 0; | |
566 | } else { | |
567 | if (filter.index && filter.index != NONE_DEV) | |
568 | return 0; | |
143969f2 | 569 | } |
3c1e087b | 570 | } |
56f5daac | 571 | |
3c1e087b SH |
572 | open_json_object(NULL); |
573 | print_string(PRINT_ANY, "family", | |
574 | "%s ", family_name(ndtm->ndtm_family)); | |
56f5daac | 575 | |
3c1e087b SH |
576 | if (tb[NDTA_NAME]) { |
577 | const char *name = rta_getattr_str(tb[NDTA_NAME]); | |
143969f2 | 578 | |
3c1e087b | 579 | print_string(PRINT_ANY, "name", "%s ", name); |
143969f2 SH |
580 | } |
581 | ||
3c1e087b | 582 | print_string(PRINT_FP, NULL, "%s", _SL_); |
143969f2 | 583 | |
3c1e087b SH |
584 | ret = (tb[NDTA_THRESH1] || tb[NDTA_THRESH2] || tb[NDTA_THRESH3] || |
585 | tb[NDTA_GC_INTERVAL]); | |
586 | if (ret) | |
587 | print_string(PRINT_FP, NULL, " ", NULL); | |
143969f2 | 588 | |
3c1e087b SH |
589 | if (tb[NDTA_THRESH1]) { |
590 | __u32 thresh1 = rta_getattr_u32(tb[NDTA_THRESH1]); | |
143969f2 | 591 | |
3c1e087b SH |
592 | print_uint(PRINT_ANY, "thresh1", "thresh1 %u ", thresh1); |
593 | } | |
143969f2 | 594 | |
3c1e087b SH |
595 | if (tb[NDTA_THRESH2]) { |
596 | __u32 thresh2 = rta_getattr_u32(tb[NDTA_THRESH2]); | |
143969f2 | 597 | |
3c1e087b SH |
598 | print_uint(PRINT_ANY, "thresh2", "thresh2 %u ", thresh2); |
599 | } | |
143969f2 | 600 | |
3c1e087b SH |
601 | if (tb[NDTA_THRESH3]) { |
602 | __u32 thresh3 = rta_getattr_u32(tb[NDTA_THRESH3]); | |
143969f2 | 603 | |
3c1e087b SH |
604 | print_uint(PRINT_ANY, "thresh3", "thresh3 %u ", thresh3); |
605 | } | |
143969f2 | 606 | |
3c1e087b SH |
607 | if (tb[NDTA_GC_INTERVAL]) { |
608 | __u64 gc_int = rta_getattr_u64(tb[NDTA_GC_INTERVAL]); | |
143969f2 | 609 | |
3c1e087b | 610 | print_uint(PRINT_ANY, "gc_interval", "gc_int %llu ", gc_int); |
143969f2 SH |
611 | } |
612 | ||
3c1e087b SH |
613 | if (ret) |
614 | print_string(PRINT_FP, NULL, "%s", _SL_); | |
143969f2 | 615 | |
3c1e087b SH |
616 | if (tb[NDTA_CONFIG] && show_stats) |
617 | print_ndtconfig(RTA_DATA(tb[NDTA_CONFIG])); | |
618 | ||
619 | if (tb[NDTA_PARMS]) | |
620 | print_ndtparams(tpb); | |
621 | ||
622 | if (tb[NDTA_STATS] && show_stats) | |
623 | print_ndtstats(RTA_DATA(tb[NDTA_STATS])); | |
624 | ||
625 | print_string(PRINT_FP, NULL, "\n", ""); | |
626 | close_json_object(); | |
143969f2 | 627 | fflush(fp); |
3c1e087b | 628 | |
143969f2 SH |
629 | return 0; |
630 | } | |
631 | ||
f7b49a3f | 632 | static void ipntable_reset_filter(void) |
143969f2 SH |
633 | { |
634 | memset(&filter, 0, sizeof(filter)); | |
635 | } | |
636 | ||
637 | static int ipntable_show(int argc, char **argv) | |
638 | { | |
639 | ipntable_reset_filter(); | |
640 | ||
641 | filter.family = preferred_family; | |
642 | ||
643 | while (argc > 0) { | |
644 | if (strcmp(*argv, "dev") == 0) { | |
645 | NEXT_ARG(); | |
646 | ||
647 | if (strcmp("none", *argv) == 0) | |
648 | filter.index = NONE_DEV; | |
649 | else if ((filter.index = ll_name_to_index(*argv)) == 0) | |
650 | invarg("\"DEV\" is invalid", *argv); | |
651 | } else if (strcmp(*argv, "name") == 0) { | |
652 | NEXT_ARG(); | |
653 | ||
45c2ec9e | 654 | filter.name = *argv; |
143969f2 SH |
655 | } else |
656 | invarg("unknown", *argv); | |
657 | ||
658 | argc--; argv++; | |
659 | } | |
660 | ||
661 | if (rtnl_wilddump_request(&rth, preferred_family, RTM_GETNEIGHTBL) < 0) { | |
662 | perror("Cannot send dump request"); | |
663 | exit(1); | |
664 | } | |
665 | ||
3c1e087b | 666 | new_json_obj(json); |
cd70f3f5 | 667 | if (rtnl_dump_filter(&rth, print_ntable, stdout) < 0) { |
143969f2 SH |
668 | fprintf(stderr, "Dump terminated\n"); |
669 | exit(1); | |
670 | } | |
3c1e087b | 671 | delete_json_obj(); |
143969f2 SH |
672 | |
673 | return 0; | |
674 | } | |
675 | ||
676 | int do_ipntable(int argc, char **argv) | |
677 | { | |
678 | ll_init_map(&rth); | |
679 | ||
680 | if (argc > 0) { | |
681 | if (matches(*argv, "change") == 0 || | |
682 | matches(*argv, "chg") == 0) | |
683 | return ipntable_modify(RTM_SETNEIGHTBL, | |
684 | NLM_F_REPLACE, | |
685 | argc-1, argv+1); | |
686 | if (matches(*argv, "show") == 0 || | |
687 | matches(*argv, "lst") == 0 || | |
688 | matches(*argv, "list") == 0) | |
689 | return ipntable_show(argc-1, argv+1); | |
690 | if (matches(*argv, "help") == 0) | |
691 | usage(); | |
692 | } else | |
693 | return ipntable_show(0, NULL); | |
694 | ||
695 | fprintf(stderr, "Command \"%s\" is unknown, try \"ip ntable help\".\n", *argv); | |
696 | exit(-1); | |
697 | } |