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