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: David Ahern <dsa@cumulusnetworks.com>
13 #include <sys/types.h>
15 #include <sys/socket.h>
16 #include <sys/mount.h>
17 #include <linux/bpf.h>
30 #include "ip_common.h"
33 #define CGRP_PROC_FILE "/cgroup.procs"
35 static struct link_filter vrf_filter
;
37 static void usage(void)
40 "Usage: ip vrf show [NAME] ...\n"
41 " ip vrf exec [NAME] cmd ...\n"
42 " ip vrf identify [PID]\n"
43 " ip vrf pids [NAME]\n");
49 * parse process based cgroup file looking for PATH/vrf/NAME where
50 * NAME is the name of the vrf the process is associated with
52 static int vrf_identify(pid_t pid
, char *name
, size_t len
)
59 snprintf(path
, sizeof(path
), "/proc/%d/cgroup", pid
);
60 fp
= fopen(path
, "r");
66 while (fgets(buf
, sizeof(buf
), fp
)) {
67 /* want the controller-less cgroup */
68 if (strstr(buf
, "::/") == NULL
)
71 vrf
= strstr(buf
, "/vrf/");
73 vrf
+= 5; /* skip past "/vrf/" */
74 end
= strchr(vrf
, '\n');
78 strlcpy(name
, vrf
, len
);
88 static int ipvrf_identify(int argc
, char **argv
)
97 invarg("Extra arguments specified\n", argv
[1]);
98 else if (get_unsigned(&pid
, argv
[0], 10))
99 invarg("Invalid pid\n", argv
[0]);
101 rc
= vrf_identify(pid
, vrf
, sizeof(vrf
));
106 fprintf(stderr
, "Failed to lookup vrf association: %s\n",
113 /* read PATH/vrf/NAME/cgroup.procs file */
114 static void read_cgroup_pids(const char *base_path
, char *name
)
120 if (snprintf(path
, sizeof(path
), "%s/vrf/%s%s",
121 base_path
, name
, CGRP_PROC_FILE
) >= sizeof(path
))
124 fp
= fopen(path
, "r");
126 return; /* no cgroup file, nothing to show */
128 /* dump contents (pids) of cgroup.procs */
129 while (fgets(buf
, sizeof(buf
), fp
)) {
132 nl
= strchr(buf
, '\n');
136 if (get_command_name(buf
, comm
, sizeof(comm
)))
137 strcpy(comm
, "<terminated?>");
139 printf("%5s %s\n", buf
, comm
);
145 /* recurse path looking for PATH[/NETNS]/vrf/NAME */
146 static int recurse_dir(char *base_path
, char *name
, const char *netns
)
154 d
= opendir(base_path
);
158 while ((de
= readdir(d
)) != NULL
) {
159 if (!strcmp(de
->d_name
, ".") || !strcmp(de
->d_name
, ".."))
162 if (!strcmp(de
->d_name
, "vrf")) {
163 const char *pdir
= strrchr(base_path
, '/');
165 /* found a 'vrf' directory. if it is for the given
166 * namespace then dump the cgroup pids
168 if (*netns
== '\0' ||
169 (pdir
&& !strcmp(pdir
+1, netns
)))
170 read_cgroup_pids(base_path
, name
);
175 /* is this a subdir that needs to be walked */
176 if (snprintf(path
, sizeof(path
), "%s/%s",
177 base_path
, de
->d_name
) >= sizeof(path
))
180 if (lstat(path
, &fstat
) < 0)
183 if (S_ISDIR(fstat
.st_mode
)) {
184 rc
= recurse_dir(path
, name
, netns
);
197 static int ipvrf_get_netns(char *netns
, int len
)
199 if (netns_identify_pid("self", netns
, len
-3)) {
200 fprintf(stderr
, "Failed to get name of network namespace: %s\n",
206 strcat(netns
, "-ns");
211 static int ipvrf_pids(int argc
, char **argv
)
218 fprintf(stderr
, "Invalid arguments\n");
223 if (!name_is_vrf(vrf
)) {
224 fprintf(stderr
, "Invalid VRF name\n");
228 mnt
= find_cgroup2_mount();
232 if (ipvrf_get_netns(netns
, sizeof(netns
)) < 0)
235 ret
= recurse_dir(mnt
, vrf
, netns
);
243 /* load BPF program to set sk_bound_dev_if for sockets */
244 static char bpf_log_buf
[256*1024];
246 static int prog_load(int idx
)
248 struct bpf_insn prog
[] = {
249 BPF_MOV64_REG(BPF_REG_6
, BPF_REG_1
),
250 BPF_MOV64_IMM(BPF_REG_3
, idx
),
251 BPF_MOV64_IMM(BPF_REG_2
,
252 offsetof(struct bpf_sock
, bound_dev_if
)),
253 BPF_STX_MEM(BPF_W
, BPF_REG_1
, BPF_REG_3
,
254 offsetof(struct bpf_sock
, bound_dev_if
)),
255 BPF_MOV64_IMM(BPF_REG_0
, 1), /* r0 = verdict */
259 return bpf_prog_load(BPF_PROG_TYPE_CGROUP_SOCK
, prog
, sizeof(prog
),
260 "GPL", bpf_log_buf
, sizeof(bpf_log_buf
));
263 static int vrf_configure_cgroup(const char *path
, int ifindex
)
265 int rc
= -1, cg_fd
, prog_fd
= -1;
267 cg_fd
= open(path
, O_DIRECTORY
| O_RDONLY
);
270 "Failed to open cgroup path: '%s'\n",
276 * Load bpf program into kernel and attach to cgroup to affect
279 prog_fd
= prog_load(ifindex
);
281 fprintf(stderr
, "Failed to load BPF prog: '%s'\n",
284 if (errno
!= EPERM
) {
286 "Kernel compiled with CGROUP_BPF enabled?\n");
291 if (bpf_prog_attach_fd(prog_fd
, cg_fd
, BPF_CGROUP_INET_SOCK_CREATE
)) {
292 fprintf(stderr
, "Failed to attach prog to cgroup: '%s'\n",
305 /* get base path for controller-less cgroup for a process.
306 * path returned does not include /vrf/NAME if it exists
308 static int vrf_path(char *vpath
, size_t len
)
315 snprintf(path
, sizeof(path
), "/proc/%d/cgroup", getpid());
316 fp
= fopen(path
, "r");
322 while (fgets(buf
, sizeof(buf
), fp
)) {
325 start
= strstr(buf
, "::/");
329 /* advance past '::' */
332 nl
= strchr(start
, '\n');
336 vrf
= strstr(start
, "/vrf");
340 strlcpy(vpath
, start
, len
);
342 /* if vrf path is just / then return nothing */
343 if (!strcmp(vpath
, "/"))
354 static int vrf_switch(const char *name
)
356 char path
[PATH_MAX
], *mnt
, pid
[16];
357 char vpath
[PATH_MAX
], netns
[256];
359 int rc
= -1, len
, fd
= -1;
361 if (strcmp(name
, "default")) {
362 ifindex
= name_is_vrf(name
);
364 fprintf(stderr
, "Invalid VRF name\n");
369 mnt
= find_cgroup2_mount();
373 /* -1 on length to add '/' to the end */
374 if (ipvrf_get_netns(netns
, sizeof(netns
) - 1) < 0)
377 if (vrf_path(vpath
, sizeof(vpath
)) < 0) {
378 fprintf(stderr
, "Failed to get base cgroup path: %s\n",
383 /* if path already ends in netns then don't add it again */
384 if (*netns
!= '\0') {
385 char *pdir
= strrchr(vpath
, '/');
392 if (strcmp(pdir
, netns
) == 0)
398 /* path to cgroup; make sure buffer has room to cat "/cgroup.procs"
399 * to the end of the path
401 len
= snprintf(path
, sizeof(path
) - sizeof(CGRP_PROC_FILE
),
403 mnt
, vpath
, netns
, ifindex
? name
: "");
404 if (len
> sizeof(path
) - sizeof(CGRP_PROC_FILE
)) {
405 fprintf(stderr
, "Invalid path to cgroup2 mount\n");
409 if (make_path(path
, 0755)) {
410 fprintf(stderr
, "Failed to setup vrf cgroup2 directory\n");
414 if (ifindex
&& vrf_configure_cgroup(path
, ifindex
))
418 * write pid to cgroup.procs making process part of cgroup
420 strcat(path
, CGRP_PROC_FILE
);
421 fd
= open(path
, O_RDWR
| O_APPEND
);
423 fprintf(stderr
, "Failed to open cgroups.procs file: %s.\n",
428 snprintf(pid
, sizeof(pid
), "%d", getpid());
429 if (write(fd
, pid
, strlen(pid
)) < 0) {
430 fprintf(stderr
, "Failed to join cgroup\n");
445 static int ipvrf_exec(int argc
, char **argv
)
448 fprintf(stderr
, "No VRF name specified\n");
452 fprintf(stderr
, "No command specified\n");
456 if (vrf_switch(argv
[0]))
459 return -cmd_exec(argv
[1], argv
+ 1, !!batch_mode
);
462 /* reset VRF association of current process to default VRF;
469 if (vrf_identify(getpid(), vrf
, sizeof(vrf
)) ||
473 vrf_switch("default");
476 static int ipvrf_filter_req(struct nlmsghdr
*nlh
, int reqlen
)
478 struct rtattr
*linkinfo
;
481 if (vrf_filter
.kind
) {
482 linkinfo
= addattr_nest(nlh
, reqlen
, IFLA_LINKINFO
);
484 err
= addattr_l(nlh
, reqlen
, IFLA_INFO_KIND
, vrf_filter
.kind
,
485 strlen(vrf_filter
.kind
));
489 addattr_nest_end(nlh
, linkinfo
);
495 /* input arg is linkinfo */
496 static __u32
vrf_table_linkinfo(struct rtattr
*li
[])
498 struct rtattr
*attr
[IFLA_VRF_MAX
+ 1];
500 if (li
[IFLA_INFO_DATA
]) {
501 parse_rtattr_nested(attr
, IFLA_VRF_MAX
, li
[IFLA_INFO_DATA
]);
503 if (attr
[IFLA_VRF_TABLE
])
504 return rta_getattr_u32(attr
[IFLA_VRF_TABLE
]);
510 static int ipvrf_print(struct nlmsghdr
*n
)
512 struct ifinfomsg
*ifi
= NLMSG_DATA(n
);
513 struct rtattr
*tb
[IFLA_MAX
+1];
514 struct rtattr
*li
[IFLA_INFO_MAX
+1];
515 int len
= n
->nlmsg_len
;
519 len
-= NLMSG_LENGTH(sizeof(*ifi
));
523 if (vrf_filter
.ifindex
&& vrf_filter
.ifindex
!= ifi
->ifi_index
)
526 parse_rtattr(tb
, IFLA_MAX
, IFLA_RTA(ifi
), len
);
528 /* kernel does not support filter by master device */
529 if (tb
[IFLA_MASTER
]) {
530 int master
= *(int *)RTA_DATA(tb
[IFLA_MASTER
]);
532 if (vrf_filter
.master
&& master
!= vrf_filter
.master
)
536 if (!tb
[IFLA_IFNAME
]) {
538 "BUG: device with ifindex %d has nil ifname\n",
542 name
= rta_getattr_str(tb
[IFLA_IFNAME
]);
544 /* missing LINKINFO means not VRF. e.g., kernel does not
545 * support filtering on kind, so userspace needs to handle
547 if (!tb
[IFLA_LINKINFO
])
550 parse_rtattr_nested(li
, IFLA_INFO_MAX
, tb
[IFLA_LINKINFO
]);
552 if (!li
[IFLA_INFO_KIND
])
555 if (strcmp(RTA_DATA(li
[IFLA_INFO_KIND
]), "vrf"))
558 tb_id
= vrf_table_linkinfo(li
);
561 "BUG: VRF %s is missing table id\n", name
);
565 printf("%-16s %5u", name
, tb_id
);
571 static int ipvrf_show(int argc
, char **argv
)
573 struct nlmsg_chain linfo
= { NULL
, NULL
};
576 vrf_filter
.kind
= "vrf";
584 tb_id
= ipvrf_get_table(argv
[0]);
586 fprintf(stderr
, "Invalid VRF\n");
589 printf("%s %u\n", argv
[0], tb_id
);
593 if (ip_link_list(ipvrf_filter_req
, &linfo
) == 0) {
594 struct nlmsg_list
*l
;
598 n
= printf("%-16s %5s\n", "Name", "Table");
599 printf("%.*s\n", n
-1, "-----------------------");
600 for (l
= linfo
.head
; l
; l
= l
->next
)
601 nvrf
+= ipvrf_print(&l
->h
);
604 printf("No VRF has been configured\n");
608 free_nlmsg_chain(&linfo
);
613 int do_ipvrf(int argc
, char **argv
)
616 return ipvrf_show(0, NULL
);
618 if (matches(*argv
, "identify") == 0)
619 return ipvrf_identify(argc
-1, argv
+1);
621 if (matches(*argv
, "pids") == 0)
622 return ipvrf_pids(argc
-1, argv
+1);
624 if (matches(*argv
, "exec") == 0)
625 return ipvrf_exec(argc
-1, argv
+1);
627 if (matches(*argv
, "show") == 0 ||
628 matches(*argv
, "lst") == 0 ||
629 matches(*argv
, "list") == 0)
630 return ipvrf_show(argc
-1, argv
+1);
632 if (matches(*argv
, "help") == 0)
635 fprintf(stderr
, "Command \"%s\" is unknown, try \"ip vrf help\".\n",