On 1/28/25 1:57 AM, Bastien Curutchet (eBPF Foundation) wrote:
+#define NO_IP                          "NO_IP"
  #define PROG_NAME_MAX_LEN     128
+#define NS_NAME_MAX_LEN                32
struct veth_configuration {
        char local_veth[VETH_NAME_MAX_LEN]; /* Interface in main namespace */
        char remote_veth[VETH_NAME_MAX_LEN]; /* Peer interface in dedicated 
namespace*/
-       const char *namespace; /* Namespace for the remote veth */
+       char namespace[NS_NAME_MAX_LEN]; /* Namespace for the remote veth */
        int next_veth; /* Local interface to redirect traffic to */
-       char *remote_addr; /* IP address of the remote veth */
+       char remote_addr[IP_MAX_LEN]; /* IP address of the remote veth */
  };

        {
-               .local_veth = "veth2",
+               .local_veth = "veth2-",
                .remote_veth = "veth22",
                .next_veth = 2,
-               .remote_addr = NULL,
-               .namespace = "ns-veth22"
+               .remote_addr = NO_IP,
+               .namespace = "ns-veth22-"
        },

-static int create_network(void)
+static int create_network(struct veth_configuration *net_config)
  {
-       int i;
+       int i, err;
+
+       memcpy(net_config, default_config, VETH_PAIRS_COUNT * sizeof(struct 
veth_configuration));
/* First create and configure all interfaces */
        for (i = 0; i < VETH_PAIRS_COUNT; i++) {
+               err = append_tid(net_config[i].namespace, 
strlen(net_config[i].namespace));
+               if (!ASSERT_OK(err, "append TID to ns name"))
+                       return -1;
+
+               err = append_tid(net_config[i].local_veth, 
strlen(net_config[i].local_veth));
+               if (!ASSERT_OK(err, "append TID to local veth name"))
+                       return -1;
+
                SYS(fail, "ip netns add %s", net_config[i].namespace);
                SYS(fail, "ip link add %s type veth peer name %s netns %s",
                    net_config[i].local_veth, net_config[i].remote_veth, 
net_config[i].namespace);
                SYS(fail, "ip link set dev %s up", net_config[i].local_veth);
-               if (net_config[i].remote_addr)
+               if (memcmp(net_config[i].remote_addr, NO_IP, 5))

nit. May be "if (net_config[i].remote_addr[0])" instead of defining a new "NO_IP" and then memcmp.

                        SYS(fail, "ip -n %s addr add %s/24 dev %s",   
net_config[i].namespace,
                            net_config[i].remote_addr, 
net_config[i].remote_veth);
                SYS(fail, "ip -n %s link set dev %s up", 
net_config[i].namespace,
@@ -155,7 +169,7 @@ static int create_network(void)
        return -1;
  }


Reply via email to