netdev-vport: Make netdev_vport_patch_peer() return a malloc()'d string.
[sliver-openvswitch.git] / lib / netdev-vport.c
index 6eee8a7..287edae 100644 (file)
@@ -64,8 +64,7 @@ struct vport_class {
     struct netdev_class netdev_class;
 };
 
-static int netdev_vport_create(const struct netdev_class *, const char *,
-                               struct netdev **);
+static int netdev_vport_construct(struct netdev *);
 static int get_patch_config(const struct netdev *, struct smap *args);
 static int get_tunnel_config(const struct netdev *, struct smap *args);
 static void netdev_vport_poll_notify(struct netdev_vport *);
@@ -73,7 +72,7 @@ static void netdev_vport_poll_notify(struct netdev_vport *);
 static bool
 is_vport_class(const struct netdev_class *class)
 {
-    return class->create == netdev_vport_create;
+    return class->construct == netdev_vport_construct;
 }
 
 static const struct vport_class *
@@ -121,14 +120,12 @@ netdev_vport_class_get_dpif_port(const struct netdev_class *class)
 }
 
 const char *
-netdev_vport_get_dpif_port(const struct netdev *netdev)
+netdev_vport_get_dpif_port(const struct netdev *netdev,
+                           char namebuf[], size_t bufsize)
 {
-    const char *dpif_port;
-
     if (netdev_vport_needs_dst_port(netdev)) {
         const struct netdev_vport *vport = netdev_vport_cast(netdev);
         const char *type = netdev_get_type(netdev);
-        static char dpif_port_combined[IFNAMSIZ];
 
         /*
          * Note: IFNAMSIZ is 16 bytes long. The maximum length of a VXLAN
@@ -136,42 +133,60 @@ netdev_vport_get_dpif_port(const struct netdev *netdev)
          * assert here on the size of strlen(type) in case that changes
          * in the future.
          */
+        BUILD_ASSERT(NETDEV_VPORT_NAME_BUFSIZE >= IFNAMSIZ);
         ovs_assert(strlen(type) + 10 < IFNAMSIZ);
-        snprintf(dpif_port_combined, IFNAMSIZ, "%s_sys_%d", type,
+        snprintf(namebuf, bufsize, "%s_sys_%d", type,
                  ntohs(vport->tnl_cfg.dst_port));
-        return dpif_port_combined;
+        return namebuf;
     } else {
         const struct netdev_class *class = netdev_get_class(netdev);
-        dpif_port = netdev_vport_class_get_dpif_port(class);
+        const char *dpif_port = netdev_vport_class_get_dpif_port(class);
+        return dpif_port ? dpif_port : netdev_get_name(netdev);
     }
+}
 
-    return dpif_port ? dpif_port : netdev_get_name(netdev);
+char *
+netdev_vport_get_dpif_port_strdup(const struct netdev *netdev)
+{
+    char namebuf[NETDEV_VPORT_NAME_BUFSIZE];
+
+    return xstrdup(netdev_vport_get_dpif_port(netdev, namebuf,
+                                              sizeof namebuf));
+}
+
+static struct netdev *
+netdev_vport_alloc(void)
+{
+    struct netdev_vport *netdev = xzalloc(sizeof *netdev);
+    return &netdev->up;
 }
 
 static int
-netdev_vport_create(const struct netdev_class *netdev_class, const char *name,
-                    struct netdev **netdevp)
+netdev_vport_construct(struct netdev *netdev_)
 {
-    struct netdev_vport *dev;
+    struct netdev_vport *netdev = netdev_vport_cast(netdev_);
 
-    dev = xzalloc(sizeof *dev);
-    netdev_init(&dev->up, name, netdev_class);
-    dev->change_seq = 1;
-    eth_addr_random(dev->etheraddr);
+    netdev->change_seq = 1;
+    eth_addr_random(netdev->etheraddr);
 
-    *netdevp = &dev->up;
     route_table_register();
 
     return 0;
 }
 
 static void
-netdev_vport_destroy(struct netdev *netdev_)
+netdev_vport_destruct(struct netdev *netdev_)
 {
     struct netdev_vport *netdev = netdev_vport_cast(netdev_);
 
     route_table_unregister();
     free(netdev->peer);
+}
+
+static void
+netdev_vport_dealloc(struct netdev *netdev_)
+{
+    struct netdev_vport *netdev = netdev_vport_cast(netdev_);
     free(netdev);
 }
 
@@ -196,7 +211,7 @@ netdev_vport_get_etheraddr(const struct netdev *netdev,
 static int
 tunnel_get_status(const struct netdev *netdev, struct smap *smap)
 {
-    static char iface[IFNAMSIZ];
+    char iface[IFNAMSIZ];
     ovs_be32 route;
 
     route = netdev_vport_cast(netdev)->tnl_cfg.ip_dst;
@@ -406,13 +421,17 @@ set_tunnel_config(struct netdev *dev_, const struct smap *args)
     }
 
     if (tnl_cfg.ipsec) {
+        static struct ovs_mutex mutex = OVS_MUTEX_INITIALIZER;
         static pid_t pid = 0;
+
+        ovs_mutex_lock(&mutex);
         if (pid <= 0) {
             char *file_name = xasprintf("%s/%s", ovs_rundir(),
                                         "ovs-monitor-ipsec.pid");
             pid = read_pidfile(file_name);
             free(file_name);
         }
+        ovs_mutex_unlock(&mutex);
 
         if (pid < 0) {
             VLOG_ERR("%s: IPsec requires the ovs-monitor-ipsec daemon",
@@ -534,12 +553,23 @@ get_tunnel_config(const struct netdev *dev, struct smap *args)
 \f
 /* Code specific to patch ports. */
 
-const char *
-netdev_vport_patch_peer(const struct netdev *netdev)
+/* If 'netdev' is a patch port, returns the name of its peer as a malloc()'d
+ * string that the caller must free.
+ *
+ * If 'netdev' is not a patch port, returns NULL. */
+char *
+netdev_vport_patch_peer(const struct netdev *netdev_)
 {
-    return (netdev_vport_is_patch(netdev)
-            ? netdev_vport_cast(netdev)->peer
-            : NULL);
+    char *peer = NULL;
+
+    if (netdev_vport_is_patch(netdev_)) {
+        struct netdev_vport *netdev = netdev_vport_cast(netdev_);
+        if (netdev->peer) {
+            peer = xstrdup(netdev->peer);
+        }
+    }
+
+    return peer;
 }
 
 void
@@ -600,7 +630,7 @@ set_patch_config(struct netdev *dev_, const struct smap *args)
 
     free(dev->peer);
     dev->peer = xstrdup(peer);
-
+    netdev_vport_poll_notify(dev);
     return 0;
 }
 
@@ -618,14 +648,14 @@ get_stats(const struct netdev *netdev, struct netdev_stats *stats)
     netdev_vport_run,                                       \
     netdev_vport_wait,                                      \
                                                             \
-    netdev_vport_create,                                    \
-    netdev_vport_destroy,                                   \
+    netdev_vport_alloc,                                     \
+    netdev_vport_construct,                                 \
+    netdev_vport_destruct,                                  \
+    netdev_vport_dealloc,                                   \
     GET_CONFIG,                                             \
     SET_CONFIG,                                             \
     GET_TUNNEL_CONFIG,                                      \
                                                             \
-    NULL,                       /* rx_open */               \
-                                                            \
     NULL,                       /* send */                  \
     NULL,                       /* send_wait */             \
                                                             \
@@ -665,7 +695,15 @@ get_stats(const struct netdev *netdev, struct netdev_stats *stats)
                                                             \
     netdev_vport_update_flags,                              \
                                                             \
-    netdev_vport_change_seq
+    netdev_vport_change_seq,                                \
+                                                            \
+    NULL,                   /* rx_alloc */                  \
+    NULL,                   /* rx_construct */              \
+    NULL,                   /* rx_destruct */               \
+    NULL,                   /* rx_dealloc */                \
+    NULL,                   /* rx_recv */                   \
+    NULL,                   /* rx_wait */                   \
+    NULL,                   /* rx_drain */
 
 #define TUNNEL_CLASS(NAME, DPIF_PORT)                       \
     { DPIF_PORT,                                            \