Skip to content

Commit 1186adf

Browse files
Jon Paul Maloydavem330
authored andcommitted
tipc: simplify message forwarding and rejection in socket layer
Despite recent improvements, the handling of error codes and return values at reception of messages in the socket layer is still confusing. In this commit, we try to make it more comprehensible. First, we separate between the return values coming from the functions called by tipc_sk_rcv(), -those are TIPC specific error codes, and the return values returned by tipc_sk_rcv() itself. Second, we don't use the returned TIPC error code as indication for whether a buffer should be forwarded/rejected or not; instead we use the buffer pointer passed along with filter_msg(). This separation is necessary because we sometimes want to forward messages even when there is no error (i.e., protocol messages and successfully secondary looked up data messages). Reviewed-by: Ying Xue <ying.xue@windriver.com> Signed-off-by: Jon Maloy <jon.maloy@ericsson.com> Signed-off-by: David S. Miller <davem@davemloft.net>
1 parent c589863 commit 1186adf

1 file changed

Lines changed: 58 additions & 62 deletions

File tree

‎net/tipc/socket.c‎

Lines changed: 58 additions & 62 deletions
Original file line numberDiff line numberDiff line change
@@ -818,17 +818,14 @@ void tipc_sk_mcast_rcv(struct net *net, struct sk_buff *buf)
818818
/**
819819
* tipc_sk_proto_rcv - receive a connection mng protocol message
820820
* @tsk: receiving socket
821-
* @dnode: node to send response message to, if any
822-
* @buf: buffer containing protocol message
823-
* Returns 0 (TIPC_OK) if message was consumed, 1 (TIPC_FWD_MSG) if
824-
* (CONN_PROBE_REPLY) message should be forwarded.
821+
* @skb: pointer to message buffer. Set to NULL if buffer is consumed.
825822
*/
826-
static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
827-
struct sk_buff *buf)
823+
static void tipc_sk_proto_rcv(struct tipc_sock *tsk, struct sk_buff **skb)
828824
{
829-
struct tipc_msg *msg = buf_msg(buf);
825+
struct tipc_msg *msg = buf_msg(*skb);
830826
int conn_cong;
831-
827+
u32 dnode;
828+
u32 own_node = tsk_own_node(tsk);
832829
/* Ignore if connection cannot be validated: */
833830
if (!tsk_peer_msg(tsk, msg))
834831
goto exit;
@@ -841,15 +838,15 @@ static int tipc_sk_proto_rcv(struct tipc_sock *tsk, u32 *dnode,
841838
if (conn_cong)
842839
tsk->sk.sk_write_space(&tsk->sk);
843840
} else if (msg_type(msg) == CONN_PROBE) {
844-
if (!tipc_msg_reverse(tsk_own_node(tsk), buf, dnode, TIPC_OK))
845-
return TIPC_OK;
846-
msg_set_type(msg, CONN_PROBE_REPLY);
847-
return TIPC_FWD_MSG;
841+
if (tipc_msg_reverse(own_node, *skb, &dnode, TIPC_OK)) {
842+
msg_set_type(msg, CONN_PROBE_REPLY);
843+
return;
844+
}
848845
}
849846
/* Do nothing if msg_type() == CONN_PROBE_REPLY */
850847
exit:
851-
kfree_skb(buf);
852-
return TIPC_OK;
848+
kfree_skb(*skb);
849+
*skb = NULL;
853850
}
854851

