]>
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(const struct sockaddr_nl
*who
,
85 struct nlmsghdr
*n
, void *arg
)
87 struct genlmsghdr
*ghdr
;
88 struct rtattr
*tb
[ILA_ATTR_MAX
+ 1];
89 int len
= n
->nlmsg_len
;
91 if (n
->nlmsg_type
!= genl_family
)
94 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
99 parse_rtattr(tb
, ILA_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
, len
);
101 open_json_object(NULL
);
102 print_ila_locid("locator_match", ILA_ATTR_LOCATOR_MATCH
, tb
);
103 print_ila_locid("locator", ILA_ATTR_LOCATOR
, tb
);
105 if (tb
[ILA_ATTR_IFINDEX
]) {
107 = rta_getattr_u32(tb
[ILA_ATTR_IFINDEX
]);
109 print_color_string(PRINT_ANY
, COLOR_IFNAME
,
110 "interface", "%-16s",
111 ll_index_to_name(ifindex
));
113 print_string(PRINT_FP
, NULL
, "%-10s ", "-");
116 if (tb
[ILA_ATTR_CSUM_MODE
]) {
117 __u8 csum
= rta_getattr_u8(tb
[ILA_ATTR_CSUM_MODE
]);
119 print_string(PRINT_ANY
, "csum_mode", "%s",
120 ila_csum_mode2name(csum
));
122 print_string(PRINT_FP
, NULL
, "%-10s ", "-");
124 if (tb
[ILA_ATTR_IDENT_TYPE
])
125 print_string(PRINT_ANY
, "ident_type", "%s",
126 ila_ident_type2name(rta_getattr_u8(
127 tb
[ILA_ATTR_IDENT_TYPE
])));
129 print_string(PRINT_FP
, NULL
, "%s", "-");
131 print_string(PRINT_FP
, NULL
, "%s", _SL_
);
137 #define NLMSG_BUF_SIZE 4096
139 static int do_list(int argc
, char **argv
)
141 ILA_REQUEST(req
, 1024, ILA_CMD_GET
, NLM_F_REQUEST
| NLM_F_DUMP
);
144 fprintf(stderr
, "\"ip ila show\" does not take "
149 if (rtnl_send(&genl_rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
150 perror("Cannot send dump request");
155 if (rtnl_dump_filter(&genl_rth
, print_ila_mapping
, stdout
) < 0) {
156 fprintf(stderr
, "Dump terminated\n");
165 static int ila_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
,
169 __u64 locator_match
= 0;
173 bool loc_set
= false;
174 bool loc_match_set
= false;
175 bool ifindex_set
= false;
176 bool csum_mode_set
= false;
177 bool ident_type_set
= false;
180 if (!matches(*argv
, "loc")) {
183 if (get_addr64(&locator
, *argv
) < 0) {
184 fprintf(stderr
, "Bad locator: %s\n", *argv
);
188 } else if (!matches(*argv
, "loc_match")) {
191 if (get_addr64(&locator_match
, *argv
) < 0) {
192 fprintf(stderr
, "Bad locator to match: %s\n",
196 loc_match_set
= true;
197 } else if (!matches(*argv
, "csum-mode")) {
200 csum_mode
= ila_csum_name2mode(*argv
);
202 fprintf(stderr
, "Bad csum-mode: %s\n",
206 csum_mode_set
= true;
207 } else if (!matches(*argv
, "ident-type")) {
210 ident_type
= ila_ident_name2type(*argv
);
211 if (ident_type
< 0) {
212 fprintf(stderr
, "Bad ident-type: %s\n",
216 ident_type_set
= true;
217 } else if (!matches(*argv
, "dev")) {
220 ifindex
= ll_name_to_index(*argv
);
222 fprintf(stderr
, "No such interface: %s\n",
236 fprintf(stderr
, "ila: missing locator\n");
239 if (!loc_match_set
) {
240 fprintf(stderr
, "ila: missing locator0match\n");
246 addattr64(n
, 1024, ILA_ATTR_LOCATOR_MATCH
, locator_match
);
249 addattr64(n
, 1024, ILA_ATTR_LOCATOR
, locator
);
252 addattr32(n
, 1024, ILA_ATTR_IFINDEX
, ifindex
);
255 addattr8(n
, 1024, ILA_ATTR_CSUM_MODE
, csum_mode
);
258 addattr8(n
, 1024, ILA_ATTR_IDENT_TYPE
, ident_type
);
263 static int do_add(int argc
, char **argv
)
265 ILA_REQUEST(req
, 1024, ILA_CMD_ADD
, NLM_F_REQUEST
);
267 ila_parse_opt(argc
, argv
, &req
.n
, true);
269 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
275 static int do_del(int argc
, char **argv
)
277 ILA_REQUEST(req
, 1024, ILA_CMD_DEL
, NLM_F_REQUEST
);
279 ila_parse_opt(argc
, argv
, &req
.n
, false);
281 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
) < 0)
287 int do_ipila(int argc
, char **argv
)
292 if (matches(*argv
, "help") == 0)
295 if (genl_init_handle(&genl_rth
, ILA_GENL_NAME
, &genl_family
))
298 if (matches(*argv
, "add") == 0)
299 return do_add(argc
-1, argv
+1);
300 if (matches(*argv
, "delete") == 0)
301 return do_del(argc
-1, argv
+1);
302 if (matches(*argv
, "list") == 0)
303 return do_list(argc
-1, argv
+1);
305 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip ila help\".\n",