Commit 8826cde655fb5ca3b35a112c851c90b3dccbb7b8

Authored by Jon Paul Maloy
Committed by David S. Miller
1 parent f9fef18c6d

tipc: aggregate port structure into socket structure

After the removal of the tipc native API the relation between
a tipc_port and its API types is strictly one-to-one, i.e, the
latter can now only be a socket API. There is therefore no need
to allocate struct tipc_port and struct sock independently.

In this commit, we aggregate struct tipc_port into struct tipc_sock,
hence saving both CPU cycles and structure complexity.

There are no functional changes in this commit, except for the
elimination of the separate allocation/freeing of tipc_port.
All other changes are just adaptatons to the new data structure.

This commit also opens up for further code simplifications and
code volume reduction, something we will do in later commits.

Signed-off-by: Jon Maloy <jon.maloy@ericsson.com>
Reviewed-by: Ying Xue <ying.xue@windriver.com>
Reviewed-by: Erik Hugne <erik.hugne@ericsson.com>
Signed-off-by: David S. Miller <davem@davemloft.net>

Showing 4 changed files with 101 additions and 54 deletions Side-by-side Diff

1 1 /*
2 2 * net/tipc/port.c: TIPC port code
3 3 *
4   - * Copyright (c) 1992-2007, Ericsson AB
  4 + * Copyright (c) 1992-2007, 2014, Ericsson AB
5 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 6 * All rights reserved.
7 7 *
... ... @@ -38,6 +38,7 @@
38 38 #include "config.h"
39 39 #include "port.h"
40 40 #include "name_table.h"
  41 +#include "socket.h"
41 42  
42 43 /* Connection management: */
43 44 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
44 45  
45 46  
46 47  
... ... @@ -200,23 +201,16 @@
200 201 void (*wakeup)(struct tipc_port *),
201 202 const u32 importance)
202 203 {
203   - struct tipc_port *p_ptr;
  204 + struct tipc_port *p_ptr = tipc_sk_port(sk);
204 205 struct tipc_msg *msg;
205 206 u32 ref;
206 207  
207   - p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
208   - if (!p_ptr) {
209   - pr_warn("Port creation failed, no memory\n");
210   - return NULL;
211   - }
212 208 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
213 209 if (!ref) {
214   - pr_warn("Port creation failed, ref. table exhausted\n");
215   - kfree(p_ptr);
  210 + pr_warn("Port registration failed, ref. table exhausted\n");
216 211 return NULL;
217 212 }
218 213  
219   - p_ptr->sk = sk;
220 214 p_ptr->max_pkt = MAX_PKT_DEFAULT;
221 215 p_ptr->ref = ref;
222 216 INIT_LIST_HEAD(&p_ptr->wait_list);
... ... @@ -262,7 +256,6 @@
262 256 list_del(&p_ptr->wait_list);
263 257 spin_unlock_bh(&tipc_port_list_lock);
264 258 k_term_timer(&p_ptr->timer);
265   - kfree(p_ptr);
266 259 tipc_net_route_msg(buf);
267 260 return 0;
268 261 }
1 1 /*
2 2 * net/tipc/port.h: Include file for TIPC port code
3 3 *
4   - * Copyright (c) 1994-2007, Ericsson AB
  4 + * Copyright (c) 1994-2007, 2014, Ericsson AB
5 5 * Copyright (c) 2004-2007, 2010-2013, Wind River Systems
6 6 * All rights reserved.
7 7 *
... ... @@ -48,7 +48,6 @@
48 48  
49 49 /**
50 50 * struct tipc_port - TIPC port structure
51   - * @sk: pointer to socket handle
52 51 * @lock: pointer to spinlock for controlling access to port
53 52 * @connected: non-zero if port is currently connected to a peer port
54 53 * @conn_type: TIPC type used when connection was established
... ... @@ -74,7 +73,6 @@
74 73 * @subscription: "node down" subscription used to terminate failed connections
75 74 */
76 75 struct tipc_port {
77   - struct sock *sk;
78 76 spinlock_t *lock;
79 77 int connected;
80 78 u32 conn_type;
1 1 /*
2 2 * net/tipc/socket.c: TIPC socket API
3 3 *
4   - * Copyright (c) 2001-2007, 2012 Ericsson AB
  4 + * Copyright (c) 2001-2007, 2012-2014, Ericsson AB
5 5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 6 * All rights reserved.
7 7 *
8 8  
9 9  
... ... @@ -38,22 +38,13 @@
38 38 #include "port.h"
39 39  
40 40 #include <linux/export.h>
41   -#include <net/sock.h>
42 41  
43 42 #define SS_LISTENING -1 /* socket is listening */
44 43 #define SS_READY -2 /* socket is connectionless */
45 44  
46 45 #define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
47 46  
48   -struct tipc_sock {
49   - struct sock sk;
50   - struct tipc_port *p;
51   - unsigned int conn_timeout;
52   -};
53 47  
54   -#define tipc_sk(sk) ((struct tipc_sock *)(sk))
55   -#define tipc_sk_port(sk) (tipc_sk(sk)->p)
56   -
57 48 static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
58 49 static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
59 50 static void wakeupdispatch(struct tipc_port *tport);
... ... @@ -114,6 +105,8 @@
114 105 * - port reference
115 106 */
116 107  
  108 +#include "socket.h"
  109 +
117 110 /**
118 111 * advance_rx_queue - discard first buffer in socket receive queue
119 112 *
... ... @@ -205,7 +198,6 @@
205 198 sk->sk_rcvbuf = sysctl_tipc_rmem[1];
206 199 sk->sk_data_ready = tipc_data_ready;
207 200 sk->sk_write_space = tipc_write_space;
208   - tipc_sk(sk)->p = tp_ptr;
209 201 tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
210 202  
211 203 spin_unlock_bh(tp_ptr->lock);
212 204  
213 205  
... ... @@ -437,18 +429,17 @@
437 429 int *uaddr_len, int peer)
438 430 {
439 431 struct sockaddr_tipc *addr = (struct sockaddr_tipc *)uaddr;
440   - struct tipc_sock *tsock = tipc_sk(sock->sk);
  432 + struct tipc_port *port = tipc_sk_port(sock->sk);
441 433  
442 434 memset(addr, 0, sizeof(*addr));
443 435 if (peer) {
444 436 if ((sock->state != SS_CONNECTED) &&
445 437 ((peer != 2) || (sock->state != SS_DISCONNECTING)))
446 438 return -ENOTCONN;
447   -
448   - addr->addr.id.ref = tipc_port_peerport(tsock->p);
449   - addr->addr.id.node = tipc_port_peernode(tsock->p);
  439 + addr->addr.id.ref = tipc_port_peerport(port);
  440 + addr->addr.id.node = tipc_port_peernode(port);
450 441 } else {
451   - addr->addr.id.ref = tsock->p->ref;
  442 + addr->addr.id.ref = port->ref;
452 443 addr->addr.id.node = tipc_own_addr;
453 444 }
454 445  
455 446  
... ... @@ -879,14 +870,13 @@
879 870 */
880 871 static int auto_connect(struct socket *sock, struct tipc_msg *msg)
881 872 {
882   - struct tipc_sock *tsock = tipc_sk(sock->sk);
883   - struct tipc_port *p_ptr;
  873 + struct tipc_port *p_ptr = tipc_sk_port(sock->sk);
884 874 struct tipc_portid peer;
885 875  
886 876 peer.ref = msg_origport(msg);
887 877 peer.node = msg_orignode(msg);
888 878  
889   - p_ptr = tipc_port_deref(tsock->p->ref);
  879 + p_ptr = tipc_port_deref(p_ptr->ref);
890 880 if (!p_ptr)
891 881 return -EINVAL;
892 882  
893 883  
894 884  
895 885  
... ... @@ -1270,17 +1260,18 @@
1270 1260  
1271 1261 /**
1272 1262 * filter_connect - Handle all incoming messages for a connection-based socket
1273   - * @tsock: TIPC socket
  1263 + * @port: TIPC port
1274 1264 * @msg: message
1275 1265 *
1276 1266 * Returns TIPC error status code and socket error status code
1277 1267 * once it encounters some errors
1278 1268 */
1279   -static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
  1269 +static u32 filter_connect(struct tipc_port *port, struct sk_buff **buf)
1280 1270 {
1281   - struct socket *sock = tsock->sk.sk_socket;
  1271 + struct sock *sk = tipc_port_to_sk(port);
  1272 + struct socket *sock = sk->sk_socket;
1282 1273 struct tipc_msg *msg = buf_msg(*buf);
1283   - struct sock *sk = &tsock->sk;
  1274 +
1284 1275 u32 retval = TIPC_ERR_NO_PORT;
1285 1276 int res;
1286 1277  
1287 1278  
... ... @@ -1290,10 +1281,10 @@
1290 1281 switch ((int)sock->state) {
1291 1282 case SS_CONNECTED:
1292 1283 /* Accept only connection-based messages sent by peer */
1293   - if (msg_connected(msg) && tipc_port_peer_msg(tsock->p, msg)) {
  1284 + if (msg_connected(msg) && tipc_port_peer_msg(port, msg)) {
1294 1285 if (unlikely(msg_errcode(msg))) {
1295 1286 sock->state = SS_DISCONNECTING;
1296   - __tipc_port_disconnect(tsock->p);
  1287 + __tipc_port_disconnect(port);
1297 1288 }
1298 1289 retval = TIPC_OK;
1299 1290 }
... ... @@ -1401,7 +1392,7 @@
1401 1392 if (msg_connected(msg))
1402 1393 return TIPC_ERR_NO_PORT;
1403 1394 } else {
1404   - res = filter_connect(tipc_sk(sk), &buf);
  1395 + res = filter_connect(tipc_sk_port(sk), &buf);
1405 1396 if (res != TIPC_OK || buf == NULL)
1406 1397 return res;
1407 1398 }
1408 1399  
... ... @@ -1447,9 +1438,9 @@
1447 1438 *
1448 1439 * Returns TIPC error status code (TIPC_OK if message is not to be rejected)
1449 1440 */
1450   -static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
  1441 +static u32 dispatch(struct tipc_port *port, struct sk_buff *buf)
1451 1442 {
1452   - struct sock *sk = tport->sk;
  1443 + struct sock *sk = tipc_port_to_sk(port);
1453 1444 u32 res;
1454 1445  
1455 1446 /*
1456 1447  
... ... @@ -1478,10 +1469,9 @@
1478 1469 *
1479 1470 * Called with port lock already taken.
1480 1471 */
1481   -static void wakeupdispatch(struct tipc_port *tport)
  1472 +static void wakeupdispatch(struct tipc_port *port)
1482 1473 {
1483   - struct sock *sk = tport->sk;
1484   -
  1474 + struct sock *sk = tipc_port_to_sk(port);
1485 1475 sk->sk_write_space(sk);
1486 1476 }
1487 1477  
... ... @@ -1661,8 +1651,7 @@
1661 1651 {
1662 1652 struct sock *new_sk, *sk = sock->sk;
1663 1653 struct sk_buff *buf;
1664   - struct tipc_sock *new_tsock;
1665   - struct tipc_port *new_tport;
  1654 + struct tipc_port *new_port;
1666 1655 struct tipc_msg *msg;
1667 1656 struct tipc_portid peer;
1668 1657 u32 new_ref;
... ... @@ -1675,7 +1664,6 @@
1675 1664 res = -EINVAL;
1676 1665 goto exit;
1677 1666 }
1678   -
1679 1667 timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
1680 1668 res = tipc_wait_for_accept(sock, timeo);
1681 1669 if (res)
... ... @@ -1688,9 +1676,8 @@
1688 1676 goto exit;
1689 1677  
1690 1678 new_sk = new_sock->sk;
1691   - new_tsock = tipc_sk(new_sk);
1692   - new_tport = new_tsock->p;
1693   - new_ref = new_tport->ref;
  1679 + new_port = tipc_sk_port(new_sk);
  1680 + new_ref = new_port->ref;
1694 1681 msg = buf_msg(buf);
1695 1682  
1696 1683 /* we lock on new_sk; but lockdep sees the lock on sk */
... ... @@ -1710,8 +1697,8 @@
1710 1697  
1711 1698 tipc_set_portimportance(new_ref, msg_importance(msg));
1712 1699 if (msg_named(msg)) {
1713   - new_tport->conn_type = msg_nametype(msg);
1714   - new_tport->conn_instance = msg_nameinst(msg);
  1700 + new_port->conn_type = msg_nametype(msg);
  1701 + new_port->conn_instance = msg_nameinst(msg);
1715 1702 }
1716 1703  
1717 1704 /*
... ... @@ -1729,7 +1716,6 @@
1729 1716 skb_set_owner_r(buf, new_sk);
1730 1717 }
1731 1718 release_sock(new_sk);
1732   -
1733 1719 exit:
1734 1720 release_sock(sk);
1735 1721 return res;
  1 +/* net/tipc/socket.h: Include file for TIPC socket code
  2 + *
  3 + * Copyright (c) 2014, Ericsson AB
  4 + * All rights reserved.
  5 + *
  6 + * Redistribution and use in source and binary forms, with or without
  7 + * modification, are permitted provided that the following conditions are met:
  8 + *
  9 + * 1. Redistributions of source code must retain the above copyright
  10 + * notice, this list of conditions and the following disclaimer.
  11 + * 2. Redistributions in binary form must reproduce the above copyright
  12 + * notice, this list of conditions and the following disclaimer in the
  13 + * documentation and/or other materials provided with the distribution.
  14 + * 3. Neither the names of the copyright holders nor the names of its
  15 + * contributors may be used to endorse or promote products derived from
  16 + * this software without specific prior written permission.
  17 + *
  18 + * Alternatively, this software may be distributed under the terms of the
  19 + * GNU General Public License ("GPL") version 2 as published by the Free
  20 + * Software Foundation.
  21 + *
  22 + * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  23 + * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  24 + * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  25 + * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  26 + * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  27 + * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  28 + * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  29 + * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  30 + * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  31 + * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  32 + * POSSIBILITY OF SUCH DAMAGE.
  33 + */
  34 +
  35 +#ifndef _TIPC_SOCK_H
  36 +#define _TIPC_SOCK_H
  37 +
  38 +#include "port.h"
  39 +#include <net/sock.h>
  40 +
  41 +/**
  42 + * struct tipc_sock - TIPC socket structure
  43 + * @sk: socket - interacts with 'port' and with user via the socket API
  44 + * @port: port - interacts with 'sk' and with the rest of the TIPC stack
  45 + * @peer_name: the peer of the connection, if any
  46 + * @conn_timeout: the time we can wait for an unresponded setup request
  47 + */
  48 +
  49 +struct tipc_sock {
  50 + struct sock sk;
  51 + struct tipc_port port;
  52 + unsigned int conn_timeout;
  53 +};
  54 +
  55 +static inline struct tipc_sock *tipc_sk(const struct sock *sk)
  56 +{
  57 + return container_of(sk, struct tipc_sock, sk);
  58 +}
  59 +
  60 +static inline struct tipc_port *tipc_sk_port(const struct sock *sk)
  61 +{
  62 + return &(tipc_sk(sk)->port);
  63 +}
  64 +
  65 +static inline struct sock *tipc_port_to_sk(const struct tipc_port *port)
  66 +{
  67 + return &(container_of(port, struct tipc_sock, port))->sk;
  68 +}
  69 +
  70 +#endif