]>
Commit | Line | Data |
---|---|---|
1 | /* | |
2 | * iplink_macvtap.c macvtap device support | |
3 | * | |
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. | |
8 | */ | |
9 | ||
10 | #include <stdio.h> | |
11 | #include <stdlib.h> | |
12 | #include <string.h> | |
13 | #include <sys/socket.h> | |
14 | #include <linux/if_link.h> | |
15 | ||
16 | #include "rt_names.h" | |
17 | #include "utils.h" | |
18 | #include "ip_common.h" | |
19 | ||
20 | static void print_explain(FILE *f) | |
21 | { | |
22 | fprintf(stderr, | |
23 | "Usage: ... macvtap mode { private | vepa | bridge | passthru }\n" | |
24 | ); | |
25 | } | |
26 | ||
27 | static void explain(void) | |
28 | { | |
29 | print_explain(stderr); | |
30 | } | |
31 | ||
32 | static int mode_arg(const char *arg) | |
33 | { | |
34 | fprintf(stderr, "Error: argument of \"mode\" must be \"private\", " | |
35 | "\"vepa\", \"bridge\" or \"passthru\", not \"%s\"\n", arg); | |
36 | return -1; | |
37 | } | |
38 | ||
39 | static int macvtap_parse_opt(struct link_util *lu, int argc, char **argv, | |
40 | struct nlmsghdr *n) | |
41 | { | |
42 | while (argc > 0) { | |
43 | if (matches(*argv, "mode") == 0) { | |
44 | __u32 mode = 0; | |
45 | NEXT_ARG(); | |
46 | ||
47 | if (strcmp(*argv, "private") == 0) | |
48 | mode = MACVLAN_MODE_PRIVATE; | |
49 | else if (strcmp(*argv, "vepa") == 0) | |
50 | mode = MACVLAN_MODE_VEPA; | |
51 | else if (strcmp(*argv, "bridge") == 0) | |
52 | mode = MACVLAN_MODE_BRIDGE; | |
53 | else if (strcmp(*argv, "passthru") == 0) | |
54 | mode = MACVLAN_MODE_PASSTHRU; | |
55 | else | |
56 | return mode_arg(*argv); | |
57 | ||
58 | addattr32(n, 1024, IFLA_MACVLAN_MODE, mode); | |
59 | } else if (matches(*argv, "help") == 0) { | |
60 | explain(); | |
61 | return -1; | |
62 | } else { | |
63 | fprintf(stderr, "macvtap: unknown command \"%s\"?\n", *argv); | |
64 | explain(); | |
65 | return -1; | |
66 | } | |
67 | argc--, argv++; | |
68 | } | |
69 | ||
70 | return 0; | |
71 | } | |
72 | ||
73 | static void macvtap_print_opt(struct link_util *lu, FILE *f, struct rtattr *tb[]) | |
74 | { | |
75 | __u32 mode; | |
76 | ||
77 | if (!tb) | |
78 | return; | |
79 | ||
80 | if (!tb[IFLA_MACVLAN_MODE] || | |
81 | RTA_PAYLOAD(tb[IFLA_MACVLAN_MODE]) < sizeof(__u32)) | |
82 | return; | |
83 | ||
84 | mode = rta_getattr_u32(tb[IFLA_VLAN_ID]); | |
85 | fprintf(f, " mode %s ", | |
86 | mode == MACVLAN_MODE_PRIVATE ? "private" | |
87 | : mode == MACVLAN_MODE_VEPA ? "vepa" | |
88 | : mode == MACVLAN_MODE_BRIDGE ? "bridge" | |
89 | : mode == MACVLAN_MODE_PASSTHRU ? "passthru" | |
90 | : "unknown"); | |
91 | } | |
92 | ||
93 | static void macvtap_print_help(struct link_util *lu, int argc, char **argv, | |
94 | FILE *f) | |
95 | { | |
96 | print_explain(f); | |
97 | } | |
98 | ||
99 | struct link_util macvtap_link_util = { | |
100 | .id = "macvtap", | |
101 | .maxattr = IFLA_MACVLAN_MAX, | |
102 | .parse_opt = macvtap_parse_opt, | |
103 | .print_opt = macvtap_print_opt, | |
104 | .print_help = macvtap_print_help, | |
105 | }; |