[BusyBox] vlanconfig - alternative to vconfig applet

Nick Fedchik fnm at fusion.ukrsat.com
Sat Jan 25 09:43:03 UTC 2003


Hi All!

One time ago I had intention to do vconfig applet for busybox.
Actually I used separated executable module.
Now I found vconfig applet into bb sources.
But vconfig is very raw and when I tried to optimize it, I made
so much more changes and have made a decision to make new  applet
- vlanconfig.

# size v*.o
   text    data     bss     dec     hex filename
   2504       0       0    2504     9c8 vconfig.o
    998       0       0     998     3e6 vlanconfig.o

# ls -log v*.o
-rw-r--r--    1     4300 Jan 25 18:25 vconfig.o
-rw-r--r--    1     2404 Jan 25 18:25 vlanconfig.o

Main diff-s:
command "rem" changed to "del" (like ifconfig, ip, route)
commands "set_" splitted to "set" <options>

The patch attached.

brctl is next in the my busybox TODO.

-- 
Nick Fedchik, FNM3-RIPE(UANIC)
Internet Dept./ISP UkrSat, Kiev, Ukraine
-------------- next part --------------
diff -urN busybox/networking/Config.in busybox-dirty/networking/Config.in
--- busybox/networking/Config.in	2003-01-23 07:59:32 +0200
+++ busybox-dirty/networking/Config.in	2003-01-25 17:16:35 +0200
@@ -382,6 +382,13 @@
 	help
 	  Creates, removes, and configures VLAN interfaces
 
+config CONFIG_VLANCONFIG
+	bool "vlanconfig"
+	default n
+	help
+	  Utility to add, delete or configure VLAN-aware interfaces.
+	  Alternative to vconfig.
+
 config CONFIG_WGET
 	bool "wget"
 	default n
diff -urN busybox/networking/Makefile.in busybox-dirty/networking/Makefile.in
--- busybox/networking/Makefile.in	2003-01-23 07:59:32 +0200
+++ busybox-dirty/networking/Makefile.in	2003-01-25 17:16:49 +0200
@@ -48,6 +48,7 @@
 NETWORKING-$(CONFIG_TFTP)		+= tftp.o
 NETWORKING-$(CONFIG_TRACEROUTE)	+= traceroute.o
 NETWORKING-$(CONFIG_VCONFIG)		+= vconfig.o
+NETWORKING-$(CONFIG_VLANCONFIG)		+= vlanconfig.o
 NETWORKING-$(CONFIG_WGET)		+= wget.o
 
 libraries-y+=$(NETWORKING_DIR)$(NETWORKING_AR)
