[rtems-libbsd commit] rc_conf: Add support for ifconfig_<interface>_alias[ 0-9]+.

Chris Johns chrisj at rtems.org
Thu Apr 27 03:59:58 UTC 2017


Module:    rtems-libbsd
Branch:    master
Commit:    443757313e0f2c4da58cf25009e2964026043957
Changeset: http://git.rtems.org/rtems-libbsd/commit/?id=443757313e0f2c4da58cf25009e2964026043957

Author:    Chris Johns <chrisj at rtems.org>
Date:      Thu Apr 27 13:57:28 2017 +1000

rc_conf: Add support for ifconfig_<interface>_alias[0-9]+.

The interface alias allows extra IP addresses to be set on and
interface and it also allows the MAC address to be set.

Examples:
  ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
  ifconfig_em0_alias0="ether 10:22:33:44:55:66"
  ifconfig_em0_alias1="inet 10.1.1.111 netmask 0xffffffff"

---

 rtemsbsd/rtems/rtems-bsd-rc-conf-net.c | 81 ++++++++++++++++++++++++++++------
 testsuite/rcconf01/test_main.c         | 45 ++++++++++++++++++-
 testsuite/rcconf02/test_main.c         | 17 ++++++-
 3 files changed, 128 insertions(+), 15 deletions(-)

diff --git a/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
index 5176838..b7bac8c 100644
--- a/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
+++ b/rtemsbsd/rtems/rtems-bsd-rc-conf-net.c
@@ -221,9 +221,22 @@ load_create_args(rtems_bsd_rc_conf* rc_conf, rtems_bsd_rc_conf_argc_argv* aa)
 }
 
 /*
+ * ifconfig_show
+
+ */
+static int
+ifconfig_show(const char* ifname)
+{
+  const char const* ifconfig_show[] = { "ifconfig", ifname, NULL };
+  return rtems_bsd_command_ifconfig(2, (char**) ifconfig_show);
+}
+
+/*
  * ifconfig_'interface'
  *
  * eg ifconfig_em0="inet 10.10.5.33 netmask 255.255.255.0"
+ *    ifconfig_em0_alias0="ether 10:22:33:44:55:66"
+ *    ifconfig_em0_alias1="inet 10.1.1.111 netmask 0xffffffff"
  *
  * See 'man rc.conf(5)' on FreeBSD.
  */
