1 /* vconfig.c - Creates virtual ethernet devices.
3 * Copyright 2012 Sandeep Sharma <sandeep.jack2756@gmail.com>
4 * Copyright 2012 Kyungwan Han <asura321@gmail.com>
8 USE_VCONFIG(NEWTOY(vconfig, "<2>4", TOYFLAG_NEEDROOT|TOYFLAG_SBIN))
14 usage: vconfig COMMAND [OPTIONS]
16 add [interface-name] [vlan_id]
18 set_flag [interface-name] [flag-num] [0 | 1]
19 set_egress_map [vlan-name] [skb_priority] [vlan_qos]
20 set_ingress_map [vlan-name] [skb_priority] [vlan_qos]
21 set_name_type [name-type]
23 Create and remove virtual ethernet devices
27 #include <linux/if_vlan.h>
28 #include <linux/sockios.h>
30 void vconfig_main(void)
32 struct vlan_ioctl_args request;
36 fd = xsocket(AF_INET, SOCK_STREAM, 0);
37 memset(&request, 0, sizeof(struct vlan_ioctl_args));
38 cmd = toys.optargs[0];
40 if (!strcmp(cmd, "set_name_type")) {
41 char *types[] = {"VLAN_PLUS_VID", "DEV_PLUS_VID", "VLAN_PLUS_VID_NO_PAD",
42 "DEV_PLUS_VID_NO_PAD"};
43 int i, j = sizeof(types)/sizeof(*types);
45 for (i=0; i<j; i++) if (!strcmp(toys.optargs[1], types[i])) break;
47 for (i=0; i<j; i++) puts(types[i]);
48 error_exit("%s: unknown '%s'", cmd, toys.optargs[1]);
51 request.u.name_type = i;
52 request.cmd = SET_VLAN_NAME_TYPE_CMD;
53 xioctl(fd, SIOCSIFVLAN, &request);
57 // Store interface name
58 xstrncpy(request.device1, toys.optargs[1], 16);
60 if (!strcmp(cmd, "add")) {
61 request.cmd = ADD_VLAN_CMD;
62 if (toys.optargs[2]) request.u.VID = atolx_range(toys.optargs[2], 0, 4094);
63 if (request.u.VID == 1)
64 xprintf("WARNING: VLAN 1 does not work with many switches.\n");
65 } else if (!strcmp(cmd, "rem")) request.cmd = DEL_VLAN_CMD;
66 else if (!strcmp(cmd, "set_flag")) {
67 request.cmd = SET_VLAN_FLAG_CMD;
68 if (toys.optargs[2]) request.u.flag = atolx_range(toys.optargs[2], 0, 1);
69 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
70 } else if(strcmp(cmd, "set_egress_map") == 0) {
71 request.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
73 request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
74 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
75 } else if(strcmp(cmd, "set_ingress_map") == 0) {
76 request.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
78 request.u.skb_priority = atolx_range(toys.optargs[2], 0, INT_MAX);
79 //To set flag we must have to set vlan_qos
80 if (toys.optargs[3]) request.vlan_qos = atolx_range(toys.optargs[3], 0, 7);
83 perror_exit("Unknown command %s", cmd);
86 xioctl(fd, SIOCSIFVLAN, &request);
87 xprintf("Successful %s on device %s\n", cmd, toys.optargs[1]);