]>
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"
26 static void usage(void)
28 fprintf(stderr
, "Usage: ip ila add loc_match LOCATOR_MATCH "
29 "loc LOCATOR [ dev DEV ]\n");
30 fprintf(stderr
, " ip ila del loc_match LOCATOR_MATCH "
31 "[ loc LOCATOR ] [ dev DEV ]\n");
32 fprintf(stderr
, " ip ila list\n");
33 fprintf(stderr
, "\n");
39 static struct rtnl_handle genl_rth
= { .fd
= -1 };
40 static int genl_family
= -1;
42 #define ILA_REQUEST(_req, _bufsiz, _cmd, _flags) \
43 GENL_REQUEST(_req, _bufsiz, genl_family, 0, \
44 ILA_GENL_VERSION, _cmd, _flags)
46 #define ILA_RTA(g) ((struct rtattr *)(((char *)(g)) + \
47 NLMSG_ALIGN(sizeof(struct genlmsghdr))))
49 #define ADDR_BUF_SIZE sizeof("xxxx:xxxx:xxxx:xxxx")
51 static int 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
);
75 static void print_ila_locid(FILE *fp
, int attr
, struct rtattr
*tb
[], int space
)
82 blen
= print_addr64(rta_getattr_u32(tb
[attr
]),
84 fprintf(fp
, "%s", abuf
);
90 for (i
= 0; i
< space
- blen
; i
++)
94 static int print_ila_mapping(const struct sockaddr_nl
*who
,
95 struct nlmsghdr
*n
, void *arg
)
97 FILE *fp
= (FILE *)arg
;
98 struct genlmsghdr
*ghdr
;
99 struct rtattr
*tb
[ILA_ATTR_MAX
+ 1];
100 int len
= n
->nlmsg_len
;
102 if (n
->nlmsg_type
!= genl_family
)
105 len
-= NLMSG_LENGTH(GENL_HDRLEN
);
109 ghdr
= NLMSG_DATA(n
);
110 parse_rtattr(tb
, ILA_ATTR_MAX
, (void *) ghdr
+ GENL_HDRLEN
, len
);
112 print_ila_locid(fp
, ILA_ATTR_LOCATOR_MATCH
, tb
, ADDR_BUF_SIZE
);
113 print_ila_locid(fp
, ILA_ATTR_LOCATOR
, tb
, ADDR_BUF_SIZE
);
115 if (tb
[ILA_ATTR_IFINDEX
])
116 fprintf(fp
, "%s", ll_index_to_name(rta_getattr_u32(tb
[ILA_ATTR_IFINDEX
])));
124 #define NLMSG_BUF_SIZE 4096
126 static int do_list(int argc
, char **argv
)
128 ILA_REQUEST(req
, 1024, ILA_CMD_GET
, NLM_F_REQUEST
| NLM_F_DUMP
);
131 fprintf(stderr
, "\"ip ila show\" does not take "
136 if (rtnl_send(&genl_rth
, (void *)&req
, req
.n
.nlmsg_len
) < 0) {
137 perror("Cannot send dump request");
141 if (rtnl_dump_filter(&genl_rth
, print_ila_mapping
, stdout
) < 0) {
142 fprintf(stderr
, "Dump terminated\n");
149 static int ila_parse_opt(int argc
, char **argv
, struct nlmsghdr
*n
,
153 __u64 locator_match
= 0;
155 bool loc_set
= false;
156 bool loc_match_set
= false;
157 bool ifindex_set
= false;
160 if (!matches(*argv
, "loc")) {
163 if (get_addr64(&locator
, *argv
) < 0) {
164 fprintf(stderr
, "Bad locator: %s\n", *argv
);
168 } else if (!matches(*argv
, "loc_match")) {
171 if (get_addr64(&locator_match
, *argv
) < 0) {
172 fprintf(stderr
, "Bad locator to match: %s\n",
176 loc_match_set
= true;
177 } else if (!matches(*argv
, "dev")) {
180 ifindex
= ll_name_to_index(*argv
);
182 fprintf(stderr
, "No such interface: %s\n",
196 fprintf(stderr
, "ila: missing locator\n");
199 if (!loc_match_set
) {
200 fprintf(stderr
, "ila: missing locator0match\n");
206 addattr64(n
, 1024, ILA_ATTR_LOCATOR_MATCH
, locator_match
);
209 addattr64(n
, 1024, ILA_ATTR_LOCATOR
, locator
);
212 addattr32(n
, 1024, ILA_ATTR_IFINDEX
, ifindex
);
217 static int do_add(int argc
, char **argv
)
219 ILA_REQUEST(req
, 1024, ILA_CMD_ADD
, NLM_F_REQUEST
);
221 ila_parse_opt(argc
, argv
, &req
.n
, true);
223 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
, 0) < 0)
229 static int do_del(int argc
, char **argv
)
231 ILA_REQUEST(req
, 1024, ILA_CMD_DEL
, NLM_F_REQUEST
);
233 ila_parse_opt(argc
, argv
, &req
.n
, false);
235 if (rtnl_talk(&genl_rth
, &req
.n
, NULL
, 0) < 0)
241 int do_ipila(int argc
, char **argv
)
246 if (matches(*argv
, "help") == 0)
249 if (genl_init_handle(&genl_rth
, ILA_GENL_NAME
, &genl_family
))
252 if (matches(*argv
, "add") == 0)
253 return do_add(argc
-1, argv
+1);
254 if (matches(*argv
, "delete") == 0)
255 return do_del(argc
-1, argv
+1);
256 if (matches(*argv
, "list") == 0)
257 return do_list(argc
-1, argv
+1);
259 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip ila help\".\n",