@@ -231,21 +244,22 @@ static int
 ifconfig_(rtems_bsd_rc_conf* rc_conf,
           const char*        ifname,
           int                argc,
-          const char**       argv)
+          const char**       argv,
+          int                opt_argc,
+          const char**       opt_argv,
+          bool               add_up)
 {
   const char**      args;
   int               arg;
   int               ifconfig_argc = 0;
-  bool              add_up = true;
   int               r;
-  const char const* ifconfig_show[] = { "ifconfig", ifname, NULL };
 
   for (arg = 1; arg < argc; ++arg) {
     if (strcasecmp(argv[arg], "NOAUTO") == 0)
       return 0;
   }
 
-  args = calloc(argc + 3, sizeof(char*));
+  args = calloc(argc + opt_argc + 3, sizeof(char*));
   if (args == NULL) {
     errno = ENOMEM;
     return -1;
@@ -256,7 +270,8 @@ ifconfig_(rtems_bsd_rc_conf* rc_conf,
 
   for (arg = 1; arg < argc; ++arg) {
     if (strcasecmp("DHCP",     argv[arg]) == 0 ||
-        strcasecmp("SYNCDHCP", argv[arg]) == 0) {
+        strcasecmp("SYNCDHCP", argv[arg]) == 0 ||
+        strcasecmp("UP",       argv[arg]) == 0) {
       add_up = false;
     }
     else {
@@ -264,6 +279,12 @@ ifconfig_(rtems_bsd_rc_conf* rc_conf,
     }
   }
 
+  if (opt_argv != NULL) {
+    for (arg = 0; arg < opt_argc; ++arg) {
+      args[ifconfig_argc++] = opt_argv[arg];
+    }
+  }
+
   if (add_up)
     args[ifconfig_argc++] = "up";
 
@@ -278,8 +299,6 @@ ifconfig_(rtems_bsd_rc_conf* rc_conf,
     return -1;
   }
 
-  r = rtems_bsd_command_ifconfig(2, (char**) ifconfig_show);
-
   return r;
 }
 
@@ -353,7 +372,7 @@ defaultrouter(rtems_bsd_rc_conf* rc_conf, rtems_bsd_rc_conf_argc_argv* aa)
 }
 
 static int
-show_interfaces(const char* msg, struct ifaddrs* ifap)
+list_interfaces(const char* msg, struct ifaddrs* ifap)
 {
   struct ifaddrs* ifa;
 
@@ -382,6 +401,18 @@ show_interfaces(const char* msg, struct ifaddrs* ifap)
 }
 
 static int
+show_interfaces(struct ifaddrs* ifap)
+{
+  struct ifaddrs* ifa;
+
+  for (ifa = ifap; ifa; ifa = ifa->ifa_next) {
+    ifconfig_show(ifa->ifa_name);
+  }
+
+  return 0;
+}
+
+static int
 dhcp_check(rtems_bsd_rc_conf_argc_argv* aa)
 {
   int arg;
@@ -403,7 +434,11 @@ setup_lo0(rtems_bsd_rc_conf* rc_conf, struct ifaddrs* ifap)
       const char* lo0_argv[] = {
         "ifconfig_lo0", "inet", "127.0.0.1", "netmask", "255.0.0.0", NULL
       };
-      show_result("lo0", ifconfig_(rc_conf, "lo0", 5, lo0_argv));
+      show_result("lo0",
+                  ifconfig_(rc_conf, "lo0",
+                            5, lo0_argv,
+                            0, NULL,
+                            true));
       return 0;
     }
   }
@@ -435,7 +470,23 @@ setup_interfaces(rtems_bsd_rc_conf*           rc_conf,
          * A DHCP ifconfig can have other options we need to set on the
          * interface.
          */
-        show_result(iface, ifconfig_(rc_conf, ifa->ifa_name, aa->argc, aa->argv));
+        show_result(iface, ifconfig_(rc_conf, ifa->ifa_name,
+                                     aa->argc, aa->argv,
+                                     0, NULL,
+                                     true));
+      }
+      snprintf(iface, sizeof(iface), "ifconfig_%s_alias[0-9]+", ifa->ifa_name);
+      if (r == 0) {
+        r = rtems_bsd_rc_conf_find(rc_conf, iface, aa);
+        while (r == 0) {
+          const char* alias_argv[] = { "alias", NULL };
+          show_result(iface,
+                      ifconfig_(rc_conf, ifa->ifa_name,
+                                aa->argc, aa->argv,
+                                1, alias_argv,
+                                false));
+          r = rtems_bsd_rc_conf_find_next(rc_conf, aa);
+        }
       }
     }
   }
@@ -493,8 +544,11 @@ setup_vlans(rtems_bsd_rc_conf*           rc_conf,
                 *dhcp = true;
               }
               else {
-                show_result(vlan_name, ifconfig_(rc_conf, vlan_name,
-                                                 vaa->argc, vaa->argv));
+                show_result(vlan_name,
+                            ifconfig_(rc_conf, vlan_name,
+                                      vaa->argc, vaa->argv,
+                                      0, NULL,
+                                      true));
               }
             }
           }
@@ -699,12 +753,13 @@ interfaces(rtems_bsd_rc_conf* rc_conf, rtems_bsd_rc_conf_argc_argv* aa)
     return -1;
   }
 
-  show_interfaces("Starting network: ", ifap);
+  list_interfaces("Starting network: ", ifap);
   show_result("cloned_interfaces", cloned_interfaces(rc_conf, aa));
   show_result("lo0", setup_lo0(rc_conf, ifap));
   show_result("ifaces", setup_interfaces(rc_conf, aa, ifap, &dhcp));
   show_result("vlans", setup_vlans(rc_conf, aa, ifap, &dhcp));
   show_result("defaultrouter", defaultrouter(rc_conf, aa));
+  show_interfaces(ifap);
   if (dhcp)
     show_result("dhcp", run_dhcp(rc_conf, aa));
 
diff --git a/testsuite/rcconf01/test_main.c b/testsuite/rcconf01/test_main.c
index be878a3..51723bc 100644
--- a/testsuite/rcconf01/test_main.c
+++ b/testsuite/rcconf01/test_main.c
@@ -69,6 +69,12 @@ test_service(rtems_bsd_rc_conf* rc_conf)
 
   test_service_last_num = 1;
 
+  /*
+   * Fill match of anything.
+   */
+  puts("test_service regex: 'test_regex_.*'");
+  test_regex_last_num = 0;
+
   assert((aa = rtems_bsd_rc_conf_argc_argv_create()) != NULL);
   r = rtems_bsd_rc_conf_find(rc_conf, "test_regex_.*", aa);
   assert(r == 0 || (r < 0 && errno == ENOENT));