855852
static int tipc_wait_for_sndmsg(struct socket *sock, long *timeo_p)
@@ -1568,16 +1565,16 @@ static void tipc_data_ready(struct sock *sk)
15681565
/**
15691566
* filter_connect - Handle all incoming messages for a connection-based socket
15701567
* @tsk: TIPC socket
1571-
* @msg: message
1568+
* @skb: pointer to message buffer. Set to NULL if buffer is consumed
15721569
*
15731570
* Returns 0 (TIPC_OK) if everything ok, -TIPC_ERR_NO_PORT otherwise
15741571
*/
1575-
static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
1572+
static int filter_connect(struct tipc_sock *tsk, struct sk_buff **skb)
15761573
{
15771574
struct sock *sk = &tsk->sk;
15781575
struct net *net = sock_net(sk);
15791576
struct socket *sock = sk->sk_socket;
1580-
struct tipc_msg *msg = buf_msg(*buf);
1577+
struct tipc_msg *msg = buf_msg(*skb);
15811578
int retval = -TIPC_ERR_NO_PORT;
15821579

15831580
if (msg_mcast(msg))
@@ -1627,8 +1624,8 @@ static int filter_connect(struct tipc_sock *tsk, struct sk_buff **buf)
16271624
* connect() routine if sleeping.
16281625
*/
16291626
if (msg_data_sz(msg) == 0) {
1630-
kfree_skb(*buf);
1631-
*buf = NULL;
1627+
kfree_skb(*skb);
1628+
*skb = NULL;
16321629
if (waitqueue_active(sk_sleep(sk)))
16331630
wake_up_interruptible(sk_sleep(sk));
16341631
}
@@ -1680,32 +1677,33 @@ static unsigned int rcvbuf_limit(struct sock *sk, struct sk_buff *buf)
16801677
/**
16811678
* filter_rcv - validate incoming message
16821679
* @sk: socket
1683-
* @buf: message
1680+
* @skb: pointer to message. Set to NULL if buffer is consumed.
16841681
*
16851682
* Enqueues message on receive queue if acceptable; optionally handles
16861683
* disconnect indication for a connected socket.
16871684
*
1688-
* Called with socket lock already taken; port lock may also be taken.
1685+
* Called with socket lock already taken
16891686
*
1690-
* Returns 0 (TIPC_OK) if message was consumed, -TIPC error code if message
1691-
* to be rejected, 1 (TIPC_FWD_MSG) if (CONN_MANAGER) message to be forwarded
1687+
* Returns 0 (TIPC_OK) if message was ok, -TIPC error code if rejected
16921688
*/
1693-
static int filter_rcv(struct sock *sk, struct sk_buff *buf)
1689+
static int filter_rcv(struct sock *sk, struct sk_buff **skb)
16941690
{
16951691
struct socket *sock = sk->sk_socket;
16961692
struct tipc_sock *tsk = tipc_sk(sk);
1697-
struct tipc_msg *msg = buf_msg(buf);
1698-
unsigned int limit = rcvbuf_limit(sk, buf);
1699-
u32 onode;
1693+
struct tipc_msg *msg = buf_msg(*skb);
1694+
unsigned int limit = rcvbuf_limit(sk, *skb);
17001695
int rc = TIPC_OK;
17011696

1702-
if (unlikely(msg_user(msg) == CONN_MANAGER))
1703-
return tipc_sk_proto_rcv(tsk, &onode, buf);
1697+
if (unlikely(msg_user(msg) == CONN_MANAGER)) {
1698+
tipc_sk_proto_rcv(tsk, skb);
1699+
return TIPC_OK;
1700+
}
17041701

17051702
if (unlikely(msg_user(msg) == SOCK_WAKEUP)) {
1706-
kfree_skb(buf);
1703+
kfree_skb(*skb);
17071704
tsk->link_cong = 0;
17081705
sk->sk_write_space(sk);
1706+
*skb = NULL;
17091707
return TIPC_OK;
17101708
}
17111709

@@ -1717,21 +1715,22 @@ static int filter_rcv(struct sock *sk, struct sk_buff *buf)
17171715
if (msg_connected(msg))
17181716
return -TIPC_ERR_NO_PORT;
17191717
} else {
1720-
rc = filter_connect(tsk, &buf);
1721-
if (rc != TIPC_OK || buf == NULL)
1718+
rc = filter_connect(tsk, skb);
1719+
if (rc != TIPC_OK || !*skb)
17221720
return rc;
17231721
}
17241722

17251723
/* Reject message if there isn't room to queue it */
1726-
if (sk_rmem_alloc_get(sk) + buf->truesize >= limit)
1724+
if (sk_rmem_alloc_get(sk) + (*skb)->truesize >= limit)
17271725
return -TIPC_ERR_OVERLOAD;
17281726

17291727
/* Enqueue message */
1730-
TIPC_SKB_CB(buf)->handle = NULL;
1731-
__skb_queue_tail(&sk->sk_receive_queue, buf);
1732-
skb_set_owner_r(buf, sk);
1728+
TIPC_SKB_CB(*skb)->handle = NULL;
1729+
__skb_queue_tail(&sk->sk_receive_queue, *skb);
1730+
skb_set_owner_r(*skb, sk);
17331731

17341732
sk->sk_data_ready(sk);
1733+
*skb = NULL;
17351734
return TIPC_OK;
17361735
}
17371736

