Skip to contentSkip to conversation

Module for ip utility to support veth device

PATCH Applied Vitaliy Gusev 2 participants 2 messages Dec 18, 2007 – Dec 21, 2007

Subsystems (2)
lore
Share & tools
lore
#0 lore raw

PATCH 2/2 Module for ip utility to support veth device

module link_veth

---

 link_veth.c |   76 +++++++++++++++++++-----------------------------------------
 veth.h      |    2 -
 2 files changed, 25 insertions(+), 53 deletions(-)
ip/link_veth.c +25 -51
index a4764f2..226b4ef 100644
@@ -10,78 +10,52 @@
  *
  */
 
-#include <stdio.h>
 #include <string.h>
 
 #include "utils.h"
 #include "ip_common.h"
 #include "veth.h"
 
-#define ETH_ALEN	6
+#define	IFNAMSIZ	16
 
 static void usage(void)
 {
-	printf("Usage: ip link add ... type veth "
-			"[peer <peer-name>] [mac <mac>] [peer_mac <mac>]\n");
+	printf("Usage: ip link <options> type veth "
+		"[peer <options>]\nTo get <options> type "
+		"'ip link add help'\n");
 }
 
 static int veth_parse_opt(struct link_util *lu, int argc, char **argv,
 		struct nlmsghdr *hdr)
 {
-	__u8 mac[ETH_ALEN];
+	char *name, *type, *link, *dev;
+	int err, len;
+	struct rtattr * data;
 
-	for (; argc != 0; argv++, argc--) {
-		if (strcmp(*argv, "peer") == 0) {
-			argv++;
-			argc--;
-			if (argc == 0) {
-				usage();
-				return -1;
-			}
-
-			addattr_l(hdr, 1024, VETH_INFO_PEER,
-					*argv, strlen(*argv));
-
-			continue;
-		}
-
-		if (strcmp(*argv, "mac") == 0) {
-			argv++;
-			argc--;
-			if (argc == 0) {
-				usage();
-				return -1;
-			}
-
-			if (hexstring_a2n(*argv, mac, sizeof(mac)) == NULL)
-				return -1;
-
-			addattr_l(hdr, 1024, VETH_INFO_MAC,
-					mac, ETH_ALEN);
-			continue;
-		}
+	if (strcmp(argv[0], "peer") != 0) {
+		usage();
+		return -1;
+	}
 
-		if (strcmp(*argv, "peer_mac") == 0) {
-			argv++;
-			argc--;
-			if (argc == 0) {
-				usage();
-				return -1;
-			}
+	data = NLMSG_TAIL(hdr);
+	addattr_l(hdr, 1024, VETH_INFO_PEER, NULL, 0);
 
-			if (hexstring_a2n(*argv, mac, sizeof(mac)) == NULL)
-				return -1;
+	hdr->nlmsg_len += sizeof(struct ifinfomsg);
 
-			addattr_l(hdr, 1024, VETH_INFO_PEER_MAC,
-					mac, ETH_ALEN);
-			continue;
-		}
+	err = iplink_parse(argc - 1, argv + 1, (struct iplink_req *)hdr,
+			&name, &type, &link, &dev);
+	if (err < 0)
+		return err;
 
-		usage();
-		return -1;
+	if (name) {
+		len = strlen(name) + 1;
+		if (len > IFNAMSIZ)
+			invarg("\"name\" too long\n", *argv);
+		addattr_l(hdr, 1024, IFLA_IFNAME, name, len);
 	}
 
-	return 0;
+	data->rta_len = (void *)NLMSG_TAIL(hdr) - (void *)data;
+	return argc - 1 - err;
 }
 
 struct link_util veth_link_util = {
ip/veth.h +0 -2
index b84a530..aa2e6f9 100644
@@ -3,9 +3,7 @@
 
 enum {
 	VETH_INFO_UNSPEC,
-	VETH_INFO_MAC,
 	VETH_INFO_PEER,
-	VETH_INFO_PEER_MAC,
 
 	__VETH_INFO_MAX
 #define VETH_INFO_MAX	(__VETH_INFO_MAX - 1)
Signature · 2 lines
Thank,
Vitaliy Gusev