Skip to content

Commit 8826cde

Browse files
Jon Paul Maloydavem330
authored andcommitted
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>
1 parent f9fef18 commit 8826cde

4 files changed

Lines changed: 101 additions & 54 deletions

File tree

‎net/tipc/port.c‎

Lines changed: 4 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* net/tipc/port.c: TIPC port code
33
*
4-
* Copyright (c) 1992-2007, Ericsson AB
4+
* Copyright (c) 1992-2007, 2014, Ericsson AB
55
* Copyright (c) 2004-2008, 2010-2013, Wind River Systems
66
* All rights reserved.
77
*
@@ -38,6 +38,7 @@
3838
#include "config.h"
3939
#include "port.h"
4040
#include "name_table.h"
41+
#include "socket.h"
4142

4243
/* Connection management: */
4344
#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
@@ -200,23 +201,16 @@ struct tipc_port *tipc_createport(struct sock *sk,
200201
void (*wakeup)(struct tipc_port *),
201202
const u32 importance)
202203
{
203-
struct tipc_port *p_ptr;
204+
struct tipc_port *p_ptr = tipc_sk_port(sk);
204205
struct tipc_msg *msg;
205206
u32 ref;
206207

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-
}
212208
ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
213209
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");
216211
return NULL;
217212
}
218213

219-
p_ptr->sk = sk;
220214
p_ptr->max_pkt = MAX_PKT_DEFAULT;
221215
p_ptr->ref = ref;
222216
INIT_LIST_HEAD(&p_ptr->wait_list);
@@ -262,7 +256,6 @@ int tipc_deleteport(struct tipc_port *p_ptr)
262256
list_del(&p_ptr->wait_list);
263257
spin_unlock_bh(&tipc_port_list_lock);
264258
k_term_timer(&p_ptr->timer);
265-
kfree(p_ptr);
266259
tipc_net_route_msg(buf);
267260
return 0;
268261
}

‎net/tipc/port.h‎

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* net/tipc/port.h: Include file for TIPC port code
33
*
4-
* Copyright (c) 1994-2007, Ericsson AB
4+
* Copyright (c) 1994-2007, 2014, Ericsson AB
55
* Copyright (c) 2004-2007, 2010-2013, Wind River Systems
66
* All rights reserved.
77
*
@@ -48,7 +48,6 @@
4848

4949
/**
5050
* struct tipc_port - TIPC port structure
51-
* @sk: pointer to socket handle
5251
* @lock: pointer to spinlock for controlling access to port
5352
* @connected: non-zero if port is currently connected to a peer port
5453
* @conn_type: TIPC type used when connection was established
@@ -74,7 +73,6 @@
7473
* @subscription: "node down" subscription used to terminate failed connections
7574
*/
7675
struct tipc_port {
77-
struct sock *sk;
7876
spinlock_t *lock;
7977
int connected;
8078
u32 conn_type;

‎net/tipc/socket.c‎

Lines changed: 26 additions & 40 deletions
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
/*
22
* net/tipc/socket.c: TIPC socket API
33
*
4-
* Copyright (c) 2001-2007, 2012 Ericsson AB
4+
* Copyright (c) 2001-2007, 2012-2014, Ericsson AB
55
* Copyright (c) 2004-2008, 2010-2013, Wind River Systems
66
* All rights reserved.
77
*
@@ -38,21 +38,12 @@
3838
#include "port.h"
3939

4040
#include <linux/export.h>
41-
#include <net/sock.h>
4241

4342
#define SS_LISTENING -1 /* socket is listening */
4443
#define SS_READY -2 /* socket is connectionless */
4544

4645
#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
4746

48-
struct tipc_sock {
49-
struct sock sk;
50-
struct tipc_port *p;
51-
unsigned int conn_timeout;
52-
};
53-
54-
#define tipc_sk(sk) ((struct tipc_sock *)(sk))
55-
#define tipc_sk_port(sk) (tipc_sk(sk)->p)
5647

5748
static int backlog_rcv(struct sock *sk, struct sk_buff *skb);
5849
static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf);
@@ -114,6 +105,8 @@ static struct proto tipc_proto_kern;
114105
* - port reference
115106
*/
116107

108+
#include "socket.h"
109+
117110
/**
118111
* advance_rx_queue - discard first buffer in socket receive queue
119112
*
@@ -205,7 +198,6 @@ static int tipc_sk_create(struct net *net, struct socket *sock, int protocol,
205198
sk->sk_rcvbuf = sysctl_tipc_rmem[1];
206199
sk->sk_data_ready = tipc_data_ready;
207200
sk->sk_write_space = tipc_write_space;
208-
tipc_sk(sk)->p = tp_ptr;
209201
tipc_sk(sk)->conn_timeout = CONN_TIMEOUT_DEFAULT;
210202

211203
spin_unlock_bh(tp_ptr->lock);
@@ -437,18 +429,17 @@ static int tipc_getname(struct socket *sock, struct sockaddr *uaddr,
437429
int *uaddr_len, int peer)
438430
{
439431
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);
441433

442434
memset(addr, 0, sizeof(*addr));
443435
if (peer) {
444436
if ((sock->state != SS_CONNECTED) &&
445437
((peer != 2) || (sock->state != SS_DISCONNECTING)))
446438
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);
450441
} else {
451-
addr->addr.id.ref = tsock->p->ref;
442+
addr->addr.id.ref = port->ref;
452443
addr->addr.id.node = tipc_own_addr;
453444
}
454445

@@ -879,14 +870,13 @@ static int tipc_send_stream(struct kiocb *iocb, struct socket *sock,
879870
*/
880871
static int auto_connect(struct socket *sock, struct tipc_msg *msg)
881872
{
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);
884874
struct tipc_portid peer;
885875