@@ -1746,25 +1745,22 @@ static int filter_rcv(struct sock *sk, struct sk_buff *buf)
17461745
*/
17471746
static int tipc_backlog_rcv(struct sock *sk, struct sk_buff *skb)
17481747
{
1749-
int rc;
1750-
u32 onode;
1748+
int err;
1749+
atomic_t *dcnt;
1750+
u32 dnode;
17511751
struct tipc_sock *tsk = tipc_sk(sk);
17521752
struct net *net = sock_net(sk);
17531753
uint truesize = skb->truesize;
17541754

1755-
rc = filter_rcv(sk, skb);
1756-
1757-
if (likely(!rc)) {
1758-
if (atomic_read(&tsk->dupl_rcvcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1759-
atomic_add(truesize, &tsk->dupl_rcvcnt);
1755+
err = filter_rcv(sk, &skb);
1756+
if (likely(!skb)) {
1757+
dcnt = &tsk->dupl_rcvcnt;
1758+
if (atomic_read(dcnt) < TIPC_CONN_OVERLOAD_LIMIT)
1759+
atomic_add(truesize, dcnt);
17601760
return 0;
17611761
}
1762-
1763-
if ((rc < 0) && !tipc_msg_reverse(tsk_own_node(tsk), skb, &onode, -rc))
1764-
return 0;
1765-
1766-
tipc_link_xmit_skb(net, skb, onode, 0);
1767-
1762+
if (!err || tipc_msg_reverse(tsk_own_node(tsk), skb, &dnode, -err))
1763+
tipc_link_xmit_skb(net, skb, dnode, tsk->portid);
17681764
return 0;
17691765
}
17701766

@@ -1780,14 +1776,14 @@ int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
17801776
struct tipc_net *tn;
17811777
struct sock *sk;
17821778
u32 dport = msg_destport(buf_msg(skb));
1783-
int rc = TIPC_OK;
1779+
int err = TIPC_OK;
17841780
uint limit;
17851781
u32 dnode;
17861782

17871783
/* Validate destination and message */
17881784
tsk = tipc_sk_lookup(net, dport);
17891785
if (unlikely(!tsk)) {
1790-
rc = tipc_msg_eval(net, skb, &dnode);
1786+
err = tipc_msg_eval(net, skb, &dnode);
17911787
goto exit;
17921788
}
17931789
sk = &tsk->sk;
@@ -1796,25 +1792,25 @@ int tipc_sk_rcv(struct net *net, struct sk_buff *skb)
17961792
spin_lock_bh(&sk->sk_lock.slock);
17971793

17981794
if (!sock_owned_by_user(sk)) {
1799-
rc = filter_rcv(sk, skb);
1795+
err = filter_rcv(sk, &skb);
18001796
} else {
18011797
if (sk->sk_backlog.len == 0)
18021798
atomic_set(&tsk->dupl_rcvcnt, 0);
18031799
limit = rcvbuf_limit(sk, skb) + atomic_read(&tsk->dupl_rcvcnt);
1804-
if (sk_add_backlog(sk, skb, limit))
1805-
rc = -TIPC_ERR_OVERLOAD;
1800+
if (likely(!sk_add_backlog(sk, skb, limit)))
1801+
skb = NULL;
1802+
else
1803+
err = -TIPC_ERR_OVERLOAD;
18061804
}
18071805
spin_unlock_bh(&sk->sk_lock.slock);
18081806
sock_put(sk);
1809-
if (likely(!rc))
1810-
return 0;
18111807
exit:
1812-
tn = net_generic(net, tipc_net_id);
1813-
if ((rc < 0) && !tipc_msg_reverse(tn->own_addr, skb, &dnode, -rc))
1814-
return -EHOSTUNREACH;
1815-
1816-
tipc_link_xmit_skb(net, skb, dnode, 0);
1817-
return (rc < 0) ? -EHOSTUNREACH : 0;
1808+
if (unlikely(skb)) {
1809+
tn = net_generic(net, tipc_net_id);
1810+
if (!err || tipc_msg_reverse(tn->own_addr, skb, &dnode, -err))
1811+
tipc_link_xmit_skb(net, skb, dnode, 0);
1812+
}
1813+
return err ? -EHOSTUNREACH : 0;
18181814
}
18191815

18201816
static int tipc_wait_for_connect(struct socket *sock, long *timeo_p)

0 commit comments

Comments
 (0)