Skip to content

Commit 205f356

Browse files
Jiri Bencdavem330
authored andcommitted
vxlan: make vxlan_sock_add and vxlan_sock_release complementary
Make vxlan_sock_add both alloc the socket and attach it to vxlan_dev. Let vxlan_sock_release accept vxlan_dev as its parameter instead of vxlan_sock. This makes vxlan_sock_add and vxlan_sock release complementary. It reduces code duplication in the next patch. Signed-off-by: Jiri Benc <jbenc@redhat.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent 8b7a704 commit 205f356

File tree

1 file changed

+25
-28
lines changed

1 file changed

+25
-28
lines changed

drivers/net/vxlan.c

Lines changed: 25 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -75,8 +75,7 @@ static struct rtnl_link_ops vxlan_link_ops;
7575

7676
static const u8 all_zeros_mac[ETH_ALEN];
7777

78-
static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
79-
bool no_share, u32 flags);
78+
static int vxlan_sock_add(struct vxlan_dev *vxlan);
8079

8180
/* per-network namespace private data for this module */
8281
struct vxlan_net {
@@ -1022,8 +1021,9 @@ static bool vxlan_group_used(struct vxlan_net *vn, struct vxlan_dev *dev)
10221021
return false;
10231022
}
10241023

1025-
static void vxlan_sock_release(struct vxlan_sock *vs)
1024+
static void vxlan_sock_release(struct vxlan_dev *vxlan)
10261025
{
1026+
struct vxlan_sock *vs = vxlan->vn_sock;
10271027
struct sock *sk = vs->sock->sk;
10281028
struct net *net = sock_net(sk);
10291029
struct vxlan_net *vn = net_generic(net, vxlan_net_id);
@@ -2244,22 +2244,18 @@ static void vxlan_uninit(struct net_device *dev)
22442244
static int vxlan_open(struct net_device *dev)
22452245
{
22462246
struct vxlan_dev *vxlan = netdev_priv(dev);
2247-
struct vxlan_sock *vs;
2248-
int ret = 0;
2249-
2250-
vs = vxlan_sock_add(vxlan->net, vxlan->cfg.dst_port,
2251-
vxlan->cfg.no_share, vxlan->flags);
2252-
if (IS_ERR(vs))
2253-
return PTR_ERR(vs);
2247+
int ret;
22542248

2255-
vxlan_vs_add_dev(vs, vxlan);
2249+
ret = vxlan_sock_add(vxlan);
2250+
if (ret < 0)
2251+
return ret;
22562252

22572253
if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip)) {
22582254
ret = vxlan_igmp_join(vxlan);
22592255
if (ret == -EADDRINUSE)
22602256
ret = 0;
22612257
if (ret) {
2262-
vxlan_sock_release(vs);
2258+
vxlan_sock_release(vxlan);
22632259
return ret;
22642260
}
22652261
}
@@ -2294,7 +2290,6 @@ static int vxlan_stop(struct net_device *dev)
22942290
{
22952291
struct vxlan_dev *vxlan = netdev_priv(dev);
22962292
struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2297-
struct vxlan_sock *vs = vxlan->vn_sock;
22982293
int ret = 0;
22992294

23002295
if (vxlan_addr_multicast(&vxlan->default_dst.remote_ip) &&
@@ -2304,7 +2299,7 @@ static int vxlan_stop(struct net_device *dev)
23042299
del_timer_sync(&vxlan->age_timer);
23052300

23062301
vxlan_flush(vxlan);
2307-
vxlan_sock_release(vs);
2302+
vxlan_sock_release(vxlan);
23082303

23092304
return ret;
23102305
}
@@ -2592,27 +2587,29 @@ static struct vxlan_sock *vxlan_socket_create(struct net *net, __be16 port,
25922587
return vs;
25932588
}
25942589

2595-
static struct vxlan_sock *vxlan_sock_add(struct net *net, __be16 port,
2596-
bool no_share, u32 flags)
2590+
static int vxlan_sock_add(struct vxlan_dev *vxlan)
25972591
{
2598-
struct vxlan_net *vn = net_generic(net, vxlan_net_id);
2599-
struct vxlan_sock *vs;
2600-
bool ipv6 = flags & VXLAN_F_IPV6;
2592+
struct vxlan_net *vn = net_generic(vxlan->net, vxlan_net_id);
2593+
struct vxlan_sock *vs = NULL;
2594+
bool ipv6 = vxlan->flags & VXLAN_F_IPV6;
26012595

2602-
if (!no_share) {
2596+
if (!vxlan->cfg.no_share) {
26032597
spin_lock(&vn->sock_lock);
2604-
vs = vxlan_find_sock(net, ipv6 ? AF_INET6 : AF_INET, port,
2605-
flags);
2606-
if (vs) {
2607-
if (!atomic_add_unless(&vs->refcnt, 1, 0))
2608-
vs = ERR_PTR(-EBUSY);
2598+
vs = vxlan_find_sock(vxlan->net, ipv6 ? AF_INET6 : AF_INET,
2599+
vxlan->cfg.dst_port, vxlan->flags);
2600+
if (vs && !atomic_add_unless(&vs->refcnt, 1, 0)) {
26092601
spin_unlock(&vn->sock_lock);
2610-
return vs;
2602+
return -EBUSY;
26112603
}
26122604
spin_unlock(&vn->sock_lock);
26132605
}
2614-
2615-
return vxlan_socket_create(net, port, flags);
2606+
if (!vs)
2607+
vs = vxlan_socket_create(vxlan->net, vxlan->cfg.dst_port,
2608+
vxlan->flags);
2609+
if (IS_ERR(vs))
2610+
return PTR_ERR(vs);
2611+
vxlan_vs_add_dev(vs, vxlan);
2612+
return 0;
26162613
}
26172614

26182615
static int vxlan_dev_configure(struct net *src_net, struct net_device *dev,

0 commit comments

Comments
 (0)