diff -urN busybox/networking/vlanconfig.c busybox-dirty/networking/vlanconfig.c
--- busybox/networking/vlanconfig.c	1970-01-01 03:00:00 +0300
+++ busybox-dirty/networking/vlanconfig.c	2003-01-25 18:17:24 +0200
@@ -0,0 +1,127 @@
+/*
+ * vlanconfig.c - Configure 802.1Q vlan-aware Interfaces for busybox.
+ *
+ * Busybox 2002 by Nick Fedchik <nick at fedchik.org.ua>
+ *
+ * This program is free software; you can redistribute it and/or modify
+ * it under the terms of the GNU General Public License as published by
+ * the Free Software Foundation; either version 2 of the License, or
+ * (at your option) any later version.
+ *
+ * This program is distributed in the hope that it will be useful,
+ * but WITHOUT ANY WARRANTY; without even the implied warranty of
+ * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
+ * General Public License for more details.
+ *
+ * You should have received a copy of the GNU General Public License
+ * along with this program; if not, write to the Free Software
+ * Foundation, Inc., 59 Temple Place, Suite 330, Boston, MA
+ * 02111-1307 USA
+ *
+ */
+
+#include <stdlib.h>
+#include <unistd.h>
+#include <fcntl.h>
+#include <sys/ioctl.h>
+#include <net/if.h>
+#include <linux/if_vlan.h>
+#include <string.h>
+#include "busybox.h"
+
+#define VLAN_GROUP_ARRAY_LEN 4096
+#define SIOCSIFVLAN	0x8983		/* Set 802.1Q VLAN options 	*/
+
+int vlanconfig_main(int argc, char **argv)
+{
+	int fd;
+	struct vlan_ioctl_args ifr;
+	char *cmd = NULL;
+	char **endptr = NULL;
+	char *conf_file_name = "/proc/net/vlan/config";
+	char *int_len_msg = "Interface name must be less than";
+
+	if ((argc < 3) || (argc > 6))
+		show_usage();
+
+/*
+Usage: add      <iface-name> <vid>
+       del      <vlan-name>
+       set	nametype  {1|2|3|4}
+       set	flag	<vlan-name>
+       set	egress  <vlan-name>	<skb_priority>   <vlan_qos> 0-7
+       set	ingress <vlan-name>	<skb_priority>   <vlan_qos>
+
+*/
+
+	// Open up the /proc/vlan/config
+	if ((fd = open(conf_file_name, O_RDONLY)) < 0)
+		error_msg_and_die("Could not open %s. Try to load the 8021q module\n",
+						  conf_file_name);
+	else
+		close(fd);
+	/* We use sockets now, instead of the file descriptor */
+	if ((fd = socket(AF_INET, SOCK_STREAM, 0)) < 0)
+		error_msg_and_die("Could not open a socket %m\n");
+	memset(&ifr, 0, sizeof(struct vlan_ioctl_args));
+	ifr.u.name_type = VLAN_NAME_TYPE_PLUS_VID;
+	cmd = argv[1];		// set|add|del
+	if (!strcasecmp(cmd, "set")) {
+		cmd = argv[2];	// flag|egress|ingress|nametype
+// set nametype
+		if (!strcasecmp(cmd, "nametype")) {
+			/* Check name type range */
+			ifr.u.name_type = strtoul(argv[3], endptr, 10);
+			if ((ifr.u.name_type > VLAN_NAME_TYPE_HIGHEST) || (!(*endptr)))
+				show_usage();
+			ifr.cmd = SET_VLAN_NAME_TYPE_CMD;
+		} else {		// flag|egress|ingress
+// here argv[3] probably should be vlan-aware interface name,  check interface len
+			if (argc < 4)
+				show_usage();
+// For those cases argv[3] is interface (physical or vlan-aware)
+			if (strlen(argv[3]) > IF_NAMESIZE)
+				error_msg_and_die("%s %d\n", int_len_msg, IF_NAMESIZE);
+			strcpy(ifr.device1, argv[3]);
+			if (!strcasecmp(cmd, "flag")) {
+				ifr.cmd = SET_VLAN_FLAG_CMD;
+			} else {	//egress|ingress
+				if (argc < 6)
+					show_usage();
+				ifr.u.skb_priority = strtoul(argv[4], endptr, 10);
+				if (!(*endptr))
+					show_usage();
+				ifr.vlan_qos = strtoul(argv[5], endptr, 8);
+				if ((ifr.vlan_qos > 7) || (!(*endptr)))
+					show_usage();
+				if (!strcasecmp(cmd, "egress"))
+					ifr.cmd = SET_VLAN_EGRESS_PRIORITY_CMD;
+				if (!strcasecmp(cmd, "ingress"))
+					ifr.cmd = SET_VLAN_INGRESS_PRIORITY_CMD;
+				if (!ifr.cmd)
+					error_msg_and_die("Unknown set parameter %s\n", cmd);
+			}			//egress|ingress
+			if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
+				error_msg_and_die("ioctl error when set %s: %m\n", cmd);
+		}				// not a nametype
+	} else {			// add|del
+// For those cases argv[3] is interface (physical or vlan-aware)
+		if (strlen(argv[2]) > IF_NAMESIZE)
+			error_msg_and_die("%s %d\n", int_len_msg, IF_NAMESIZE);
+		strcpy(ifr.device1, argv[2]);
+		if (!strcasecmp(cmd, "del")) {
+			ifr.cmd = DEL_VLAN_CMD;
+		} else if (!strcasecmp(cmd, "add")) {
+			ifr.cmd = ADD_VLAN_CMD;
+			ifr.u.VID = strtoul(argv[3], endptr, 10);
+			if ((ifr.u.VID > VLAN_GROUP_ARRAY_LEN) || (!(*endptr)))
+				show_usage();
+		} else
+			error_msg_and_die("Unknown command %s\n", cmd);
+		if (ioctl(fd, SIOCSIFVLAN, &ifr) < 0)
+			error_msg_and_die("ioctl error when %s on %s: %m\n", cmd,
+							  argv[2]);
+	}
+
+	return 0;
+}						/* main */
diff -urN busybox/include/applets.h busybox-dirty/include/applets.h
--- busybox/include/applets.h	2003-01-23 07:59:31 +0200
+++ busybox-dirty/include/applets.h	2003-01-25 17:17:50 +0200
@@ -604,6 +604,9 @@
 #ifdef CONFIG_VI
 	APPLET(vi, vi_main, _BB_DIR_BIN, _BB_SUID_NEVER)
 #endif
+#ifdef CONFIG_VLANCONFIG
+	APPLET(vlanconfig, vlanconfig_main, _BB_DIR_SBIN, _BB_SUID_NEVER)
+#endif
 #ifdef CONFIG_VLOCK
 	APPLET(vlock, vlock_main, _BB_DIR_USR_BIN, _BB_SUID_ALWAYS)
 #endif
diff -urN busybox/include/usage.h busybox-dirty/include/usage.h
--- busybox/include/usage.h	2003-01-23 07:59:31 +0200
+++ busybox-dirty/include/usage.h	2003-01-25 18:05:47 +0200
@@ -2387,6 +2387,27 @@
 	"Options:\n" \
 	"\t-R\tRead-only- do not write to the file." 
 
+#define vlanconfig_trivial_usage \
+	"COMMAND [OPTIONS]"
+#define vlanconfig_full_usage \
+	"Add, delete or configure vlan-aware interface\n" \
+	"Options:\n" \
+	"\tadd\t<if-name>\t<1-4096>\n" \
+	"\t\tAdd new vlan-aware interface with specified vlan ID\n" \
+	"\tdel\t<if_vlan-name>\n" \
+	"\t\tDelete vlan-aware interface\n" \
+	"\tset\tnametype\t<0-3> Set vlan-aware interface type\n" \
+	"\t\t\t0 - vlan plus vlan-id (vlan0005)\n" \
+	"\t\t\t1 - vlan plus vlan-id no pad (vlan5)\n" \
+	"\t\t\t2 - device plus vlan-id (eth0.0005)\n" \
+	"\t\t\t3 - device plus vlan-id no pad (eth0.5)\n" \
+	"\tset\tflag\t<if_vlan-name>\t<0|1>\n" \
+	"\t\tSet flag to make vlan-aware interface look like real ethernet device\n" \
+	"\tset\tegress\t<if_vlan-name>\t<skb_priority>\t<0-7>\n" \
+	"\t\tSet egress mapping in vlan-aware interface\n" \
+	"\tset\tingress\t<if_vlan-name>\t<skb_priority>\t<0-7>\n" \
+	"\t\tSet ingress mapping in vlan-aware interface\n"
+
 #define vlock_trivial_usage \
 	"[OPTIONS]"
 #define vlock_full_usage \


More information about the busybox mailing list