hc
2024-12-19 9370bb92b2d16684ee45cf24e879c93c509162da
kernel/net/tipc/socket.c
....@@ -46,13 +46,15 @@
4646 #include "bcast.h"
4747 #include "netlink.h"
4848 #include "group.h"
49
+#include "trace.h"
4950
50
-#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
51
+#define NAGLE_START_INIT 4
52
+#define NAGLE_START_MAX 1024
53
+#define CONN_TIMEOUT_DEFAULT 8000 /* default connect timeout = 8s */
5154 #define CONN_PROBING_INTV msecs_to_jiffies(3600000) /* [ms] => 1 h */
52
-#define TIPC_FWD_MSG 1
5355 #define TIPC_MAX_PORT 0xffffffff
5456 #define TIPC_MIN_PORT 1
55
-#define TIPC_ACK_RATE 4 /* ACK at 1/4 of of rcv window size */
57
+#define TIPC_ACK_RATE 4 /* ACK at 1/4 of rcv window size */
5658
5759 enum {
5860 TIPC_LISTEN = TCP_LISTEN,
....@@ -74,13 +76,13 @@
7476 * @conn_instance: TIPC instance used when connection was established
7577 * @published: non-zero if port has one or more associated names
7678 * @max_pkt: maximum packet size "hint" used when building messages sent by port
79
+ * @maxnagle: maximum size of msg which can be subject to nagle
7780 * @portid: unique port identity in TIPC socket hash table
7881 * @phdr: preformatted message header used when sending messages
7982 * #cong_links: list of congested links
8083 * @publications: list of publications for port
8184 * @blocking_link: address of the congested link we are currently sleeping on
8285 * @pub_count: total # of publications port has made during its lifetime
83
- * @probing_state:
8486 * @conn_timeout: the time we can wait for an unresponded setup request
8587 * @dupl_rcvcnt: number of bytes counted twice, in both backlog and rcv queue
8688 * @cong_link_cnt: number of congested links
....@@ -97,13 +99,14 @@
9799 u32 conn_instance;
98100 int published;
99101 u32 max_pkt;
102
+ u32 maxnagle;
100103 u32 portid;
101104 struct tipc_msg phdr;
102105 struct list_head cong_links;
103106 struct list_head publications;
104107 u32 pub_count;
105
- uint conn_timeout;
106108 atomic_t dupl_rcvcnt;
109
+ u16 conn_timeout;
107110 bool probe_unacked;
108111 u16 cong_link_cnt;
109112 u16 snt_unacked;
....@@ -116,6 +119,13 @@
116119 struct tipc_mc_method mc_method;
117120 struct rcu_head rcu;
118121 struct tipc_group *group;
122
+ u32 oneway;
123
+ u32 nagle_start;
124
+ u16 snd_backlog;
125
+ u16 msg_acc;
126
+ u16 pkt_cnt;
127
+ bool expect_ack;
128
+ bool nodelay;
119129 bool group_is_open;
120130 };
121131
....@@ -137,6 +147,8 @@
137147 static void tipc_sk_remove(struct tipc_sock *tsk);
138148 static int __tipc_sendstream(struct socket *sock, struct msghdr *m, size_t dsz);
139149 static int __tipc_sendmsg(struct socket *sock, struct msghdr *m, size_t dsz);
150
+static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack);
151
+static int tipc_wait_for_connect(struct socket *sock, long *timeo_p);
140152
141153 static const struct proto_ops packet_ops;
142154 static const struct proto_ops stream_ops;
....@@ -184,17 +196,17 @@
184196 return msg_importance(&tsk->phdr);
185197 }
186198
187
-static int tsk_set_importance(struct tipc_sock *tsk, int imp)
188
-{
189
- if (imp > TIPC_CRITICAL_IMPORTANCE)
190
- return -EINVAL;
191
- msg_set_importance(&tsk->phdr, (u32)imp);
192
- return 0;
193
-}
194
-
195199 static struct tipc_sock *tipc_sk(const struct sock *sk)
196200 {
197201 return container_of(sk, struct tipc_sock, sk);
202
+}
203
+
204
+int tsk_set_importance(struct sock *sk, int imp)
205
+{
206
+ if (imp > TIPC_CRITICAL_IMPORTANCE)
207
+ return -EINVAL;
208
+ msg_set_importance(&tipc_sk(sk)->phdr, (u32)imp);
209
+ return 0;
198210 }
199211
200212 static bool tsk_conn_cong(struct tipc_sock *tsk)
....@@ -227,6 +239,26 @@
227239 return 1;
228240 }
229241
242
+/* tsk_set_nagle - enable/disable nagle property by manipulating maxnagle
243
+ */
244
+static void tsk_set_nagle(struct tipc_sock *tsk)
245
+{
246
+ struct sock *sk = &tsk->sk;
247
+
248
+ tsk->maxnagle = 0;
249
+ if (sk->sk_type != SOCK_STREAM)
250
+ return;
251
+ if (tsk->nodelay)
252
+ return;
253
+ if (!(tsk->peer_caps & TIPC_NAGLE))
254
+ return;
255
+ /* Limit node local buffer size to avoid receive queue overflow */
256
+ if (tsk->max_pkt == MAX_MSG_SIZE)
257
+ tsk->maxnagle = 1500;
258
+ else
259
+ tsk->maxnagle = tsk->max_pkt;
260
+}
261
+
230262 /**
231263 * tsk_advance_rx_queue - discard first buffer in socket receive queue
232264 *
....@@ -234,6 +266,7 @@
234266 */
235267 static void tsk_advance_rx_queue(struct sock *sk)
236268 {
269
+ trace_tipc_sk_advance_rx(sk, NULL, TIPC_DUMP_SK_RCVQ, " ");
237270 kfree_skb(__skb_dequeue(&sk->sk_receive_queue));
238271 }
239272
....@@ -248,6 +281,7 @@
248281 if (!tipc_msg_reverse(onode, &skb, err))
249282 return;
250283
284
+ trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE, "@sk_respond!");
251285 dnode = msg_destnode(buf_msg(skb));
252286 selector = msg_origport(buf_msg(skb));
253287 tipc_node_xmit_skb(sock_net(sk), skb, dnode, selector);
....@@ -258,17 +292,17 @@
258292 *
259293 * Caller must hold socket lock
260294 */
261
-static void tsk_rej_rx_queue(struct sock *sk)
295
+static void tsk_rej_rx_queue(struct sock *sk, int error)
262296 {
263297 struct sk_buff *skb;
264298
265299 while ((skb = __skb_dequeue(&sk->sk_receive_queue)))
266
- tipc_sk_respond(sk, skb, TIPC_ERR_NO_PORT);
300
+ tipc_sk_respond(sk, skb, error);
267301 }
268302
269
-static bool tipc_sk_connected(struct sock *sk)
303
+static bool tipc_sk_connected(const struct sock *sk)
270304 {
271
- return sk->sk_state == TIPC_ESTABLISHED;
305
+ return READ_ONCE(sk->sk_state) == TIPC_ESTABLISHED;
272306 }
273307
274308 /* tipc_sk_type_connectionless - check if the socket is datagram socket
....@@ -388,7 +422,7 @@
388422 rc_ = tipc_sk_sock_err((sock_), timeo_); \
389423 if (rc_) \
390424 break; \
391
- prepare_to_wait(sk_sleep(sk_), &wait_, TASK_INTERRUPTIBLE); \
425
+ add_wait_queue(sk_sleep(sk_), &wait_); \
392426 release_sock(sk_); \
393427 *(timeo_) = wait_woken(&wait_, TASK_INTERRUPTIBLE, *(timeo_)); \
394428 sched_annotate_sleep(); \
....@@ -444,6 +478,8 @@
444478
445479 tsk = tipc_sk(sk);
446480 tsk->max_pkt = MAX_PKT_DEFAULT;
481
+ tsk->maxnagle = 0;
482
+ tsk->nagle_start = NAGLE_START_INIT;
447483 INIT_LIST_HEAD(&tsk->publications);
448484 INIT_LIST_HEAD(&tsk->cong_links);
449485 msg = &tsk->phdr;
....@@ -453,6 +489,7 @@
453489 sock_init_data(sock, sk);
454490 tipc_set_sk_state(sk, TIPC_OPEN);
455491 if (tipc_sk_insert(tsk)) {
492
+ sk_free(sk);
456493 pr_warn("Socket create failed; port number exhausted\n");
457494 return -EINVAL;
458495 }
....@@ -467,7 +504,7 @@
467504 timer_setup(&sk->sk_timer, tipc_sk_timeout, 0);
468505 sk->sk_shutdown = 0;
469506 sk->sk_backlog_rcv = tipc_sk_backlog_rcv;
470
- sk->sk_rcvbuf = sysctl_tipc_rmem[1];
507
+ sk->sk_rcvbuf = READ_ONCE(sysctl_tipc_rmem[1]);
471508 sk->sk_data_ready = tipc_data_ready;
472509 sk->sk_write_space = tipc_write_space;
473510 sk->sk_destruct = tipc_sock_destruct;
....@@ -484,7 +521,8 @@
484521 if (sock->type == SOCK_DGRAM)
485522 tsk_set_unreliable(tsk, true);
486523 }
487
-
524
+ __skb_queue_head_init(&tsk->mc_method.deferredq);
525
+ trace_tipc_sk_create(sk, NULL, TIPC_DUMP_NONE, " ");
488526 return 0;
489527 }
490528
....@@ -509,34 +547,50 @@
509547 tipc_wait_for_cond(sock, &timeout, (!tsk->cong_link_cnt &&
510548 !tsk_conn_cong(tsk)));
511549
512
- /* Reject all unreceived messages, except on an active connection
513
- * (which disconnects locally & sends a 'FIN+' to peer).
514
- */
515
- while ((skb = __skb_dequeue(&sk->sk_receive_queue)) != NULL) {
516
- if (TIPC_SKB_CB(skb)->bytes_read) {
517
- kfree_skb(skb);
518
- continue;
519
- }
520
- if (!tipc_sk_type_connectionless(sk) &&
521
- sk->sk_state != TIPC_DISCONNECTING) {
522
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
523
- tipc_node_remove_conn(net, dnode, tsk->portid);
524
- }
525
- tipc_sk_respond(sk, skb, error);
550
+ /* Push out delayed messages if in Nagle mode */
551
+ tipc_sk_push_backlog(tsk, false);
552
+ /* Remove pending SYN */
553
+ __skb_queue_purge(&sk->sk_write_queue);
554
+
555
+ /* Remove partially received buffer if any */
556
+ skb = skb_peek(&sk->sk_receive_queue);
557
+ if (skb && TIPC_SKB_CB(skb)->bytes_read) {
558
+ __skb_unlink(skb, &sk->sk_receive_queue);
559
+ kfree_skb(skb);
526560 }
527561
528
- if (tipc_sk_type_connectionless(sk))
562
+ /* Reject all unreceived messages if connectionless */
563
+ if (tipc_sk_type_connectionless(sk)) {
564
+ tsk_rej_rx_queue(sk, error);
529565 return;
566
+ }
530567
531
- if (sk->sk_state != TIPC_DISCONNECTING) {
568
+ switch (sk->sk_state) {
569
+ case TIPC_CONNECTING:
570
+ case TIPC_ESTABLISHED:
571
+ tipc_set_sk_state(sk, TIPC_DISCONNECTING);
572
+ tipc_node_remove_conn(net, dnode, tsk->portid);
573
+ /* Send a FIN+/- to its peer */
574
+ skb = __skb_dequeue(&sk->sk_receive_queue);
575
+ if (skb) {
576
+ __skb_queue_purge(&sk->sk_receive_queue);
577
+ tipc_sk_respond(sk, skb, error);
578
+ break;
579
+ }
532580 skb = tipc_msg_create(TIPC_CRITICAL_IMPORTANCE,
533581 TIPC_CONN_MSG, SHORT_H_SIZE, 0, dnode,
534582 tsk_own_node(tsk), tsk_peer_port(tsk),
535583 tsk->portid, error);
536584 if (skb)
537585 tipc_node_xmit_skb(net, skb, dnode, tsk->portid);
538
- tipc_node_remove_conn(net, dnode, tsk->portid);
539
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
586
+ break;
587
+ case TIPC_LISTEN:
588
+ /* Reject all SYN messages */
589
+ tsk_rej_rx_queue(sk, error);
590
+ break;
591
+ default:
592
+ __skb_queue_purge(&sk->sk_receive_queue);
593
+ break;
540594 }
541595 }
542596
....@@ -571,10 +625,12 @@
571625 tsk = tipc_sk(sk);
572626 lock_sock(sk);
573627
628
+ trace_tipc_sk_release(sk, NULL, TIPC_DUMP_ALL, " ");
574629 __tipc_shutdown(sock, TIPC_ERR_NO_PORT);
575630 sk->sk_shutdown = SHUTDOWN_MASK;
576631 tipc_sk_leave(tsk);
577632 tipc_sk_withdraw(tsk, 0, NULL);
633
+ __skb_queue_purge(&tsk->mc_method.deferredq);
578634 sk_stop_timer(sk, &sk->sk_timer);
579635 tipc_sk_remove(tsk);
580636
....@@ -656,7 +712,6 @@
656712 * tipc_getname - get port ID of socket or peer socket
657713 * @sock: socket structure
658714 * @uaddr: area for returned socket address
659
- * @uaddr_len: area for returned length of socket address
660715 * @peer: 0 = own ID, 1 = current peer ID, 2 = current/former peer ID
661716 *
662717 * Returns 0 on success, errno otherwise
....@@ -718,6 +773,7 @@
718773 __poll_t revents = 0;
719774
720775 sock_poll_wait(file, sock, wait);
776
+ trace_tipc_sk_poll(sk, NULL, TIPC_DUMP_ALL, " ");
721777
722778 if (sk->sk_shutdown & RCV_SHUTDOWN)
723779 revents |= EPOLLRDHUP | EPOLLIN | EPOLLRDNORM;
....@@ -728,7 +784,7 @@
728784 case TIPC_ESTABLISHED:
729785 if (!tsk->cong_link_cnt && !tsk_conn_cong(tsk))
730786 revents |= EPOLLOUT;
731
- /* fall thru' */
787
+ fallthrough;
732788 case TIPC_LISTEN:
733789 case TIPC_CONNECTING:
734790 if (!skb_queue_empty_lockless(&sk->sk_receive_queue))
....@@ -804,9 +860,12 @@
804860 rc = tipc_msg_build(hdr, msg, 0, dlen, mtu, &pkts);
805861
806862 /* Send message if build was successful */
807
- if (unlikely(rc == dlen))
863
+ if (unlikely(rc == dlen)) {
864
+ trace_tipc_sk_sendmcast(sk, skb_peek(&pkts),
865
+ TIPC_DUMP_SK_SNDQ, " ");
808866 rc = tipc_mcast_xmit(net, &pkts, method, &dsts,
809867 &tsk->cong_link_cnt);
868
+ }
810869
811870 tipc_nlist_purge(&dsts);
812871
....@@ -842,7 +901,7 @@
842901
843902 /* Build message as chain of buffers */
844903 __skb_queue_head_init(&pkts);
845
- mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
904
+ mtu = tipc_node_get_mtu(net, dnode, tsk->portid, false);
846905 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
847906 if (unlikely(rc != dlen))
848907 return rc;
....@@ -994,7 +1053,7 @@
9941053
9951054 /**
9961055 * tipc_send_group_bcast - send message to all members in communication group
997
- * @sk: socket structure
1056
+ * @sock: socket structure
9981057 * @m: message to send
9991058 * @dlen: total length of message data
10001059 * @timeout: timeout to wait for wakeup
....@@ -1199,6 +1258,56 @@
11991258 tipc_sk_rcv(net, inputq);
12001259 }
12011260
1261
+/* tipc_sk_push_backlog(): send accumulated buffers in socket write queue
1262
+ * when socket is in Nagle mode
1263
+ */
1264
+static void tipc_sk_push_backlog(struct tipc_sock *tsk, bool nagle_ack)
1265
+{
1266
+ struct sk_buff_head *txq = &tsk->sk.sk_write_queue;
1267
+ struct sk_buff *skb = skb_peek_tail(txq);
1268
+ struct net *net = sock_net(&tsk->sk);
1269
+ u32 dnode = tsk_peer_node(tsk);
1270
+ int rc;
1271
+
1272
+ if (nagle_ack) {
1273
+ tsk->pkt_cnt += skb_queue_len(txq);
1274
+ if (!tsk->pkt_cnt || tsk->msg_acc / tsk->pkt_cnt < 2) {
1275
+ tsk->oneway = 0;
1276
+ if (tsk->nagle_start < NAGLE_START_MAX)
1277
+ tsk->nagle_start *= 2;
1278
+ tsk->expect_ack = false;
1279
+ pr_debug("tsk %10u: bad nagle %u -> %u, next start %u!\n",
1280
+ tsk->portid, tsk->msg_acc, tsk->pkt_cnt,
1281
+ tsk->nagle_start);
1282
+ } else {
1283
+ tsk->nagle_start = NAGLE_START_INIT;
1284
+ if (skb) {
1285
+ msg_set_ack_required(buf_msg(skb));
1286
+ tsk->expect_ack = true;
1287
+ } else {
1288
+ tsk->expect_ack = false;
1289
+ }
1290
+ }
1291
+ tsk->msg_acc = 0;
1292
+ tsk->pkt_cnt = 0;
1293
+ }
1294
+
1295
+ if (!skb || tsk->cong_link_cnt)
1296
+ return;
1297
+
1298
+ /* Do not send SYN again after congestion */
1299
+ if (msg_is_syn(buf_msg(skb)))
1300
+ return;
1301
+
1302
+ if (tsk->msg_acc)
1303
+ tsk->pkt_cnt += skb_queue_len(txq);
1304
+ tsk->snt_unacked += tsk->snd_backlog;
1305
+ tsk->snd_backlog = 0;
1306
+ rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
1307
+ if (rc == -ELINKCONG)
1308
+ tsk->cong_link_cnt = 1;
1309
+}
1310
+
12021311 /**
12031312 * tipc_sk_conn_proto_rcv - receive a connection mng protocol message
12041313 * @tsk: receiving socket
....@@ -1212,11 +1321,13 @@
12121321 u32 onode = tsk_own_node(tsk);
12131322 struct sock *sk = &tsk->sk;
12141323 int mtyp = msg_type(hdr);
1215
- bool conn_cong;
1324
+ bool was_cong;
12161325
12171326 /* Ignore if connection cannot be validated: */
1218
- if (!tsk_peer_msg(tsk, hdr))
1327
+ if (!tsk_peer_msg(tsk, hdr)) {
1328
+ trace_tipc_sk_drop_msg(sk, skb, TIPC_DUMP_NONE, "@proto_rcv!");
12191329 goto exit;
1330
+ }
12201331
12211332 if (unlikely(msg_errcode(hdr))) {
12221333 tipc_set_sk_state(sk, TIPC_DISCONNECTING);
....@@ -1243,11 +1354,12 @@
12431354 __skb_queue_tail(xmitq, skb);
12441355 return;
12451356 } else if (mtyp == CONN_ACK) {
1246
- conn_cong = tsk_conn_cong(tsk);
1357
+ was_cong = tsk_conn_cong(tsk);
1358
+ tipc_sk_push_backlog(tsk, msg_nagle_ack(hdr));
12471359 tsk->snt_unacked -= msg_conn_ack(hdr);
12481360 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
12491361 tsk->snd_win = msg_adv_win(hdr);
1250
- if (conn_cong)
1362
+ if (was_cong && !tsk_conn_cong(tsk))
12511363 sk->sk_write_space(sk);
12521364 } else if (mtyp != CONN_PROBE_REPLY) {
12531365 pr_warn("Received unknown CONN_PROTO msg\n");
....@@ -1295,8 +1407,8 @@
12951407 struct tipc_msg *hdr = &tsk->phdr;
12961408 struct tipc_name_seq *seq;
12971409 struct sk_buff_head pkts;
1298
- u32 dport, dnode = 0;
1299
- u32 type, inst;
1410
+ u32 dport = 0, dnode = 0;
1411
+ u32 type = 0, inst = 0;
13001412 int mtu, rc;
13011413
13021414 if (unlikely(dlen > TIPC_MAX_USER_MSG_SIZE))
....@@ -1338,6 +1450,7 @@
13381450 tsk->conn_type = dest->addr.name.name.type;
13391451 tsk->conn_instance = dest->addr.name.name.instance;
13401452 }
1453
+ msg_set_syn(hdr, 1);
13411454 }
13421455
13431456 seq = &dest->addr.nameseq;
....@@ -1348,23 +1461,11 @@
13481461 type = dest->addr.name.name.type;
13491462 inst = dest->addr.name.name.instance;
13501463 dnode = dest->addr.name.domain;
1351
- msg_set_type(hdr, TIPC_NAMED_MSG);
1352
- msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1353
- msg_set_nametype(hdr, type);
1354
- msg_set_nameinst(hdr, inst);
1355
- msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
13561464 dport = tipc_nametbl_translate(net, type, inst, &dnode);
1357
- msg_set_destnode(hdr, dnode);
1358
- msg_set_destport(hdr, dport);
13591465 if (unlikely(!dport && !dnode))
13601466 return -EHOSTUNREACH;
13611467 } else if (dest->addrtype == TIPC_ADDR_ID) {
13621468 dnode = dest->addr.id.node;
1363
- msg_set_type(hdr, TIPC_DIRECT_MSG);
1364
- msg_set_lookup_scope(hdr, 0);
1365
- msg_set_destnode(hdr, dnode);
1366
- msg_set_destport(hdr, dest->addr.id.ref);
1367
- msg_set_hdr_sz(hdr, BASIC_H_SIZE);
13681469 } else {
13691470 return -EINVAL;
13701471 }
....@@ -1375,12 +1476,33 @@
13751476 if (unlikely(rc))
13761477 return rc;
13771478
1479
+ if (dest->addrtype == TIPC_ADDR_NAME) {
1480
+ msg_set_type(hdr, TIPC_NAMED_MSG);
1481
+ msg_set_hdr_sz(hdr, NAMED_H_SIZE);
1482
+ msg_set_nametype(hdr, type);
1483
+ msg_set_nameinst(hdr, inst);
1484
+ msg_set_lookup_scope(hdr, tipc_node2scope(dnode));
1485
+ msg_set_destnode(hdr, dnode);
1486
+ msg_set_destport(hdr, dport);
1487
+ } else { /* TIPC_ADDR_ID */
1488
+ msg_set_type(hdr, TIPC_DIRECT_MSG);
1489
+ msg_set_lookup_scope(hdr, 0);
1490
+ msg_set_destnode(hdr, dnode);
1491
+ msg_set_destport(hdr, dest->addr.id.ref);
1492
+ msg_set_hdr_sz(hdr, BASIC_H_SIZE);
1493
+ }
1494
+
13781495 __skb_queue_head_init(&pkts);
1379
- mtu = tipc_node_get_mtu(net, dnode, tsk->portid);
1496
+ mtu = tipc_node_get_mtu(net, dnode, tsk->portid, true);
13801497 rc = tipc_msg_build(hdr, m, 0, dlen, mtu, &pkts);
13811498 if (unlikely(rc != dlen))
13821499 return rc;
1500
+ if (unlikely(syn && !tipc_msg_skb_clone(&pkts, &sk->sk_write_queue))) {
1501
+ __skb_queue_purge(&pkts);
1502
+ return -ENOMEM;
1503
+ }
13831504
1505
+ trace_tipc_sk_sendmsg(sk, skb_peek(&pkts), TIPC_DUMP_SK_SNDQ, " ");
13841506 rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
13851507 if (unlikely(rc == -ELINKCONG)) {
13861508 tipc_dest_push(clinks, dnode, 0);
....@@ -1388,8 +1510,13 @@
13881510 rc = 0;
13891511 }
13901512
1391
- if (unlikely(syn && !rc))
1513
+ if (unlikely(syn && !rc)) {
13921514 tipc_set_sk_state(sk, TIPC_CONNECTING);
1515
+ if (dlen && timeout) {
1516
+ timeout = msecs_to_jiffies(timeout);
1517
+ tipc_wait_for_connect(sock, &timeout);
1518
+ }
1519
+ }
13931520
13941521 return rc ? rc : dlen;
13951522 }
....@@ -1422,21 +1549,22 @@
14221549 struct sock *sk = sock->sk;
14231550 DECLARE_SOCKADDR(struct sockaddr_tipc *, dest, m->msg_name);
14241551 long timeout = sock_sndtimeo(sk, m->msg_flags & MSG_DONTWAIT);
1552
+ struct sk_buff_head *txq = &sk->sk_write_queue;
14251553 struct tipc_sock *tsk = tipc_sk(sk);
14261554 struct tipc_msg *hdr = &tsk->phdr;
14271555 struct net *net = sock_net(sk);
1428
- struct sk_buff_head pkts;
1556
+ struct sk_buff *skb;
14291557 u32 dnode = tsk_peer_node(tsk);
1558
+ int maxnagle = tsk->maxnagle;
1559
+ int maxpkt = tsk->max_pkt;
14301560 int send, sent = 0;
1431
- int rc = 0;
1432
-
1433
- __skb_queue_head_init(&pkts);
1561
+ int blocks, rc = 0;
14341562
14351563 if (unlikely(dlen > INT_MAX))
14361564 return -EMSGSIZE;
14371565
14381566 /* Handle implicit connection setup */
1439
- if (unlikely(dest)) {
1567
+ if (unlikely(dest && sk->sk_state == TIPC_OPEN)) {
14401568 rc = __tipc_sendmsg(sock, m, dlen);
14411569 if (dlen && dlen == rc) {
14421570 tsk->peer_caps = tipc_node_get_capabilities(net, dnode);
....@@ -1452,19 +1580,48 @@
14521580 tipc_sk_connected(sk)));
14531581 if (unlikely(rc))
14541582 break;
1455
-
14561583 send = min_t(size_t, dlen - sent, TIPC_MAX_USER_MSG_SIZE);
1457
- rc = tipc_msg_build(hdr, m, sent, send, tsk->max_pkt, &pkts);
1458
- if (unlikely(rc != send))
1459
- break;
1460
-
1461
- rc = tipc_node_xmit(net, &pkts, dnode, tsk->portid);
1584
+ blocks = tsk->snd_backlog;
1585
+ if (tsk->oneway++ >= tsk->nagle_start && maxnagle &&
1586
+ send <= maxnagle) {
1587
+ rc = tipc_msg_append(hdr, m, send, maxnagle, txq);
1588
+ if (unlikely(rc < 0))
1589
+ break;
1590
+ blocks += rc;
1591
+ tsk->msg_acc++;
1592
+ if (blocks <= 64 && tsk->expect_ack) {
1593
+ tsk->snd_backlog = blocks;
1594
+ sent += send;
1595
+ break;
1596
+ } else if (blocks > 64) {
1597
+ tsk->pkt_cnt += skb_queue_len(txq);
1598
+ } else {
1599
+ skb = skb_peek_tail(txq);
1600
+ if (skb) {
1601
+ msg_set_ack_required(buf_msg(skb));
1602
+ tsk->expect_ack = true;
1603
+ } else {
1604
+ tsk->expect_ack = false;
1605
+ }
1606
+ tsk->msg_acc = 0;
1607
+ tsk->pkt_cnt = 0;
1608
+ }
1609
+ } else {
1610
+ rc = tipc_msg_build(hdr, m, sent, send, maxpkt, txq);
1611
+ if (unlikely(rc != send))
1612
+ break;
1613
+ blocks += tsk_inc(tsk, send + MIN_H_SIZE);
1614
+ }
1615
+ trace_tipc_sk_sendstream(sk, skb_peek(txq),
1616
+ TIPC_DUMP_SK_SNDQ, " ");
1617
+ rc = tipc_node_xmit(net, txq, dnode, tsk->portid);
14621618 if (unlikely(rc == -ELINKCONG)) {
14631619 tsk->cong_link_cnt = 1;
14641620 rc = 0;
14651621 }
14661622 if (likely(!rc)) {
1467
- tsk->snt_unacked += tsk_inc(tsk, send + MIN_H_SIZE);
1623
+ tsk->snt_unacked += blocks;
1624
+ tsk->snd_backlog = 0;
14681625 sent += send;
14691626 }
14701627 } while (sent < dlen && !rc);
....@@ -1499,6 +1656,7 @@
14991656 struct net *net = sock_net(sk);
15001657 struct tipc_msg *msg = &tsk->phdr;
15011658
1659
+ msg_set_syn(msg, 0);
15021660 msg_set_destnode(msg, peer_node);
15031661 msg_set_destport(msg, peer_port);
15041662 msg_set_type(msg, TIPC_CONN_MSG);
....@@ -1508,8 +1666,10 @@
15081666 sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
15091667 tipc_set_sk_state(sk, TIPC_ESTABLISHED);
15101668 tipc_node_add_conn(net, peer_node, tsk->portid, peer_port);
1511
- tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid);
1669
+ tsk->max_pkt = tipc_node_get_mtu(net, peer_node, tsk->portid, true);
15121670 tsk->peer_caps = tipc_node_get_capabilities(net, peer_node);
1671
+ tsk_set_nagle(tsk);
1672
+ __skb_queue_purge(&sk->sk_write_queue);
15131673 if (tsk->peer_caps & TIPC_BLOCK_FLOWCTL)
15141674 return;
15151675
....@@ -1521,7 +1681,7 @@
15211681 /**
15221682 * tipc_sk_set_orig_addr - capture sender's address for received message
15231683 * @m: descriptor for message info
1524
- * @hdr: received message header
1684
+ * @skb: received message
15251685 *
15261686 * Note: Address is not captured if not requested by receiver.
15271687 */
....@@ -1630,22 +1790,21 @@
16301790 return 0;
16311791 }
16321792
1633
-static void tipc_sk_send_ack(struct tipc_sock *tsk)
1793
+static struct sk_buff *tipc_sk_build_ack(struct tipc_sock *tsk)
16341794 {
16351795 struct sock *sk = &tsk->sk;
1636
- struct net *net = sock_net(sk);
16371796 struct sk_buff *skb = NULL;
16381797 struct tipc_msg *msg;
16391798 u32 peer_port = tsk_peer_port(tsk);
16401799 u32 dnode = tsk_peer_node(tsk);
16411800
16421801 if (!tipc_sk_connected(sk))
1643
- return;
1802
+ return NULL;
16441803 skb = tipc_msg_create(CONN_MANAGER, CONN_ACK, INT_H_SIZE, 0,
16451804 dnode, tsk_own_node(tsk), peer_port,
16461805 tsk->portid, TIPC_OK);
16471806 if (!skb)
1648
- return;
1807
+ return NULL;
16491808 msg = buf_msg(skb);
16501809 msg_set_conn_ack(msg, tsk->rcv_unacked);
16511810 tsk->rcv_unacked = 0;
....@@ -1655,13 +1814,25 @@
16551814 tsk->rcv_win = tsk_adv_blocks(tsk->sk.sk_rcvbuf);
16561815 msg_set_adv_win(msg, tsk->rcv_win);
16571816 }
1658
- tipc_node_xmit_skb(net, skb, dnode, msg_link_selector(msg));
1817
+ return skb;
1818
+}
1819
+
1820
+static void tipc_sk_send_ack(struct tipc_sock *tsk)
1821
+{
1822
+ struct sk_buff *skb;
1823
+
1824
+ skb = tipc_sk_build_ack(tsk);
1825
+ if (!skb)
1826
+ return;
1827
+
1828
+ tipc_node_xmit_skb(sock_net(&tsk->sk), skb, tsk_peer_node(tsk),
1829
+ msg_link_selector(buf_msg(skb)));
16591830 }
16601831
16611832 static int tipc_wait_for_rcvmsg(struct socket *sock, long *timeop)
16621833 {
16631834 struct sock *sk = sock->sk;
1664
- DEFINE_WAIT(wait);
1835
+ DEFINE_WAIT_FUNC(wait, woken_wake_function);
16651836 long timeo = *timeop;
16661837 int err = sock_error(sk);
16671838
....@@ -1669,15 +1840,17 @@
16691840 return err;
16701841
16711842 for (;;) {
1672
- prepare_to_wait(sk_sleep(sk), &wait, TASK_INTERRUPTIBLE);
16731843 if (timeo && skb_queue_empty(&sk->sk_receive_queue)) {
16741844 if (sk->sk_shutdown & RCV_SHUTDOWN) {
16751845 err = -ENOTCONN;
16761846 break;
16771847 }
1848
+ add_wait_queue(sk_sleep(sk), &wait);
16781849 release_sock(sk);
1679
- timeo = schedule_timeout(timeo);
1850
+ timeo = wait_woken(&wait, TASK_INTERRUPTIBLE, timeo);
1851
+ sched_annotate_sleep();
16801852 lock_sock(sk);
1853
+ remove_wait_queue(sk_sleep(sk), &wait);
16811854 }
16821855 err = 0;
16831856 if (!skb_queue_empty(&sk->sk_receive_queue))
....@@ -1693,7 +1866,6 @@
16931866 if (err)
16941867 break;
16951868 }
1696
- finish_wait(sk_sleep(sk), &wait);
16971869 *timeop = timeo;
16981870 return err;
16991871 }
....@@ -1919,7 +2091,7 @@
19192091
19202092 /* Send connection flow control advertisement when applicable */
19212093 tsk->rcv_unacked += tsk_inc(tsk, hlen + dlen);
1922
- if (unlikely(tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE))
2094
+ if (tsk->rcv_unacked >= tsk->rcv_win / TIPC_ACK_RATE)
19232095 tipc_sk_send_ack(tsk);
19242096
19252097 /* Exit if all requested data or FIN/error received */
....@@ -1951,7 +2123,6 @@
19512123 /**
19522124 * tipc_data_ready - wake up threads to indicate messages have been received
19532125 * @sk: socket
1954
- * @len: the length of messages
19552126 */
19562127 static void tipc_data_ready(struct sock *sk)
19572128 {
....@@ -1990,6 +2161,7 @@
19902161 smp_wmb();
19912162 tsk->cong_link_cnt--;
19922163 wakeup = true;
2164
+ tipc_sk_push_backlog(tsk, false);
19932165 break;
19942166 case GROUP_PROTOCOL:
19952167 tipc_group_proto_rcv(grp, &wakeup, hdr, inputq, xmitq);
....@@ -2009,91 +2181,106 @@
20092181 }
20102182
20112183 /**
2012
- * tipc_filter_connect - Handle incoming message for a connection-based socket
2184
+ * tipc_sk_filter_connect - check incoming message for a connection-based socket
20132185 * @tsk: TIPC socket
2014
- * @skb: pointer to message buffer. Set to NULL if buffer is consumed
2015
- *
2016
- * Returns true if everything ok, false otherwise
2186
+ * @skb: pointer to message buffer.
2187
+ * @xmitq: for Nagle ACK if any
2188
+ * Returns true if message should be added to receive queue, false otherwise
20172189 */
2018
-static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb)
2190
+static bool tipc_sk_filter_connect(struct tipc_sock *tsk, struct sk_buff *skb,
2191
+ struct sk_buff_head *xmitq)
20192192 {
20202193 struct sock *sk = &tsk->sk;
20212194 struct net *net = sock_net(sk);
20222195 struct tipc_msg *hdr = buf_msg(skb);
2023
- u32 pport = msg_origport(hdr);
2024
- u32 pnode = msg_orignode(hdr);
2196
+ bool con_msg = msg_connected(hdr);
2197
+ u32 pport = tsk_peer_port(tsk);
2198
+ u32 pnode = tsk_peer_node(tsk);
2199
+ u32 oport = msg_origport(hdr);
2200
+ u32 onode = msg_orignode(hdr);
2201
+ int err = msg_errcode(hdr);
2202
+ unsigned long delay;
20252203
20262204 if (unlikely(msg_mcast(hdr)))
20272205 return false;
2206
+ tsk->oneway = 0;
20282207
20292208 switch (sk->sk_state) {
20302209 case TIPC_CONNECTING:
2031
- /* Accept only ACK or NACK message */
2032
- if (unlikely(!msg_connected(hdr))) {
2033
- if (pport != tsk_peer_port(tsk) ||
2034
- pnode != tsk_peer_node(tsk))
2035
- return false;
2036
-
2037
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2038
- sk->sk_err = ECONNREFUSED;
2210
+ /* Setup ACK */
2211
+ if (likely(con_msg)) {
2212
+ if (err)
2213
+ break;
2214
+ tipc_sk_finish_conn(tsk, oport, onode);
2215
+ msg_set_importance(&tsk->phdr, msg_importance(hdr));
2216
+ /* ACK+ message with data is added to receive queue */
2217
+ if (msg_data_sz(hdr))
2218
+ return true;
2219
+ /* Empty ACK-, - wake up sleeping connect() and drop */
20392220 sk->sk_state_change(sk);
2040
- return true;
2221
+ msg_set_dest_droppable(hdr, 1);
2222
+ return false;
20412223 }
2042
-
2043
- if (unlikely(msg_errcode(hdr))) {
2044
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2045
- sk->sk_err = ECONNREFUSED;
2046
- sk->sk_state_change(sk);
2047
- return true;
2048
- }
2049
-
2050
- if (unlikely(!msg_isdata(hdr))) {
2051
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2052
- sk->sk_err = EINVAL;
2053
- sk->sk_state_change(sk);
2054
- return true;
2055
- }
2056
-
2057
- tipc_sk_finish_conn(tsk, msg_origport(hdr), msg_orignode(hdr));
2058
- msg_set_importance(&tsk->phdr, msg_importance(hdr));
2059
-
2060
- /* If 'ACK+' message, add to socket receive queue */
2061
- if (msg_data_sz(hdr))
2062
- return true;
2063
-
2064
- /* If empty 'ACK-' message, wake up sleeping connect() */
2065
- sk->sk_state_change(sk);
2066
-
2067
- /* 'ACK-' message is neither accepted nor rejected: */
2068
- msg_set_dest_droppable(hdr, 1);
2069
- return false;
2070
-
2071
- case TIPC_OPEN:
2072
- case TIPC_DISCONNECTING:
2073
- break;
2074
- case TIPC_LISTEN:
2075
- /* Accept only SYN message */
2076
- if (!msg_connected(hdr) && !(msg_errcode(hdr)))
2077
- return true;
2078
- break;
2079
- case TIPC_ESTABLISHED:
2080
- /* Accept only connection-based messages sent by peer */
2081
- if (unlikely(!tsk_peer_msg(tsk, hdr)))
2224
+ /* Ignore connectionless message if not from listening socket */
2225
+ if (oport != pport || onode != pnode)
20822226 return false;
20832227
2084
- if (unlikely(msg_errcode(hdr))) {
2085
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2086
- /* Let timer expire on it's own */
2087
- tipc_node_remove_conn(net, tsk_peer_node(tsk),
2088
- tsk->portid);
2089
- sk->sk_state_change(sk);
2228
+ /* Rejected SYN */
2229
+ if (err != TIPC_ERR_OVERLOAD)
2230
+ break;
2231
+
2232
+ /* Prepare for new setup attempt if we have a SYN clone */
2233
+ if (skb_queue_empty(&sk->sk_write_queue))
2234
+ break;
2235
+ get_random_bytes(&delay, 2);
2236
+ delay %= (tsk->conn_timeout / 4);
2237
+ delay = msecs_to_jiffies(delay + 100);
2238
+ sk_reset_timer(sk, &sk->sk_timer, jiffies + delay);
2239
+ return false;
2240
+ case TIPC_OPEN:
2241
+ case TIPC_DISCONNECTING:
2242
+ return false;
2243
+ case TIPC_LISTEN:
2244
+ /* Accept only SYN message */
2245
+ if (!msg_is_syn(hdr) &&
2246
+ tipc_node_get_capabilities(net, onode) & TIPC_SYN_BIT)
2247
+ return false;
2248
+ if (!con_msg && !err)
2249
+ return true;
2250
+ return false;
2251
+ case TIPC_ESTABLISHED:
2252
+ if (!skb_queue_empty(&sk->sk_write_queue))
2253
+ tipc_sk_push_backlog(tsk, false);
2254
+ /* Accept only connection-based messages sent by peer */
2255
+ if (likely(con_msg && !err && pport == oport &&
2256
+ pnode == onode)) {
2257
+ if (msg_ack_required(hdr)) {
2258
+ struct sk_buff *skb;
2259
+
2260
+ skb = tipc_sk_build_ack(tsk);
2261
+ if (skb) {
2262
+ msg_set_nagle_ack(buf_msg(skb));
2263
+ __skb_queue_tail(xmitq, skb);
2264
+ }
2265
+ }
2266
+ return true;
20902267 }
2268
+ if (!tsk_peer_msg(tsk, hdr))
2269
+ return false;
2270
+ if (!err)
2271
+ return true;
2272
+ tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2273
+ tipc_node_remove_conn(net, pnode, tsk->portid);
2274
+ sk->sk_state_change(sk);
20912275 return true;
20922276 default:
20932277 pr_err("Unknown sk_state %u\n", sk->sk_state);
20942278 }
2095
-
2096
- return false;
2279
+ /* Abort connection setup attempt */
2280
+ tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2281
+ sk->sk_err = ECONNREFUSED;
2282
+ sk->sk_state_change(sk);
2283
+ return true;
20972284 }
20982285
20992286 /**
....@@ -2120,13 +2307,13 @@
21202307 struct tipc_msg *hdr = buf_msg(skb);
21212308
21222309 if (unlikely(msg_in_group(hdr)))
2123
- return sk->sk_rcvbuf;
2310
+ return READ_ONCE(sk->sk_rcvbuf);
21242311
21252312 if (unlikely(!msg_connected(hdr)))
2126
- return sk->sk_rcvbuf << msg_importance(hdr);
2313
+ return READ_ONCE(sk->sk_rcvbuf) << msg_importance(hdr);
21272314
21282315 if (likely(tsk->peer_caps & TIPC_BLOCK_FLOWCTL))
2129
- return sk->sk_rcvbuf;
2316
+ return READ_ONCE(sk->sk_rcvbuf);
21302317
21312318 return FLOWCTL_MSG_LIM;
21322319 }
....@@ -2151,8 +2338,10 @@
21512338 struct tipc_msg *hdr = buf_msg(skb);
21522339 struct net *net = sock_net(sk);
21532340 struct sk_buff_head inputq;
2341
+ int mtyp = msg_type(hdr);
21542342 int limit, err = TIPC_OK;
21552343
2344
+ trace_tipc_sk_filter_rcv(sk, skb, TIPC_DUMP_ALL, " ");
21562345 TIPC_SKB_CB(skb)->bytes_read = 0;
21572346 __skb_queue_head_init(&inputq);
21582347 __skb_queue_tail(&inputq, skb);
....@@ -2163,26 +2352,37 @@
21632352 if (unlikely(grp))
21642353 tipc_group_filter_msg(grp, &inputq, xmitq);
21652354
2355
+ if (unlikely(!grp) && mtyp == TIPC_MCAST_MSG)
2356
+ tipc_mcast_filter_msg(net, &tsk->mc_method.deferredq, &inputq);
2357
+
21662358 /* Validate and add to receive buffer if there is space */
21672359 while ((skb = __skb_dequeue(&inputq))) {
21682360 hdr = buf_msg(skb);
21692361 limit = rcvbuf_limit(sk, skb);
2170
- if ((sk_conn && !tipc_sk_filter_connect(tsk, skb)) ||
2362
+ if ((sk_conn && !tipc_sk_filter_connect(tsk, skb, xmitq)) ||
21712363 (!sk_conn && msg_connected(hdr)) ||
21722364 (!grp && msg_in_group(hdr)))
21732365 err = TIPC_ERR_NO_PORT;
21742366 else if (sk_rmem_alloc_get(sk) + skb->truesize >= limit) {
2367
+ trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL,
2368
+ "err_overload2!");
21752369 atomic_inc(&sk->sk_drops);
21762370 err = TIPC_ERR_OVERLOAD;
21772371 }
21782372
21792373 if (unlikely(err)) {
2180
- tipc_skb_reject(net, err, skb, xmitq);
2374
+ if (tipc_msg_reverse(tipc_own_addr(net), &skb, err)) {
2375
+ trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_NONE,
2376
+ "@filter_rcv!");
2377
+ __skb_queue_tail(xmitq, skb);
2378
+ }
21812379 err = TIPC_OK;
21822380 continue;
21832381 }
21842382 __skb_queue_tail(&sk->sk_receive_queue, skb);
21852383 skb_set_owner_r(skb, sk);
2384
+ trace_tipc_sk_overlimit2(sk, skb, TIPC_DUMP_ALL,
2385
+ "rcvq >90% allocated!");
21862386 sk->sk_data_ready(sk);
21872387 }
21882388 }
....@@ -2248,14 +2448,21 @@
22482448 if (!sk->sk_backlog.len)
22492449 atomic_set(dcnt, 0);
22502450 lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
2251
- if (likely(!sk_add_backlog(sk, skb, lim)))
2451
+ if (likely(!sk_add_backlog(sk, skb, lim))) {
2452
+ trace_tipc_sk_overlimit1(sk, skb, TIPC_DUMP_ALL,
2453
+ "bklg & rcvq >90% allocated!");
22522454 continue;
2455
+ }
22532456
2457
+ trace_tipc_sk_dump(sk, skb, TIPC_DUMP_ALL, "err_overload!");
22542458 /* Overload => reject message back to sender */
22552459 onode = tipc_own_addr(sock_net(sk));
22562460 atomic_inc(&sk->sk_drops);
2257
- if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD))
2461
+ if (tipc_msg_reverse(onode, &skb, TIPC_ERR_OVERLOAD)) {
2462
+ trace_tipc_sk_rej_msg(sk, skb, TIPC_DUMP_ALL,
2463
+ "@sk_enqueue!");
22582464 __skb_queue_tail(xmitq, skb);
2465
+ }
22592466 break;
22602467 }
22612468 }
....@@ -2304,6 +2511,8 @@
23042511 /* Prepare for message rejection */
23052512 if (!tipc_msg_reverse(tipc_own_addr(net), &skb, err))
23062513 continue;
2514
+
2515
+ trace_tipc_sk_rej_msg(NULL, skb, TIPC_DUMP_NONE, "@sk_rcv!");
23072516 xmit:
23082517 dnode = msg_destnode(buf_msg(skb));
23092518 tipc_node_xmit_skb(net, skb, dnode, dport);
....@@ -2324,10 +2533,12 @@
23242533 return -ETIMEDOUT;
23252534 if (signal_pending(current))
23262535 return sock_intr_errno(*timeo_p);
2536
+ if (sk->sk_state == TIPC_DISCONNECTING)
2537
+ break;
23272538
23282539 add_wait_queue(sk_sleep(sk), &wait);
2329
- done = sk_wait_event(sk, timeo_p,
2330
- sk->sk_state != TIPC_CONNECTING, &wait);
2540
+ done = sk_wait_event(sk, timeo_p, tipc_sk_connected(sk),
2541
+ &wait);
23312542 remove_wait_queue(sk_sleep(sk), &wait);
23322543 } while (!done);
23332544 return 0;
....@@ -2415,7 +2626,7 @@
24152626 * case is EINPROGRESS, rather than EALREADY.
24162627 */
24172628 res = -EINPROGRESS;
2418
- /* fall thru' */
2629
+ fallthrough;
24192630 case TIPC_CONNECTING:
24202631 if (!timeout) {
24212632 if (previous == TIPC_CONNECTING)
....@@ -2492,7 +2703,7 @@
24922703 /**
24932704 * tipc_accept - wait for connection request
24942705 * @sock: listening socket
2495
- * @newsock: new socket that is to be connected
2706
+ * @new_sock: new socket that is to be connected
24962707 * @flags: file-related flags associated with socket
24972708 *
24982709 * Returns 0 on success, errno otherwise
....@@ -2501,9 +2712,10 @@
25012712 bool kern)
25022713 {
25032714 struct sock *new_sk, *sk = sock->sk;
2504
- struct sk_buff *buf;
25052715 struct tipc_sock *new_tsock;
2716
+ struct msghdr m = {NULL,};
25062717 struct tipc_msg *msg;
2718
+ struct sk_buff *buf;
25072719 long timeo;
25082720 int res;
25092721
....@@ -2536,31 +2748,29 @@
25362748 * Reject any stray messages received by new socket
25372749 * before the socket lock was taken (very, very unlikely)
25382750 */
2539
- tsk_rej_rx_queue(new_sk);
2751
+ tsk_rej_rx_queue(new_sk, TIPC_ERR_NO_PORT);
25402752
25412753 /* Connect new socket to it's peer */
25422754 tipc_sk_finish_conn(new_tsock, msg_origport(msg), msg_orignode(msg));
25432755
2544
- tsk_set_importance(new_tsock, msg_importance(msg));
2756
+ tsk_set_importance(new_sk, msg_importance(msg));
25452757 if (msg_named(msg)) {
25462758 new_tsock->conn_type = msg_nametype(msg);
25472759 new_tsock->conn_instance = msg_nameinst(msg);
25482760 }
25492761
25502762 /*
2551
- * Respond to 'SYN-' by discarding it & returning 'ACK'-.
2552
- * Respond to 'SYN+' by queuing it on new socket.
2763
+ * Respond to 'SYN-' by discarding it & returning 'ACK'.
2764
+ * Respond to 'SYN+' by queuing it on new socket & returning 'ACK'.
25532765 */
25542766 if (!msg_data_sz(msg)) {
2555
- struct msghdr m = {NULL,};
2556
-
25572767 tsk_advance_rx_queue(sk);
2558
- __tipc_sendstream(new_sock, &m, 0);
25592768 } else {
25602769 __skb_dequeue(&sk->sk_receive_queue);
25612770 __skb_queue_head(&new_sk->sk_receive_queue, buf);
25622771 skb_set_owner_r(buf, new_sk);
25632772 }
2773
+ __tipc_sendstream(new_sock, &m, 0);
25642774 release_sock(new_sk);
25652775 exit:
25662776 release_sock(sk);
....@@ -2586,6 +2796,7 @@
25862796
25872797 lock_sock(sk);
25882798
2799
+ trace_tipc_sk_shutdown(sk, NULL, TIPC_DUMP_ALL, " ");
25892800 __tipc_shutdown(sock, TIPC_CONN_SHUTDOWN);
25902801 sk->sk_shutdown = SHUTDOWN_MASK;
25912802
....@@ -2604,43 +2815,80 @@
26042815 return res;
26052816 }
26062817
2818
+static void tipc_sk_check_probing_state(struct sock *sk,
2819
+ struct sk_buff_head *list)
2820
+{
2821
+ struct tipc_sock *tsk = tipc_sk(sk);
2822
+ u32 pnode = tsk_peer_node(tsk);
2823
+ u32 pport = tsk_peer_port(tsk);
2824
+ u32 self = tsk_own_node(tsk);
2825
+ u32 oport = tsk->portid;
2826
+ struct sk_buff *skb;
2827
+
2828
+ if (tsk->probe_unacked) {
2829
+ tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2830
+ sk->sk_err = ECONNABORTED;
2831
+ tipc_node_remove_conn(sock_net(sk), pnode, pport);
2832
+ sk->sk_state_change(sk);
2833
+ return;
2834
+ }
2835
+ /* Prepare new probe */
2836
+ skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2837
+ pnode, self, pport, oport, TIPC_OK);
2838
+ if (skb)
2839
+ __skb_queue_tail(list, skb);
2840
+ tsk->probe_unacked = true;
2841
+ sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2842
+}
2843
+
2844
+static void tipc_sk_retry_connect(struct sock *sk, struct sk_buff_head *list)
2845
+{
2846
+ struct tipc_sock *tsk = tipc_sk(sk);
2847
+
2848
+ /* Try again later if dest link is congested */
2849
+ if (tsk->cong_link_cnt) {
2850
+ sk_reset_timer(sk, &sk->sk_timer,
2851
+ jiffies + msecs_to_jiffies(100));
2852
+ return;
2853
+ }
2854
+ /* Prepare SYN for retransmit */
2855
+ tipc_msg_skb_clone(&sk->sk_write_queue, list);
2856
+}
2857
+
26072858 static void tipc_sk_timeout(struct timer_list *t)
26082859 {
26092860 struct sock *sk = from_timer(sk, t, sk_timer);
26102861 struct tipc_sock *tsk = tipc_sk(sk);
2611
- u32 peer_port = tsk_peer_port(tsk);
2612
- u32 peer_node = tsk_peer_node(tsk);
2613
- u32 own_node = tsk_own_node(tsk);
2614
- u32 own_port = tsk->portid;
2615
- struct net *net = sock_net(sk);
2616
- struct sk_buff *skb = NULL;
2862
+ u32 pnode = tsk_peer_node(tsk);
2863
+ struct sk_buff_head list;
2864
+ int rc = 0;
26172865
2866
+ __skb_queue_head_init(&list);
26182867 bh_lock_sock(sk);
2619
- if (!tipc_sk_connected(sk))
2620
- goto exit;
26212868
26222869 /* Try again later if socket is busy */
26232870 if (sock_owned_by_user(sk)) {
26242871 sk_reset_timer(sk, &sk->sk_timer, jiffies + HZ / 20);
2625
- goto exit;
2872
+ bh_unlock_sock(sk);
2873
+ sock_put(sk);
2874
+ return;
26262875 }
26272876
2628
- if (tsk->probe_unacked) {
2629
- tipc_set_sk_state(sk, TIPC_DISCONNECTING);
2630
- tipc_node_remove_conn(net, peer_node, peer_port);
2631
- sk->sk_state_change(sk);
2632
- goto exit;
2633
- }
2634
- /* Send new probe */
2635
- skb = tipc_msg_create(CONN_MANAGER, CONN_PROBE, INT_H_SIZE, 0,
2636
- peer_node, own_node, peer_port, own_port,
2637
- TIPC_OK);
2638
- tsk->probe_unacked = true;
2639
- sk_reset_timer(sk, &sk->sk_timer, jiffies + CONN_PROBING_INTV);
2640
-exit:
2877
+ if (sk->sk_state == TIPC_ESTABLISHED)
2878
+ tipc_sk_check_probing_state(sk, &list);
2879
+ else if (sk->sk_state == TIPC_CONNECTING)
2880
+ tipc_sk_retry_connect(sk, &list);
2881
+
26412882 bh_unlock_sock(sk);
2642
- if (skb)
2643
- tipc_node_xmit_skb(net, skb, peer_node, own_port);
2883
+
2884
+ if (!skb_queue_empty(&list))
2885
+ rc = tipc_node_xmit(sock_net(sk), &list, pnode, tsk->portid);
2886
+
2887
+ /* SYN messages may cause link congestion */
2888
+ if (rc == -ELINKCONG) {
2889
+ tipc_dest_push(&tsk->cong_links, pnode, 0);
2890
+ tsk->cong_link_cnt = 1;
2891
+ }
26442892 sock_put(sk);
26452893 }
26462894
....@@ -2746,7 +2994,7 @@
27462994 struct tipc_sock *tsk;
27472995
27482996 rcu_read_lock();
2749
- tsk = rhashtable_lookup_fast(&tn->sk_rht, &portid, tsk_rht_params);
2997
+ tsk = rhashtable_lookup(&tn->sk_rht, &portid, tsk_rht_params);
27502998 if (tsk)
27512999 sock_hold(&tsk->sk);
27523000 rcu_read_unlock();
....@@ -2883,7 +3131,7 @@
28833131 * Returns 0 on success, errno otherwise
28843132 */
28853133 static int tipc_setsockopt(struct socket *sock, int lvl, int opt,
2886
- char __user *ov, unsigned int ol)
3134
+ sockptr_t ov, unsigned int ol)
28873135 {
28883136 struct sock *sk = sock->sk;
28893137 struct tipc_sock *tsk = tipc_sk(sk);
....@@ -2901,19 +3149,20 @@
29013149 case TIPC_SRC_DROPPABLE:
29023150 case TIPC_DEST_DROPPABLE:
29033151 case TIPC_CONN_TIMEOUT:
3152
+ case TIPC_NODELAY:
29043153 if (ol < sizeof(value))
29053154 return -EINVAL;
2906
- if (get_user(value, (u32 __user *)ov))
3155
+ if (copy_from_sockptr(&value, ov, sizeof(u32)))
29073156 return -EFAULT;
29083157 break;
29093158 case TIPC_GROUP_JOIN:
29103159 if (ol < sizeof(mreq))
29113160 return -EINVAL;
2912
- if (copy_from_user(&mreq, ov, sizeof(mreq)))
3161
+ if (copy_from_sockptr(&mreq, ov, sizeof(mreq)))
29133162 return -EFAULT;
29143163 break;
29153164 default:
2916
- if (ov || ol)
3165
+ if (!sockptr_is_null(ov) || ol)
29173166 return -EINVAL;
29183167 }
29193168
....@@ -2921,7 +3170,7 @@
29213170
29223171 switch (opt) {
29233172 case TIPC_IMPORTANCE:
2924
- res = tsk_set_importance(tsk, value);
3173
+ res = tsk_set_importance(sk, value);
29253174 break;
29263175 case TIPC_SRC_DROPPABLE:
29273176 if (sock->type != SOCK_STREAM)
....@@ -2948,6 +3197,10 @@
29483197 break;
29493198 case TIPC_GROUP_LEAVE:
29503199 res = tipc_sk_leave(tsk);
3200
+ break;
3201
+ case TIPC_NODELAY:
3202
+ tsk->nodelay = !!value;
3203
+ tsk_set_nagle(tsk);
29513204 break;
29523205 default:
29533206 res = -EINVAL;
....@@ -3010,6 +3263,9 @@
30103263 break;
30113264 case TIPC_SOCK_RECVQ_DEPTH:
30123265 value = skb_queue_len(&sk->sk_receive_queue);
3266
+ break;
3267
+ case TIPC_SOCK_RECVQ_USED:
3268
+ value = sk_rmem_alloc_get(sk);
30133269 break;
30143270 case TIPC_GROUP_JOIN:
30153271 seq.type = 0;
....@@ -3211,7 +3467,9 @@
32113467 peer_node = tsk_peer_node(tsk);
32123468 peer_port = tsk_peer_port(tsk);
32133469
3214
- nest = nla_nest_start(skb, TIPC_NLA_SOCK_CON);
3470
+ nest = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_CON);
3471
+ if (!nest)
3472
+ return -EMSGSIZE;
32153473
32163474 if (nla_put_u32(skb, TIPC_NLA_CON_NODE, peer_node))
32173475 goto msg_full;
....@@ -3268,7 +3526,7 @@
32683526 if (!hdr)
32693527 goto msg_cancel;
32703528
3271
- attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
3529
+ attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
32723530 if (!attrs)
32733531 goto genlmsg_cancel;
32743532
....@@ -3373,7 +3631,7 @@
33733631 if (!(sk_filter_state & (1 << sk->sk_state)))
33743632 return 0;
33753633
3376
- attrs = nla_nest_start(skb, TIPC_NLA_SOCK);
3634
+ attrs = nla_nest_start_noflag(skb, TIPC_NLA_SOCK);
33773635 if (!attrs)
33783636 goto msg_cancel;
33793637
....@@ -3391,7 +3649,7 @@
33913649 TIPC_NLA_SOCK_PAD))
33923650 goto attr_msg_cancel;
33933651
3394
- stat = nla_nest_start(skb, TIPC_NLA_SOCK_STAT);
3652
+ stat = nla_nest_start_noflag(skb, TIPC_NLA_SOCK_STAT);
33953653 if (!stat)
33963654 goto attr_msg_cancel;
33973655
....@@ -3448,7 +3706,7 @@
34483706 if (!hdr)
34493707 goto msg_cancel;
34503708
3451
- attrs = nla_nest_start(skb, TIPC_NLA_PUBL);
3709
+ attrs = nla_nest_start_noflag(skb, TIPC_NLA_PUBL);
34523710 if (!attrs)
34533711 goto genlmsg_cancel;
34543712
....@@ -3525,19 +3783,15 @@
35253783 struct tipc_sock *tsk;
35263784
35273785 if (!tsk_portid) {
3528
- struct nlattr **attrs;
3786
+ struct nlattr **attrs = genl_dumpit_info(cb)->attrs;
35293787 struct nlattr *sock[TIPC_NLA_SOCK_MAX + 1];
3530
-
3531
- err = tipc_nlmsg_parse(cb->nlh, &attrs);
3532
- if (err)
3533
- return err;
35343788
35353789 if (!attrs[TIPC_NLA_SOCK])
35363790 return -EINVAL;
35373791
3538
- err = nla_parse_nested(sock, TIPC_NLA_SOCK_MAX,
3539
- attrs[TIPC_NLA_SOCK],
3540
- tipc_nl_sock_policy, NULL);
3792
+ err = nla_parse_nested_deprecated(sock, TIPC_NLA_SOCK_MAX,
3793
+ attrs[TIPC_NLA_SOCK],
3794
+ tipc_nl_sock_policy, NULL);
35413795 if (err)
35423796 return err;
35433797
....@@ -3567,3 +3821,187 @@
35673821
35683822 return skb->len;
35693823 }
3824
+
3825
+/**
3826
+ * tipc_sk_filtering - check if a socket should be traced
3827
+ * @sk: the socket to be examined
3828
+ * @sysctl_tipc_sk_filter[]: the socket tuple for filtering,
3829
+ * (portid, sock type, name type, name lower, name upper)
3830
+ *
3831
+ * Returns true if the socket meets the socket tuple data
3832
+ * (value 0 = 'any') or when there is no tuple set (all = 0),
3833
+ * otherwise false
3834
+ */
3835
+bool tipc_sk_filtering(struct sock *sk)
3836
+{
3837
+ struct tipc_sock *tsk;
3838
+ struct publication *p;
3839
+ u32 _port, _sktype, _type, _lower, _upper;
3840
+ u32 type = 0, lower = 0, upper = 0;
3841
+
3842
+ if (!sk)
3843
+ return true;
3844
+
3845
+ tsk = tipc_sk(sk);
3846
+
3847
+ _port = sysctl_tipc_sk_filter[0];
3848
+ _sktype = sysctl_tipc_sk_filter[1];
3849
+ _type = sysctl_tipc_sk_filter[2];
3850
+ _lower = sysctl_tipc_sk_filter[3];
3851
+ _upper = sysctl_tipc_sk_filter[4];
3852
+
3853
+ if (!_port && !_sktype && !_type && !_lower && !_upper)
3854
+ return true;
3855
+
3856
+ if (_port)
3857
+ return (_port == tsk->portid);
3858
+
3859
+ if (_sktype && _sktype != sk->sk_type)
3860
+ return false;
3861
+
3862
+ if (tsk->published) {
3863
+ p = list_first_entry_or_null(&tsk->publications,
3864
+ struct publication, binding_sock);
3865
+ if (p) {
3866
+ type = p->type;
3867
+ lower = p->lower;
3868
+ upper = p->upper;
3869
+ }
3870
+ }
3871
+
3872
+ if (!tipc_sk_type_connectionless(sk)) {
3873
+ type = tsk->conn_type;
3874
+ lower = tsk->conn_instance;
3875
+ upper = tsk->conn_instance;
3876
+ }
3877
+
3878
+ if ((_type && _type != type) || (_lower && _lower != lower) ||
3879
+ (_upper && _upper != upper))
3880
+ return false;
3881
+
3882
+ return true;
3883
+}
3884
+
3885
+u32 tipc_sock_get_portid(struct sock *sk)
3886
+{
3887
+ return (sk) ? (tipc_sk(sk))->portid : 0;
3888
+}
3889
+
3890
+/**
3891
+ * tipc_sk_overlimit1 - check if socket rx queue is about to be overloaded,
3892
+ * both the rcv and backlog queues are considered
3893
+ * @sk: tipc sk to be checked
3894
+ * @skb: tipc msg to be checked
3895
+ *
3896
+ * Returns true if the socket rx queue allocation is > 90%, otherwise false
3897
+ */
3898
+
3899
+bool tipc_sk_overlimit1(struct sock *sk, struct sk_buff *skb)
3900
+{
3901
+ atomic_t *dcnt = &tipc_sk(sk)->dupl_rcvcnt;
3902
+ unsigned int lim = rcvbuf_limit(sk, skb) + atomic_read(dcnt);
3903
+ unsigned int qsize = sk->sk_backlog.len + sk_rmem_alloc_get(sk);
3904
+
3905
+ return (qsize > lim * 90 / 100);
3906
+}
3907
+
3908
+/**
3909
+ * tipc_sk_overlimit2 - check if socket rx queue is about to be overloaded,
3910
+ * only the rcv queue is considered
3911
+ * @sk: tipc sk to be checked
3912
+ * @skb: tipc msg to be checked
3913
+ *
3914
+ * Returns true if the socket rx queue allocation is > 90%, otherwise false
3915
+ */
3916
+
3917
+bool tipc_sk_overlimit2(struct sock *sk, struct sk_buff *skb)
3918
+{
3919
+ unsigned int lim = rcvbuf_limit(sk, skb);
3920
+ unsigned int qsize = sk_rmem_alloc_get(sk);
3921
+
3922
+ return (qsize > lim * 90 / 100);
3923
+}
3924
+
3925
+/**
3926
+ * tipc_sk_dump - dump TIPC socket
3927
+ * @sk: tipc sk to be dumped
3928
+ * @dqueues: bitmask to decide if any socket queue to be dumped?
3929
+ * - TIPC_DUMP_NONE: don't dump socket queues
3930
+ * - TIPC_DUMP_SK_SNDQ: dump socket send queue
3931
+ * - TIPC_DUMP_SK_RCVQ: dump socket rcv queue
3932
+ * - TIPC_DUMP_SK_BKLGQ: dump socket backlog queue
3933
+ * - TIPC_DUMP_ALL: dump all the socket queues above
3934
+ * @buf: returned buffer of dump data in format
3935
+ */
3936
+int tipc_sk_dump(struct sock *sk, u16 dqueues, char *buf)
3937
+{
3938
+ int i = 0;
3939
+ size_t sz = (dqueues) ? SK_LMAX : SK_LMIN;
3940
+ struct tipc_sock *tsk;
3941
+ struct publication *p;
3942
+ bool tsk_connected;
3943
+
3944
+ if (!sk) {
3945
+ i += scnprintf(buf, sz, "sk data: (null)\n");
3946
+ return i;
3947
+ }
3948
+
3949
+ tsk = tipc_sk(sk);
3950
+ tsk_connected = !tipc_sk_type_connectionless(sk);
3951
+
3952
+ i += scnprintf(buf, sz, "sk data: %u", sk->sk_type);
3953
+ i += scnprintf(buf + i, sz - i, " %d", sk->sk_state);
3954
+ i += scnprintf(buf + i, sz - i, " %x", tsk_own_node(tsk));
3955
+ i += scnprintf(buf + i, sz - i, " %u", tsk->portid);
3956
+ i += scnprintf(buf + i, sz - i, " | %u", tsk_connected);
3957
+ if (tsk_connected) {
3958
+ i += scnprintf(buf + i, sz - i, " %x", tsk_peer_node(tsk));
3959
+ i += scnprintf(buf + i, sz - i, " %u", tsk_peer_port(tsk));
3960
+ i += scnprintf(buf + i, sz - i, " %u", tsk->conn_type);
3961
+ i += scnprintf(buf + i, sz - i, " %u", tsk->conn_instance);
3962
+ }
3963
+ i += scnprintf(buf + i, sz - i, " | %u", tsk->published);
3964
+ if (tsk->published) {
3965
+ p = list_first_entry_or_null(&tsk->publications,
3966
+ struct publication, binding_sock);
3967
+ i += scnprintf(buf + i, sz - i, " %u", (p) ? p->type : 0);
3968
+ i += scnprintf(buf + i, sz - i, " %u", (p) ? p->lower : 0);
3969
+ i += scnprintf(buf + i, sz - i, " %u", (p) ? p->upper : 0);
3970
+ }
3971
+ i += scnprintf(buf + i, sz - i, " | %u", tsk->snd_win);
3972
+ i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_win);
3973
+ i += scnprintf(buf + i, sz - i, " %u", tsk->max_pkt);
3974
+ i += scnprintf(buf + i, sz - i, " %x", tsk->peer_caps);
3975
+ i += scnprintf(buf + i, sz - i, " %u", tsk->cong_link_cnt);
3976
+ i += scnprintf(buf + i, sz - i, " %u", tsk->snt_unacked);
3977
+ i += scnprintf(buf + i, sz - i, " %u", tsk->rcv_unacked);
3978
+ i += scnprintf(buf + i, sz - i, " %u", atomic_read(&tsk->dupl_rcvcnt));
3979
+ i += scnprintf(buf + i, sz - i, " %u", sk->sk_shutdown);
3980
+ i += scnprintf(buf + i, sz - i, " | %d", sk_wmem_alloc_get(sk));
3981
+ i += scnprintf(buf + i, sz - i, " %d", sk->sk_sndbuf);
3982
+ i += scnprintf(buf + i, sz - i, " | %d", sk_rmem_alloc_get(sk));
3983
+ i += scnprintf(buf + i, sz - i, " %d", sk->sk_rcvbuf);
3984
+ i += scnprintf(buf + i, sz - i, " | %d\n", READ_ONCE(sk->sk_backlog.len));
3985
+
3986
+ if (dqueues & TIPC_DUMP_SK_SNDQ) {
3987
+ i += scnprintf(buf + i, sz - i, "sk_write_queue: ");
3988
+ i += tipc_list_dump(&sk->sk_write_queue, false, buf + i);
3989
+ }
3990
+
3991
+ if (dqueues & TIPC_DUMP_SK_RCVQ) {
3992
+ i += scnprintf(buf + i, sz - i, "sk_receive_queue: ");
3993
+ i += tipc_list_dump(&sk->sk_receive_queue, false, buf + i);
3994
+ }
3995
+
3996
+ if (dqueues & TIPC_DUMP_SK_BKLGQ) {
3997
+ i += scnprintf(buf + i, sz - i, "sk_backlog:\n head ");
3998
+ i += tipc_skb_dump(sk->sk_backlog.head, false, buf + i);
3999
+ if (sk->sk_backlog.tail != sk->sk_backlog.head) {
4000
+ i += scnprintf(buf + i, sz - i, " tail ");
4001
+ i += tipc_skb_dump(sk->sk_backlog.tail, false,
4002
+ buf + i);
4003
+ }
4004
+ }
4005
+
4006
+ return i;
4007
+}