aboutsummaryrefslogtreecommitdiffstats
diff options
context:
space:
mode:
authorAsias He <asias@redhat.com>2013-06-20 05:20:30 -0400
committerDavid S. Miller <davem@davemloft.net>2013-06-24 02:51:48 -0400
commitb3a6dfe8178c5159e54117078134fef806a913ca (patch)
tree0e6317b55b00302124e3b55425ad9edfa2da3d99
parentb33698e267ff17a97ecf3ee621a925bb356e9120 (diff)
VSOCK: Introduce vsock_auto_bind helper
This peace of code is called three times, let's have a helper for it. Signed-off-by: Asias He <asias@redhat.com> Acked-by: Andy King <acking@vmware.com> Signed-off-by: David S. Miller <davem@davemloft.net>
-rw-r--r--net/vmw_vsock/af_vsock.c49
1 files changed, 21 insertions, 28 deletions
diff --git a/net/vmw_vsock/af_vsock.c b/net/vmw_vsock/af_vsock.c
index 3f77f42a3b58..b0b362ad051d 100644
--- a/net/vmw_vsock/af_vsock.c
+++ b/net/vmw_vsock/af_vsock.c
@@ -165,6 +165,18 @@ static struct list_head vsock_bind_table[VSOCK_HASH_SIZE + 1];
165static struct list_head vsock_connected_table[VSOCK_HASH_SIZE]; 165static struct list_head vsock_connected_table[VSOCK_HASH_SIZE];
166static DEFINE_SPINLOCK(vsock_table_lock); 166static DEFINE_SPINLOCK(vsock_table_lock);
167 167
168/* Autobind this socket to the local address if necessary. */
169static int vsock_auto_bind(struct vsock_sock *vsk)
170{
171 struct sock *sk = sk_vsock(vsk);
172 struct sockaddr_vm local_addr;
173
174 if (vsock_addr_bound(&vsk->local_addr))
175 return 0;
176 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
177 return __vsock_bind(sk, &local_addr);
178}
179
168static void vsock_init_tables(void) 180static void vsock_init_tables(void)
169{ 181{
170 int i; 182 int i;
@@ -956,15 +968,10 @@ static int vsock_dgram_sendmsg(struct kiocb *kiocb, struct socket *sock,
956 968
957 lock_sock(sk); 969 lock_sock(sk);
958 970
959 if (!vsock_addr_bound(&vsk->local_addr)) { 971 err = vsock_auto_bind(vsk);
960 struct sockaddr_vm local_addr; 972 if (err)
961 973 goto out;
962 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
963 err = __vsock_bind(sk, &local_addr);
964 if (err != 0)
965 goto out;
966 974
967 }
968 975
969 /* If the provided message contains an address, use that. Otherwise 976 /* If the provided message contains an address, use that. Otherwise
970 * fall back on the socket's remote handle (if it has been connected). 977 * fall back on the socket's remote handle (if it has been connected).
@@ -1038,15 +1045,9 @@ static int vsock_dgram_connect(struct socket *sock,
1038 1045
1039 lock_sock(sk); 1046 lock_sock(sk);
1040 1047
1041 if (!vsock_addr_bound(&vsk->local_addr)) { 1048 err = vsock_auto_bind(vsk);
1042 struct sockaddr_vm local_addr; 1049 if (err)
1043 1050 goto out;
1044 vsock_addr_init(&local_addr, VMADDR_CID_ANY, VMADDR_PORT_ANY);
1045 err = __vsock_bind(sk, &local_addr);
1046 if (err != 0)
1047 goto out;
1048
1049 }
1050 1051
1051 if (!transport->dgram_allow(remote_addr->svm_cid, 1052 if (!transport->dgram_allow(remote_addr->svm_cid,
1052 remote_addr->svm_port)) { 1053 remote_addr->svm_port)) {
@@ -1163,17 +1164,9 @@ static int vsock_stream_connect(struct socket *sock, struct sockaddr *addr,
1163 memcpy(&vsk->remote_addr, remote_addr, 1164 memcpy(&vsk->remote_addr, remote_addr,
1164 sizeof(vsk->remote_addr)); 1165 sizeof(vsk->remote_addr));
1165 1166
1166 /* Autobind this socket to the local address if necessary. */ 1167 err = vsock_auto_bind(vsk);
1167 if (!vsock_addr_bound(&vsk->local_addr)) { 1168 if (err)
1168 struct sockaddr_vm local_addr; 1169 goto out;
1169
1170 vsock_addr_init(&local_addr, VMADDR_CID_ANY,
1171 VMADDR_PORT_ANY);
1172 err = __vsock_bind(sk, &local_addr);
1173 if (err != 0)
1174 goto out;
1175
1176 }
1177 1170
1178 sk->sk_state = SS_CONNECTING; 1171 sk->sk_state = SS_CONNECTING;
1179 1172