886876
peer.ref = msg_origport(msg);
887877
peer.node = msg_orignode(msg);
888878

889-
p_ptr = tipc_port_deref(tsock->p->ref);
879+
p_ptr = tipc_port_deref(p_ptr->ref);
890880
if (!p_ptr)
891881
return -EINVAL;
892882

@@ -1270,17 +1260,18 @@ static void tipc_data_ready(struct sock *sk, int len)
12701260

12711261
/**
12721262
* filter_connect - Handle all incoming messages for a connection-based socket
1273-
* @tsock: TIPC socket
1263+
* @port: TIPC port
12741264
* @msg: message
12751265
*
12761266
* Returns TIPC error status code and socket error status code
12771267
* once it encounters some errors
12781268
*/
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)
12801270
{
1281-
struct socket *sock = tsock->sk.sk_socket;
1271+
struct sock *sk = tipc_port_to_sk(port);
1272+
struct socket *sock = sk->sk_socket;
12821273
struct tipc_msg *msg = buf_msg(*buf);
1283-
struct sock *sk = &tsock->sk;
1274+
12841275
u32 retval = TIPC_ERR_NO_PORT;
12851276
int res;
12861277

@@ -1290,10 +1281,10 @@ static u32 filter_connect(struct tipc_sock *tsock, struct sk_buff **buf)
12901281
switch ((int)sock->state) {
12911282
case SS_CONNECTED:
12921283
/* 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)) {
12941285
if (unlikely(msg_errcode(msg))) {
12951286
sock->state = SS_DISCONNECTING;
1296-
__tipc_port_disconnect(tsock->p);
1287+
__tipc_port_disconnect(port);
12971288
}
12981289
retval = TIPC_OK;
12991290
}
@@ -1401,7 +1392,7 @@ static u32 filter_rcv(struct sock *sk, struct sk_buff *buf)
14011392
if (msg_connected(msg))
14021393
return TIPC_ERR_NO_PORT;
14031394
} else {
1404-
res = filter_connect(tipc_sk(sk), &buf);
1395+
res = filter_connect(tipc_sk_port(sk), &buf);
14051396
if (res != TIPC_OK || buf == NULL)
14061397
return res;
14071398
}
@@ -1447,9 +1438,9 @@ static int backlog_rcv(struct sock *sk, struct sk_buff *buf)
14471438
*
14481439
* Returns TIPC error status code (TIPC_OK if message is not to be rejected)
14491440
*/
1450-
static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
1441+
static u32 dispatch(struct tipc_port *port, struct sk_buff *buf)
14511442
{
1452-
struct sock *sk = tport->sk;
1443+
struct sock *sk = tipc_port_to_sk(port);
14531444
u32 res;
14541445

14551446
/*
@@ -1478,10 +1469,9 @@ static u32 dispatch(struct tipc_port *tport, struct sk_buff *buf)
14781469
*
14791470
* Called with port lock already taken.
14801471
*/
1481-
static void wakeupdispatch(struct tipc_port *tport)
1472+
static void wakeupdispatch(struct tipc_port *port)
14821473
{
1483-
struct sock *sk = tport->sk;
1484-
1474+
struct sock *sk = tipc_port_to_sk(port);
14851475
sk->sk_write_space(sk);
14861476
}
14871477

@@ -1661,8 +1651,7 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
16611651
{
16621652
struct sock *new_sk, *sk = sock->sk;
16631653
struct sk_buff *buf;
1664-
struct tipc_sock *new_tsock;
1665-
struct tipc_port *new_tport;
1654+
struct tipc_port *new_port;
16661655
struct tipc_msg *msg;
16671656
struct tipc_portid peer;
16681657
u32 new_ref;
@@ -1675,7 +1664,6 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
16751664
res = -EINVAL;
16761665
goto exit;
16771666
}
1678-
16791667
timeo = sock_rcvtimeo(sk, flags & O_NONBLOCK);
16801668
res = tipc_wait_for_accept(sock, timeo);
16811669
if (res)
@@ -1688,9 +1676,8 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
16881676
goto exit;
16891677

16901678
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;
16941681
msg = buf_msg(buf);
16951682

16961683
/* we lock on new_sk; but lockdep sees the lock on sk */
@@ -1710,8 +1697,8 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
17101697

17111698
tipc_set_portimportance(new_ref, msg_importance(msg));
17121699
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);
17151702
}
17161703

17171704
/*
@@ -1729,7 +1716,6 @@ static int tipc_accept(struct socket *sock, struct socket *new_sock, int flags)
17291716
skb_set_owner_r(buf, new_sk);
17301717
}
17311718
release_sock(new_sk);
1732-
17331719
exit:
17341720
release_sock(sk);
17351721
return res;

‎net/tipc/socket.h‎

Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
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

0 commit comments

Comments
 (0)