@@ -78,7 +84,7 @@ test_service(rtems_bsd_rc_conf* rc_conf)
   while (r == 0) {
     int num;
     int arg;
-    rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service", aa->argc, aa->argv);
+    rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service[1]", aa->argc, aa->argv);
     assert(strncasecmp(aa->argv[0], "test_regex_", strlen("test_regex_")) == 0);
     num = atoi(aa->argv[0] + strlen("test_regex_"));
     assert(num == (test_regex_last_num + 1));
@@ -98,6 +104,43 @@ test_service(rtems_bsd_rc_conf* rc_conf)
     r = rtems_bsd_rc_conf_find_next(rc_conf, aa);
     assert(r == 0 || (r < 0 && errno == ENOENT));
   }
+
+  /*
+   * Specific match of only numbers.
+   */
+  puts("test_service regex: 'test_regex_[0-9]+'");
+  test_regex_last_num = 0;
+
+  assert((aa = rtems_bsd_rc_conf_argc_argv_create()) != NULL);
+  r = rtems_bsd_rc_conf_find(rc_conf, "test_regex_[0-9]+", aa);
+  assert(r == 0 || (r < 0 && errno == ENOENT));
+  if (r < 0 && errno == ENOENT)
+    return -1;
+
+  while (r == 0) {
+    int num;
+    int arg;
+    rtems_bsd_rc_conf_print_cmd(rc_conf, "test_service[2]", aa->argc, aa->argv);
+    assert(strncasecmp(aa->argv[0], "test_regex_", strlen("test_regex_")) == 0);
+    num = atoi(aa->argv[0] + strlen("test_regex_"));
+    assert(num == (test_regex_last_num + 1));
+    assert((num - 1) < NUM_OF_TEST_REGEX_);
+    for (arg = 0; arg < aa->argc; ++arg) {
+      const char* a = aa->argv[arg];
+      size_t      l = strlen(a);
+      if (l > 0) {
+        assert(!isspace(a[0]));
+        assert(!isspace(a[l - 1]));
+        assert(a[0] != '"');
+        assert(a[l - 1] != '"');
+      }
+    }
+    test_regex_results[num - 1] = true;
+    ++test_regex_last_num;
+    r = rtems_bsd_rc_conf_find_next(rc_conf, aa);
+    assert(r == 0 || (r < 0 && errno == ENOENT));
+  }
+
   rtems_bsd_rc_conf_argc_argv_destroy(aa);
   puts("test_service done");
   return 0;
diff --git a/testsuite/rcconf02/test_main.c b/testsuite/rcconf02/test_main.c
index 0b7c6d1..ad177d2 100644
--- a/testsuite/rcconf02/test_main.c
+++ b/testsuite/rcconf02/test_main.c
@@ -75,8 +75,11 @@
 #define IFACE_IPV4(iface) \
   "ifconfig_" # iface "=\"inet " NET_CFG_SELF_IP " netmask " NET_CFG_NETMASK "\"\n"
 
+#define IFACE_ALIAS(iface) \
+  "ifconfig_" # iface "_alias0=\"ether 10:22:33:44:55:66\"\n" \
+  "ifconfig_" # iface "_alias1=\"inet 10.1.1.111 netmask 0xffffffff\"\n"
 
-#define RC_CONF_IFACES \
+#define RC_CONF_IFACES_IPV4 \
   IFACE_IPV4(dmc0)  \
   IFACE_IPV4(sm0)   \
   IFACE_IPV4(cgem0) \
@@ -84,6 +87,18 @@
   IFACE_IPV4(em0)   \
   IFACE_IPV4(re0)
 
+#define RC_CONF_IFACES_ALIAS \
+  IFACE_ALIAS(dmc0)  \
+  IFACE_ALIAS(sm0)   \
+  IFACE_ALIAS(cgem0) \
+  IFACE_ALIAS(fec0)  \
+  IFACE_ALIAS(em0)   \
+  IFACE_ALIAS(re0)
+
+#define RC_CONF_IFACES \
+  RC_CONF_IFACES_IPV4 \
+  RC_CONF_IFACES_ALIAS
+
 #define IFACE_VLAN(iface) \
   "vlans_" # iface "=\"101 102\"\n" \
   "ifconfig_" # iface "_101=\"inet 192.0.101.1/24\"\n" \




More information about the vc mailing list