diff options
author | Ben Pfaff <blp@nicira.com> | 2013-07-31 14:09:30 -0700 |
---|---|---|
committer | Ben Pfaff <blp@nicira.com> | 2013-08-09 21:23:19 -0700 |
commit | 161b6042d853f54e609415a671aaebe859ee94a2 (patch) | |
tree | debecfcb12690abeee31d14dc4f1eb8610bbb83f | |
parent | 9dc63482bbeae23dd57b0f885a3fd26b44656844 (diff) | |
download | openvswitch-161b6042d853f54e609415a671aaebe859ee94a2.tar.gz |
netdev-vport: Make netdev_vport_patch_peer() return a malloc()'d string.
When threading comes into the picture there arises the possibility of a
race between netdev_vport_patch_peer()'s caller using the returned string
and another caller changing the peer. It is safer to return a copy.
Signed-off-by: Ben Pfaff <blp@nicira.com>
-rw-r--r-- | lib/netdev-vport.c | 21 | ||||
-rw-r--r-- | lib/netdev-vport.h | 2 | ||||
-rw-r--r-- | ofproto/ofproto-dpif.c | 8 |
3 files changed, 22 insertions, 9 deletions
diff --git a/lib/netdev-vport.c b/lib/netdev-vport.c index ac3da6345..287edae32 100644 --- a/lib/netdev-vport.c +++ b/lib/netdev-vport.c @@ -553,12 +553,23 @@ get_tunnel_config(const struct netdev *dev, struct smap *args) /* 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 diff --git a/lib/netdev-vport.h b/lib/netdev-vport.h index 53949666e..dc490970c 100644 --- a/lib/netdev-vport.h +++ b/lib/netdev-vport.h @@ -31,7 +31,7 @@ void netdev_vport_patch_register(void); bool netdev_vport_is_patch(const struct netdev *); -const char *netdev_vport_patch_peer(const struct netdev *netdev); +char *netdev_vport_patch_peer(const struct netdev *netdev); void netdev_vport_inc_rx(const struct netdev *, const struct dpif_flow_stats *); diff --git a/ofproto/ofproto-dpif.c b/ofproto/ofproto-dpif.c index 15826193f..13faad680 100644 --- a/ofproto/ofproto-dpif.c +++ b/ofproto/ofproto-dpif.c @@ -2905,7 +2905,7 @@ ofport_update_peer(struct ofport_dpif *ofport) { const struct ofproto_dpif *ofproto; struct dpif_backer *backer; - const char *peer_name; + char *peer_name; if (!netdev_vport_is_patch(ofport->up.netdev)) { return; @@ -2927,7 +2927,7 @@ ofport_update_peer(struct ofport_dpif *ofport) HMAP_FOR_EACH (ofproto, all_ofproto_dpifs_node, &all_ofproto_dpifs) { struct ofport *peer_ofport; struct ofport_dpif *peer; - const char *peer_peer; + char *peer_peer; if (ofproto->backer != backer) { continue; @@ -2945,9 +2945,11 @@ ofport_update_peer(struct ofport_dpif *ofport) ofport->peer = peer; ofport->peer->peer = ofport; } + free(peer_peer); - return; + break; } + free(peer_name); } static void |