]> git.proxmox.com Git - mirror_ubuntu-bionic-kernel.git/blob - net/tipc/port.c
tipc: use pseudo message to wake up sockets after link congestion
[mirror_ubuntu-bionic-kernel.git] / net / tipc / port.c
1 /*
2 * net/tipc/port.c: TIPC port code
3 *
4 * Copyright (c) 1992-2007, 2014, Ericsson AB
5 * Copyright (c) 2004-2008, 2010-2013, Wind River Systems
6 * All rights reserved.
7 *
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
10 *
11 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
19 *
20 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
34 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37 #include "core.h"
38 #include "config.h"
39 #include "port.h"
40 #include "name_table.h"
41 #include "socket.h"
42
43 /* Connection management: */
44 #define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
45
46 #define MAX_REJECT_SIZE 1024
47
48 DEFINE_SPINLOCK(tipc_port_list_lock);
49
50 static LIST_HEAD(ports);
51 static void port_handle_node_down(unsigned long ref);
52 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
53 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
54 static void port_timeout(unsigned long ref);
55
56 /**
57 * tipc_port_peer_msg - verify message was sent by connected port's peer
58 *
59 * Handles cases where the node's network address has changed from
60 * the default of <0.0.0> to its configured setting.
61 */
62 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
63 {
64 u32 peernode;
65 u32 orignode;
66
67 if (msg_origport(msg) != tipc_port_peerport(p_ptr))
68 return 0;
69
70 orignode = msg_orignode(msg);
71 peernode = tipc_port_peernode(p_ptr);
72 return (orignode == peernode) ||
73 (!orignode && (peernode == tipc_own_addr)) ||
74 (!peernode && (orignode == tipc_own_addr));
75 }
76
77 /* tipc_port_init - intiate TIPC port and lock it
78 *
79 * Returns obtained reference if initialization is successful, zero otherwise
80 */
81 u32 tipc_port_init(struct tipc_port *p_ptr,
82 const unsigned int importance)
83 {
84 struct tipc_msg *msg;
85 u32 ref;
86
87 ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
88 if (!ref) {
89 pr_warn("Port registration failed, ref. table exhausted\n");
90 return 0;
91 }
92
93 p_ptr->max_pkt = MAX_PKT_DEFAULT;
94 p_ptr->ref = ref;
95 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
96 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
97 INIT_LIST_HEAD(&p_ptr->publications);
98 INIT_LIST_HEAD(&p_ptr->port_list);
99
100 /*
101 * Must hold port list lock while initializing message header template
102 * to ensure a change to node's own network address doesn't result
103 * in template containing out-dated network address information
104 */
105 spin_lock_bh(&tipc_port_list_lock);
106 msg = &p_ptr->phdr;
107 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
108 msg_set_origport(msg, ref);
109 list_add_tail(&p_ptr->port_list, &ports);
110 spin_unlock_bh(&tipc_port_list_lock);
111 return ref;
112 }
113
114 void tipc_port_destroy(struct tipc_port *p_ptr)
115 {
116 struct sk_buff *buf = NULL;
117 struct tipc_msg *msg = NULL;
118 u32 peer;
119
120 tipc_withdraw(p_ptr, 0, NULL);
121
122 spin_lock_bh(p_ptr->lock);
123 tipc_ref_discard(p_ptr->ref);
124 spin_unlock_bh(p_ptr->lock);
125
126 k_cancel_timer(&p_ptr->timer);
127 if (p_ptr->connected) {
128 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
129 tipc_nodesub_unsubscribe(&p_ptr->subscription);
130 msg = buf_msg(buf);
131 peer = msg_destnode(msg);
132 tipc_link_xmit(buf, peer, msg_link_selector(msg));
133 }
134 spin_lock_bh(&tipc_port_list_lock);
135 list_del(&p_ptr->port_list);
136 spin_unlock_bh(&tipc_port_list_lock);
137 k_term_timer(&p_ptr->timer);
138 }
139
140 /*
141 * port_build_proto_msg(): create connection protocol message for port
142 *
143 * On entry the port must be locked and connected.
144 */
145 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
146 u32 type, u32 ack)
147 {
148 struct sk_buff *buf;
149 struct tipc_msg *msg;
150
151 buf = tipc_buf_acquire(INT_H_SIZE);
152 if (buf) {
153 msg = buf_msg(buf);
154 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
155 tipc_port_peernode(p_ptr));
156 msg_set_destport(msg, tipc_port_peerport(p_ptr));
157 msg_set_origport(msg, p_ptr->ref);
158 msg_set_msgcnt(msg, ack);
159 buf->next = NULL;
160 }
161 return buf;
162 }
163
164 static void port_timeout(unsigned long ref)
165 {
166 struct tipc_port *p_ptr = tipc_port_lock(ref);
167 struct sk_buff *buf = NULL;
168 struct tipc_msg *msg = NULL;
169
170 if (!p_ptr)
171 return;
172
173 if (!p_ptr->connected) {
174 tipc_port_unlock(p_ptr);
175 return;
176 }
177
178 /* Last probe answered ? */
179 if (p_ptr->probing_state == TIPC_CONN_PROBING) {
180 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
181 } else {
182 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
183 p_ptr->probing_state = TIPC_CONN_PROBING;
184 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
185 }
186 tipc_port_unlock(p_ptr);
187 msg = buf_msg(buf);
188 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
189 }
190
191
192 static void port_handle_node_down(unsigned long ref)
193 {
194 struct tipc_port *p_ptr = tipc_port_lock(ref);
195 struct sk_buff *buf = NULL;
196 struct tipc_msg *msg = NULL;
197
198 if (!p_ptr)
199 return;
200 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
201 tipc_port_unlock(p_ptr);
202 msg = buf_msg(buf);
203 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
204 }
205
206
207 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
208 {
209 struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
210
211 if (buf) {
212 struct tipc_msg *msg = buf_msg(buf);
213 msg_swap_words(msg, 4, 5);
214 msg_swap_words(msg, 6, 7);
215 buf->next = NULL;
216 }
217 return buf;
218 }
219
220
221 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
222 {
223 struct sk_buff *buf;
224 struct tipc_msg *msg;
225 u32 imp;
226
227 if (!p_ptr->connected)
228 return NULL;
229
230 buf = tipc_buf_acquire(BASIC_H_SIZE);
231 if (buf) {
232 msg = buf_msg(buf);
233 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
234 msg_set_hdr_sz(msg, BASIC_H_SIZE);
235 msg_set_size(msg, BASIC_H_SIZE);
236 imp = msg_importance(msg);
237 if (imp < TIPC_CRITICAL_IMPORTANCE)
238 msg_set_importance(msg, ++imp);
239 msg_set_errcode(msg, err);
240 buf->next = NULL;
241 }
242 return buf;
243 }
244
245 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
246 {
247 struct publication *publ;
248 int ret;
249
250 if (full_id)
251 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
252 tipc_zone(tipc_own_addr),
253 tipc_cluster(tipc_own_addr),
254 tipc_node(tipc_own_addr), p_ptr->ref);
255 else
256 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
257
258 if (p_ptr->connected) {
259 u32 dport = tipc_port_peerport(p_ptr);
260 u32 destnode = tipc_port_peernode(p_ptr);
261
262 ret += tipc_snprintf(buf + ret, len - ret,
263 " connected to <%u.%u.%u:%u>",
264 tipc_zone(destnode),
265 tipc_cluster(destnode),
266 tipc_node(destnode), dport);
267 if (p_ptr->conn_type != 0)
268 ret += tipc_snprintf(buf + ret, len - ret,
269 " via {%u,%u}", p_ptr->conn_type,
270 p_ptr->conn_instance);
271 } else if (p_ptr->published) {
272 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
273 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
274 if (publ->lower == publ->upper)
275 ret += tipc_snprintf(buf + ret, len - ret,
276 " {%u,%u}", publ->type,
277 publ->lower);
278 else
279 ret += tipc_snprintf(buf + ret, len - ret,
280 " {%u,%u,%u}", publ->type,
281 publ->lower, publ->upper);
282 }
283 }
284 ret += tipc_snprintf(buf + ret, len - ret, "\n");
285 return ret;
286 }
287
288 struct sk_buff *tipc_port_get_ports(void)
289 {
290 struct sk_buff *buf;
291 struct tlv_desc *rep_tlv;
292 char *pb;
293 int pb_len;
294 struct tipc_port *p_ptr;
295 int str_len = 0;
296
297 buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
298 if (!buf)
299 return NULL;
300 rep_tlv = (struct tlv_desc *)buf->data;
301 pb = TLV_DATA(rep_tlv);
302 pb_len = ULTRA_STRING_MAX_LEN;
303
304 spin_lock_bh(&tipc_port_list_lock);
305 list_for_each_entry(p_ptr, &ports, port_list) {
306 spin_lock_bh(p_ptr->lock);
307 str_len += port_print(p_ptr, pb, pb_len, 0);
308 spin_unlock_bh(p_ptr->lock);
309 }
310 spin_unlock_bh(&tipc_port_list_lock);
311 str_len += 1; /* for "\0" */
312 skb_put(buf, TLV_SPACE(str_len));
313 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
314
315 return buf;
316 }
317
318 void tipc_port_reinit(void)
319 {
320 struct tipc_port *p_ptr;
321 struct tipc_msg *msg;
322
323 spin_lock_bh(&tipc_port_list_lock);
324 list_for_each_entry(p_ptr, &ports, port_list) {
325 msg = &p_ptr->phdr;
326 msg_set_prevnode(msg, tipc_own_addr);
327 msg_set_orignode(msg, tipc_own_addr);
328 }
329 spin_unlock_bh(&tipc_port_list_lock);
330 }
331
332 void tipc_acknowledge(u32 ref, u32 ack)
333 {
334 struct tipc_port *p_ptr;
335 struct sk_buff *buf = NULL;
336 struct tipc_msg *msg;
337
338 p_ptr = tipc_port_lock(ref);
339 if (!p_ptr)
340 return;
341 if (p_ptr->connected)
342 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
343
344 tipc_port_unlock(p_ptr);
345 if (!buf)
346 return;
347 msg = buf_msg(buf);
348 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
349 }
350
351 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
352 struct tipc_name_seq const *seq)
353 {
354 struct publication *publ;
355 u32 key;
356
357 if (p_ptr->connected)
358 return -EINVAL;
359 key = p_ptr->ref + p_ptr->pub_count + 1;
360 if (key == p_ptr->ref)
361 return -EADDRINUSE;
362
363 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
364 scope, p_ptr->ref, key);
365 if (publ) {
366 list_add(&publ->pport_list, &p_ptr->publications);
367 p_ptr->pub_count++;
368 p_ptr->published = 1;
369 return 0;
370 }
371 return -EINVAL;
372 }
373
374 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
375 struct tipc_name_seq const *seq)
376 {
377 struct publication *publ;
378 struct publication *tpubl;
379 int res = -EINVAL;
380
381 if (!seq) {
382 list_for_each_entry_safe(publ, tpubl,
383 &p_ptr->publications, pport_list) {
384 tipc_nametbl_withdraw(publ->type, publ->lower,
385 publ->ref, publ->key);
386 }
387 res = 0;
388 } else {
389 list_for_each_entry_safe(publ, tpubl,
390 &p_ptr->publications, pport_list) {
391 if (publ->scope != scope)
392 continue;
393 if (publ->type != seq->type)
394 continue;
395 if (publ->lower != seq->lower)
396 continue;
397 if (publ->upper != seq->upper)
398 break;
399 tipc_nametbl_withdraw(publ->type, publ->lower,
400 publ->ref, publ->key);
401 res = 0;
402 break;
403 }
404 }
405 if (list_empty(&p_ptr->publications))
406 p_ptr->published = 0;
407 return res;
408 }
409
410 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
411 {
412 struct tipc_port *p_ptr;
413 int res;
414
415 p_ptr = tipc_port_lock(ref);
416 if (!p_ptr)
417 return -EINVAL;
418 res = __tipc_port_connect(ref, p_ptr, peer);
419 tipc_port_unlock(p_ptr);
420 return res;
421 }
422
423 /*
424 * __tipc_port_connect - connect to a remote peer
425 *
426 * Port must be locked.
427 */
428 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
429 struct tipc_portid const *peer)
430 {
431 struct tipc_msg *msg;
432 int res = -EINVAL;
433
434 if (p_ptr->published || p_ptr->connected)
435 goto exit;
436 if (!peer->ref)
437 goto exit;
438
439 msg = &p_ptr->phdr;
440 msg_set_destnode(msg, peer->node);
441 msg_set_destport(msg, peer->ref);
442 msg_set_type(msg, TIPC_CONN_MSG);
443 msg_set_lookup_scope(msg, 0);
444 msg_set_hdr_sz(msg, SHORT_H_SIZE);
445
446 p_ptr->probing_interval = PROBING_INTERVAL;
447 p_ptr->probing_state = TIPC_CONN_OK;
448 p_ptr->connected = 1;
449 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
450
451 tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
452 (void *)(unsigned long)ref,
453 (net_ev_handler)port_handle_node_down);
454 res = 0;
455 exit:
456 p_ptr->max_pkt = tipc_node_get_mtu(peer->node, ref);
457 return res;
458 }
459
460 /*
461 * __tipc_disconnect - disconnect port from peer
462 *
463 * Port must be locked.
464 */
465 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
466 {
467 if (tp_ptr->connected) {
468 tp_ptr->connected = 0;
469 /* let timer expire on it's own to avoid deadlock! */
470 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
471 return 0;
472 }
473
474 return -ENOTCONN;
475 }
476
477 /*
478 * tipc_port_disconnect(): Disconnect port form peer.
479 * This is a node local operation.
480 */
481 int tipc_port_disconnect(u32 ref)
482 {
483 struct tipc_port *p_ptr;
484 int res;
485
486 p_ptr = tipc_port_lock(ref);
487 if (!p_ptr)
488 return -EINVAL;
489 res = __tipc_port_disconnect(p_ptr);
490 tipc_port_unlock(p_ptr);
491 return res;
492 }
493
494 /*
495 * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
496 */
497 int tipc_port_shutdown(u32 ref)
498 {
499 struct tipc_msg *msg;
500 struct tipc_port *p_ptr;
501 struct sk_buff *buf = NULL;
502
503 p_ptr = tipc_port_lock(ref);
504 if (!p_ptr)
505 return -EINVAL;
506
507 buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
508 tipc_port_unlock(p_ptr);
509 msg = buf_msg(buf);
510 tipc_link_xmit(buf, msg_destnode(msg), msg_link_selector(msg));
511 return tipc_port_disconnect(ref);
512 }