]>
git.proxmox.com Git - mirror_iproute2.git/blob - ip/ipila.c
2 * ipila.c ILA (Identifier Locator Addressing) support
4 * This program is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU General Public License
6 * as published by the Free Software Foundation; either version
7 * 2 of the License, or (at your option) any later version.
9 * Authors: Tom Herbert <tom@herbertland.com>
17 #include <linux/ila.h>
18 #include <linux/genetlink.h>
20 #include <arpa/inet.h>
24 #include "ip_common.h"
25 #include "ila_common.h"
26 #include "json_print.h"
28 static void usage(void)
31 "Usage: ip ila add loc_match LOCATOR_MATCH loc LOCATOR [ dev DEV ] OPTIONS\n"
32 " ip ila del loc_match LOCATOR_MATCH [ loc LOCATOR ] [ dev DEV ]\n"
34 "OPTIONS := [ csum-mode { adj-transport | neutral-map | neutral-map-auto | no-action } ]\n"
35 " [ ident-type { luid | use-format } ]\n");
41 static struct rtnl_handle genl_rth
= { .fd
= -1 };
42 static int genl_family
= -1;
44 #define ILA_REQUEST(_req, _bufsiz, _cmd, _flags) \
45 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
46 ILA_GENL_VERSION, _cmd, _flags)
48 #define ILA_RTA(g) ((struct rtattr *)(((char *)(g)) + \
49 NLMSG_ALIGN(sizeof(struct genlmsghdr))))
51 static void print_addr64(__u64 addr
, char *buff
, size_t len
)
53 __u16
*words
= (__u16
*)&addr
;
59 for (i
= 0; i
< 4; i
++) {
65 ret
= snprintf(&buff
[written
], len
- written
, "%x%s", v
, sep
);
70 static void print_ila_locid(const char *tag
, int attr
, struct rtattr
*tb
[])
75 print_addr64(rta_getattr_u64(tb
[attr
]),
78 snprintf(abuf
, sizeof(abuf
), "-");
80 /* 20 = sizeof("xxxx:xxxx:xxxx:xxxx") */
81 print_string(PRINT_ANY
, tag
, "%-20s", abuf
);
84 static int print_ila_mapping(struct nlmsghdr
*n
, void *arg
)
86 struct genlmsghdr
*ghdr
;
87 struct rtattr
*tb
[ILA_ATTR_MAX
+ 1];
88 int len
= n
->nlmsg_len
;
90 if (n
->nlmsg_type
!= genl_family
)
93 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
98 parse_rtattr(tb
, ILA_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
, len
);
100 open_json_object(NULL
);
101 print_ila_locid("locator_match", ILA_ATTR_LOCATOR_MATCH
, tb
);
102 print_ila_locid("locator", ILA_ATTR_LOCATOR
, tb
);
104 if (tb
[ILA_ATTR_IFINDEX
]) {
106 = rta_getattr_u32(tb
[ILA_ATTR_IFINDEX
]);
108 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
109 "interface", "%-16s",
110 ll_index_to_name(ifindex
));
112 print_string(PRINT_FP
, NULL
, "%-10s ", "-");
115 if (tb
[ILA_ATTR_CSUM_MODE
]) {
116 __u8 csum
= rta_getattr_u8(tb
[ILA_ATTR_CSUM_MODE
]);
118 print_string(PRINT_ANY
, "csum_mode", "%s",
119 ila_csum_mode2name(csum
));
121 print_string(PRINT_FP
, NULL
, "%-10s ", "-");
123 if (tb
[ILA_ATTR_IDENT_TYPE
])
124 print_string(PRINT_ANY
, "ident_type", "%s",
125 ila_ident_type2name(rta_getattr_u8(
126 tb
[ILA_ATTR_IDENT_TYPE
])));
128 print_string(PRINT_FP
, NULL
, "%s", "-");
136 #define NLMSG_BUF_SIZE 4096
138 static int do_list(int argc
, char **argv
)
140 ILA_REQUEST(req
, 1024, ILA_CMD_GET
, NLM_F_REQUEST
| NLM_F_DUMP
);
143 fprintf(stderr
, "\"ip ila show\" does not take "
148 if (rtnl_send(&genl_rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
149 perror("Cannot send dump request");
154 if (rtnl_dump_filter(&genl_rth
, print_ila_mapping
, stdout
) < 0) {
155 fprintf(stderr
, "Dump terminated\n");
164 static int ila_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
,
168 __u64 locator_match
= 0;
172 bool loc_set
= false;
173 bool loc_match_set
= false;
174 bool ifindex_set
= false;
175 bool csum_mode_set
= false;
176 bool ident_type_set
= false;
179 if (!matches(*argv
, "loc")) {
182 if (get_addr64(&locator
, *argv
) < 0) {
183 fprintf(stderr
, "Bad locator: %s\n", *argv
);
187 } else if (!matches(*argv
, "loc_match")) {
190 if (get_addr64(&locator_match
, *argv
) < 0) {
191 fprintf(stderr
, "Bad locator to match: %s\n",
195 loc_match_set
= true;
196 } else if (!matches(*argv
, "csum-mode")) {
199 csum_mode
= ila_csum_name2mode(*argv
);
201 fprintf(stderr
, "Bad csum-mode: %s\n",
205 csum_mode_set
= true;
206 } else if (!matches(*argv
, "ident-type")) {
209 ident_type
= ila_ident_name2type(*argv
);
210 if (ident_type
< 0) {
211 fprintf(stderr
, "Bad ident-type: %s\n",
215 ident_type_set
= true;
216 } else if (!matches(*argv
, "dev")) {
219 ifindex
= ll_name_to_index(*argv
);
221 fprintf(stderr
, "No such interface: %s\n",
235 fprintf(stderr
, "ila: missing locator\n");
238 if (!loc_match_set
) {
239 fprintf(stderr
, "ila: missing locator0match\n");
245 addattr64(n
, 1024, ILA_ATTR_LOCATOR_MATCH
, locator_match
);
248 addattr64(n
, 1024, ILA_ATTR_LOCATOR
, locator
);
251 addattr32(n
, 1024, ILA_ATTR_IFINDEX
, ifindex
);
254 addattr8(n
, 1024, ILA_ATTR_CSUM_MODE
, csum_mode
);
257 addattr8(n
, 1024, ILA_ATTR_IDENT_TYPE
, ident_type
);
262 static int do_add(int argc
, char **argv
)
264 ILA_REQUEST(req
, 1024, ILA_CMD_ADD
, NLM_F_REQUEST
);
266 ila_parse_opt(argc
, argv
, &req
.n
, true);
268 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
274 static int do_del(int argc
, char **argv
)
276 ILA_REQUEST(req
, 1024, ILA_CMD_DEL
, NLM_F_REQUEST
);
278 ila_parse_opt(argc
, argv
, &req
.n
, false);
280 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
286 int do_ipila(int argc
, char **argv
)
291 if (matches(*argv
, "help") == 0)
294 if (genl_init_handle(&genl_rth
, ILA_GENL_NAME
, &genl_family
))
297 if (matches(*argv
, "add") == 0)
298 return do_add(argc
-1, argv
+1);
299 if (matches(*argv
, "delete") == 0)
300 return do_del(argc
-1, argv
+1);
301 if (matches(*argv
, "list") == 0)
302 return do_list(argc
-1, argv
+1);
304 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip ila help\".\n",