]> Git Repo - linux.git/blob - net/tipc/port.c
tipc: eliminate redundant locking
[linux.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 #define CONFIRMED 0
46 #define PROBING 1
47
48 #define MAX_REJECT_SIZE 1024
49
50 DEFINE_SPINLOCK(tipc_port_list_lock);
51
52 static LIST_HEAD(ports);
53 static void port_handle_node_down(unsigned long ref);
54 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
55 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
56 static void port_timeout(unsigned long ref);
57
58 /**
59  * tipc_port_peer_msg - verify message was sent by connected port's peer
60  *
61  * Handles cases where the node's network address has changed from
62  * the default of <0.0.0> to its configured setting.
63  */
64 int tipc_port_peer_msg(struct tipc_port *p_ptr, struct tipc_msg *msg)
65 {
66         u32 peernode;
67         u32 orignode;
68
69         if (msg_origport(msg) != tipc_port_peerport(p_ptr))
70                 return 0;
71
72         orignode = msg_orignode(msg);
73         peernode = tipc_port_peernode(p_ptr);
74         return (orignode == peernode) ||
75                 (!orignode && (peernode == tipc_own_addr)) ||
76                 (!peernode && (orignode == tipc_own_addr));
77 }
78
79 /**
80  * tipc_port_mcast_xmit - send a multicast message to local and remote
81  * destinations
82  */
83 int tipc_port_mcast_xmit(u32 ref, struct tipc_name_seq const *seq,
84                          struct iovec const *msg_sect, unsigned int len)
85 {
86         struct tipc_msg *hdr;
87         struct sk_buff *buf;
88         struct sk_buff *ibuf = NULL;
89         struct tipc_port_list dports = {0, NULL, };
90         struct tipc_port *oport = tipc_port_deref(ref);
91         int ext_targets;
92         int res;
93
94         if (unlikely(!oport))
95                 return -EINVAL;
96
97         /* Create multicast message */
98         hdr = &oport->phdr;
99         msg_set_type(hdr, TIPC_MCAST_MSG);
100         msg_set_lookup_scope(hdr, TIPC_CLUSTER_SCOPE);
101         msg_set_destport(hdr, 0);
102         msg_set_destnode(hdr, 0);
103         msg_set_nametype(hdr, seq->type);
104         msg_set_namelower(hdr, seq->lower);
105         msg_set_nameupper(hdr, seq->upper);
106         msg_set_hdr_sz(hdr, MCAST_H_SIZE);
107         res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
108         if (unlikely(!buf))
109                 return res;
110
111         /* Figure out where to send multicast message */
112         ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
113                                                 TIPC_NODE_SCOPE, &dports);
114
115         /* Send message to destinations (duplicate it only if necessary) */
116         if (ext_targets) {
117                 if (dports.count != 0) {
118                         ibuf = skb_copy(buf, GFP_ATOMIC);
119                         if (ibuf == NULL) {
120                                 tipc_port_list_free(&dports);
121                                 kfree_skb(buf);
122                                 return -ENOMEM;
123                         }
124                 }
125                 res = tipc_bclink_xmit(buf);
126                 if ((res < 0) && (dports.count != 0))
127                         kfree_skb(ibuf);
128         } else {
129                 ibuf = buf;
130         }
131
132         if (res >= 0) {
133                 if (ibuf)
134                         tipc_port_mcast_rcv(ibuf, &dports);
135         } else {
136                 tipc_port_list_free(&dports);
137         }
138         return res;
139 }
140
141 /**
142  * tipc_port_mcast_rcv - deliver multicast message to all destination ports
143  *
144  * If there is no port list, perform a lookup to create one
145  */
146 void tipc_port_mcast_rcv(struct sk_buff *buf, struct tipc_port_list *dp)
147 {
148         struct tipc_msg *msg;
149         struct tipc_port_list dports = {0, NULL, };
150         struct tipc_port_list *item = dp;
151         int cnt = 0;
152
153         msg = buf_msg(buf);
154
155         /* Create destination port list, if one wasn't supplied */
156         if (dp == NULL) {
157                 tipc_nametbl_mc_translate(msg_nametype(msg),
158                                      msg_namelower(msg),
159                                      msg_nameupper(msg),
160                                      TIPC_CLUSTER_SCOPE,
161                                      &dports);
162                 item = dp = &dports;
163         }
164
165         /* Deliver a copy of message to each destination port */
166         if (dp->count != 0) {
167                 msg_set_destnode(msg, tipc_own_addr);
168                 if (dp->count == 1) {
169                         msg_set_destport(msg, dp->ports[0]);
170                         tipc_port_rcv(buf);
171                         tipc_port_list_free(dp);
172                         return;
173                 }
174                 for (; cnt < dp->count; cnt++) {
175                         int index = cnt % PLSIZE;
176                         struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
177
178                         if (b == NULL) {
179                                 pr_warn("Unable to deliver multicast message(s)\n");
180                                 goto exit;
181                         }
182                         if ((index == 0) && (cnt != 0))
183                                 item = item->next;
184                         msg_set_destport(buf_msg(b), item->ports[index]);
185                         tipc_port_rcv(b);
186                 }
187         }
188 exit:
189         kfree_skb(buf);
190         tipc_port_list_free(dp);
191 }
192
193
194 void tipc_port_wakeup(struct tipc_port *port)
195 {
196         tipc_sk_wakeup(tipc_port_to_sk(port));
197 }
198
199 /* tipc_port_init - intiate TIPC port and lock it
200  *
201  * Returns obtained reference if initialization is successful, zero otherwise
202  */
203 u32 tipc_port_init(struct tipc_port *p_ptr,
204                    const unsigned int importance)
205 {
206         struct tipc_msg *msg;
207         u32 ref;
208
209         ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
210         if (!ref) {
211                 pr_warn("Port registration failed, ref. table exhausted\n");
212                 return 0;
213         }
214
215         p_ptr->max_pkt = MAX_PKT_DEFAULT;
216         p_ptr->ref = ref;
217         INIT_LIST_HEAD(&p_ptr->wait_list);
218         INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
219         k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
220         INIT_LIST_HEAD(&p_ptr->publications);
221         INIT_LIST_HEAD(&p_ptr->port_list);
222
223         /*
224          * Must hold port list lock while initializing message header template
225          * to ensure a change to node's own network address doesn't result
226          * in template containing out-dated network address information
227          */
228         spin_lock_bh(&tipc_port_list_lock);
229         msg = &p_ptr->phdr;
230         tipc_msg_init(msg, importance, TIPC_NAMED_MSG, NAMED_H_SIZE, 0);
231         msg_set_origport(msg, ref);
232         list_add_tail(&p_ptr->port_list, &ports);
233         spin_unlock_bh(&tipc_port_list_lock);
234         return ref;
235 }
236
237 void tipc_port_destroy(struct tipc_port *p_ptr)
238 {
239         struct sk_buff *buf = NULL;
240
241         tipc_withdraw(p_ptr, 0, NULL);
242
243         spin_lock_bh(p_ptr->lock);
244         tipc_ref_discard(p_ptr->ref);
245         spin_unlock_bh(p_ptr->lock);
246
247         k_cancel_timer(&p_ptr->timer);
248         if (p_ptr->connected) {
249                 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
250                 tipc_nodesub_unsubscribe(&p_ptr->subscription);
251         }
252
253         spin_lock_bh(&tipc_port_list_lock);
254         list_del(&p_ptr->port_list);
255         list_del(&p_ptr->wait_list);
256         spin_unlock_bh(&tipc_port_list_lock);
257         k_term_timer(&p_ptr->timer);
258         tipc_net_route_msg(buf);
259 }
260
261 /*
262  * port_build_proto_msg(): create connection protocol message for port
263  *
264  * On entry the port must be locked and connected.
265  */
266 static struct sk_buff *port_build_proto_msg(struct tipc_port *p_ptr,
267                                             u32 type, u32 ack)
268 {
269         struct sk_buff *buf;
270         struct tipc_msg *msg;
271
272         buf = tipc_buf_acquire(INT_H_SIZE);
273         if (buf) {
274                 msg = buf_msg(buf);
275                 tipc_msg_init(msg, CONN_MANAGER, type, INT_H_SIZE,
276                               tipc_port_peernode(p_ptr));
277                 msg_set_destport(msg, tipc_port_peerport(p_ptr));
278                 msg_set_origport(msg, p_ptr->ref);
279                 msg_set_msgcnt(msg, ack);
280         }
281         return buf;
282 }
283
284 int tipc_reject_msg(struct sk_buff *buf, u32 err)
285 {
286         struct tipc_msg *msg = buf_msg(buf);
287         struct sk_buff *rbuf;
288         struct tipc_msg *rmsg;
289         int hdr_sz;
290         u32 imp;
291         u32 data_sz = msg_data_sz(msg);
292         u32 src_node;
293         u32 rmsg_sz;
294
295         /* discard rejected message if it shouldn't be returned to sender */
296         if (WARN(!msg_isdata(msg),
297                  "attempt to reject message with user=%u", msg_user(msg))) {
298                 dump_stack();
299                 goto exit;
300         }
301         if (msg_errcode(msg) || msg_dest_droppable(msg))
302                 goto exit;
303
304         /*
305          * construct returned message by copying rejected message header and
306          * data (or subset), then updating header fields that need adjusting
307          */
308         hdr_sz = msg_hdr_sz(msg);
309         rmsg_sz = hdr_sz + min_t(u32, data_sz, MAX_REJECT_SIZE);
310
311         rbuf = tipc_buf_acquire(rmsg_sz);
312         if (rbuf == NULL)
313                 goto exit;
314
315         rmsg = buf_msg(rbuf);
316         skb_copy_to_linear_data(rbuf, msg, rmsg_sz);
317
318         if (msg_connected(rmsg)) {
319                 imp = msg_importance(rmsg);
320                 if (imp < TIPC_CRITICAL_IMPORTANCE)
321                         msg_set_importance(rmsg, ++imp);
322         }
323         msg_set_non_seq(rmsg, 0);
324         msg_set_size(rmsg, rmsg_sz);
325         msg_set_errcode(rmsg, err);
326         msg_set_prevnode(rmsg, tipc_own_addr);
327         msg_swap_words(rmsg, 4, 5);
328         if (!msg_short(rmsg))
329                 msg_swap_words(rmsg, 6, 7);
330
331         /* send self-abort message when rejecting on a connected port */
332         if (msg_connected(msg)) {
333                 struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
334
335                 if (p_ptr) {
336                         struct sk_buff *abuf = NULL;
337
338                         if (p_ptr->connected)
339                                 abuf = port_build_self_abort_msg(p_ptr, err);
340                         tipc_port_unlock(p_ptr);
341                         tipc_net_route_msg(abuf);
342                 }
343         }
344
345         /* send returned message & dispose of rejected message */
346         src_node = msg_prevnode(msg);
347         if (in_own_node(src_node))
348                 tipc_port_rcv(rbuf);
349         else
350                 tipc_link_xmit(rbuf, src_node, msg_link_selector(rmsg));
351 exit:
352         kfree_skb(buf);
353         return data_sz;
354 }
355
356 int tipc_port_iovec_reject(struct tipc_port *p_ptr, struct tipc_msg *hdr,
357                            struct iovec const *msg_sect, unsigned int len,
358                            int err)
359 {
360         struct sk_buff *buf;
361         int res;
362
363         res = tipc_msg_build(hdr, msg_sect, len, MAX_MSG_SIZE, &buf);
364         if (!buf)
365                 return res;
366
367         return tipc_reject_msg(buf, err);
368 }
369
370 static void port_timeout(unsigned long ref)
371 {
372         struct tipc_port *p_ptr = tipc_port_lock(ref);
373         struct sk_buff *buf = NULL;
374
375         if (!p_ptr)
376                 return;
377
378         if (!p_ptr->connected) {
379                 tipc_port_unlock(p_ptr);
380                 return;
381         }
382
383         /* Last probe answered ? */
384         if (p_ptr->probing_state == PROBING) {
385                 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
386         } else {
387                 buf = port_build_proto_msg(p_ptr, CONN_PROBE, 0);
388                 p_ptr->probing_state = PROBING;
389                 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
390         }
391         tipc_port_unlock(p_ptr);
392         tipc_net_route_msg(buf);
393 }
394
395
396 static void port_handle_node_down(unsigned long ref)
397 {
398         struct tipc_port *p_ptr = tipc_port_lock(ref);
399         struct sk_buff *buf = NULL;
400
401         if (!p_ptr)
402                 return;
403         buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
404         tipc_port_unlock(p_ptr);
405         tipc_net_route_msg(buf);
406 }
407
408
409 static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
410 {
411         struct sk_buff *buf = port_build_peer_abort_msg(p_ptr, err);
412
413         if (buf) {
414                 struct tipc_msg *msg = buf_msg(buf);
415                 msg_swap_words(msg, 4, 5);
416                 msg_swap_words(msg, 6, 7);
417         }
418         return buf;
419 }
420
421
422 static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
423 {
424         struct sk_buff *buf;
425         struct tipc_msg *msg;
426         u32 imp;
427
428         if (!p_ptr->connected)
429                 return NULL;
430
431         buf = tipc_buf_acquire(BASIC_H_SIZE);
432         if (buf) {
433                 msg = buf_msg(buf);
434                 memcpy(msg, &p_ptr->phdr, BASIC_H_SIZE);
435                 msg_set_hdr_sz(msg, BASIC_H_SIZE);
436                 msg_set_size(msg, BASIC_H_SIZE);
437                 imp = msg_importance(msg);
438                 if (imp < TIPC_CRITICAL_IMPORTANCE)
439                         msg_set_importance(msg, ++imp);
440                 msg_set_errcode(msg, err);
441         }
442         return buf;
443 }
444
445 void tipc_port_proto_rcv(struct sk_buff *buf)
446 {
447         struct tipc_msg *msg = buf_msg(buf);
448         struct tipc_port *p_ptr;
449         struct sk_buff *r_buf = NULL;
450         u32 destport = msg_destport(msg);
451         int wakeable;
452
453         /* Validate connection */
454         p_ptr = tipc_port_lock(destport);
455         if (!p_ptr || !p_ptr->connected || !tipc_port_peer_msg(p_ptr, msg)) {
456                 r_buf = tipc_buf_acquire(BASIC_H_SIZE);
457                 if (r_buf) {
458                         msg = buf_msg(r_buf);
459                         tipc_msg_init(msg, TIPC_HIGH_IMPORTANCE, TIPC_CONN_MSG,
460                                       BASIC_H_SIZE, msg_orignode(msg));
461                         msg_set_errcode(msg, TIPC_ERR_NO_PORT);
462                         msg_set_origport(msg, destport);
463                         msg_set_destport(msg, msg_origport(msg));
464                 }
465                 if (p_ptr)
466                         tipc_port_unlock(p_ptr);
467                 goto exit;
468         }
469
470         /* Process protocol message sent by peer */
471         switch (msg_type(msg)) {
472         case CONN_ACK:
473                 wakeable = tipc_port_congested(p_ptr) && p_ptr->congested;
474                 p_ptr->acked += msg_msgcnt(msg);
475                 if (!tipc_port_congested(p_ptr)) {
476                         p_ptr->congested = 0;
477                         if (wakeable)
478                                 tipc_port_wakeup(p_ptr);
479                 }
480                 break;
481         case CONN_PROBE:
482                 r_buf = port_build_proto_msg(p_ptr, CONN_PROBE_REPLY, 0);
483                 break;
484         default:
485                 /* CONN_PROBE_REPLY or unrecognized - no action required */
486                 break;
487         }
488         p_ptr->probing_state = CONFIRMED;
489         tipc_port_unlock(p_ptr);
490 exit:
491         tipc_net_route_msg(r_buf);
492         kfree_skb(buf);
493 }
494
495 static int port_print(struct tipc_port *p_ptr, char *buf, int len, int full_id)
496 {
497         struct publication *publ;
498         int ret;
499
500         if (full_id)
501                 ret = tipc_snprintf(buf, len, "<%u.%u.%u:%u>:",
502                                     tipc_zone(tipc_own_addr),
503                                     tipc_cluster(tipc_own_addr),
504                                     tipc_node(tipc_own_addr), p_ptr->ref);
505         else
506                 ret = tipc_snprintf(buf, len, "%-10u:", p_ptr->ref);
507
508         if (p_ptr->connected) {
509                 u32 dport = tipc_port_peerport(p_ptr);
510                 u32 destnode = tipc_port_peernode(p_ptr);
511
512                 ret += tipc_snprintf(buf + ret, len - ret,
513                                      " connected to <%u.%u.%u:%u>",
514                                      tipc_zone(destnode),
515                                      tipc_cluster(destnode),
516                                      tipc_node(destnode), dport);
517                 if (p_ptr->conn_type != 0)
518                         ret += tipc_snprintf(buf + ret, len - ret,
519                                              " via {%u,%u}", p_ptr->conn_type,
520                                              p_ptr->conn_instance);
521         } else if (p_ptr->published) {
522                 ret += tipc_snprintf(buf + ret, len - ret, " bound to");
523                 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
524                         if (publ->lower == publ->upper)
525                                 ret += tipc_snprintf(buf + ret, len - ret,
526                                                      " {%u,%u}", publ->type,
527                                                      publ->lower);
528                         else
529                                 ret += tipc_snprintf(buf + ret, len - ret,
530                                                      " {%u,%u,%u}", publ->type,
531                                                      publ->lower, publ->upper);
532                 }
533         }
534         ret += tipc_snprintf(buf + ret, len - ret, "\n");
535         return ret;
536 }
537
538 struct sk_buff *tipc_port_get_ports(void)
539 {
540         struct sk_buff *buf;
541         struct tlv_desc *rep_tlv;
542         char *pb;
543         int pb_len;
544         struct tipc_port *p_ptr;
545         int str_len = 0;
546
547         buf = tipc_cfg_reply_alloc(TLV_SPACE(ULTRA_STRING_MAX_LEN));
548         if (!buf)
549                 return NULL;
550         rep_tlv = (struct tlv_desc *)buf->data;
551         pb = TLV_DATA(rep_tlv);
552         pb_len = ULTRA_STRING_MAX_LEN;
553
554         spin_lock_bh(&tipc_port_list_lock);
555         list_for_each_entry(p_ptr, &ports, port_list) {
556                 spin_lock_bh(p_ptr->lock);
557                 str_len += port_print(p_ptr, pb, pb_len, 0);
558                 spin_unlock_bh(p_ptr->lock);
559         }
560         spin_unlock_bh(&tipc_port_list_lock);
561         str_len += 1;   /* for "\0" */
562         skb_put(buf, TLV_SPACE(str_len));
563         TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
564
565         return buf;
566 }
567
568 void tipc_port_reinit(void)
569 {
570         struct tipc_port *p_ptr;
571         struct tipc_msg *msg;
572
573         spin_lock_bh(&tipc_port_list_lock);
574         list_for_each_entry(p_ptr, &ports, port_list) {
575                 msg = &p_ptr->phdr;
576                 msg_set_prevnode(msg, tipc_own_addr);
577                 msg_set_orignode(msg, tipc_own_addr);
578         }
579         spin_unlock_bh(&tipc_port_list_lock);
580 }
581
582 void tipc_acknowledge(u32 ref, u32 ack)
583 {
584         struct tipc_port *p_ptr;
585         struct sk_buff *buf = NULL;
586
587         p_ptr = tipc_port_lock(ref);
588         if (!p_ptr)
589                 return;
590         if (p_ptr->connected) {
591                 p_ptr->conn_unacked -= ack;
592                 buf = port_build_proto_msg(p_ptr, CONN_ACK, ack);
593         }
594         tipc_port_unlock(p_ptr);
595         tipc_net_route_msg(buf);
596 }
597
598 int tipc_publish(struct tipc_port *p_ptr, unsigned int scope,
599                  struct tipc_name_seq const *seq)
600 {
601         struct publication *publ;
602         u32 key;
603
604         if (p_ptr->connected)
605                 return -EINVAL;
606         key = p_ptr->ref + p_ptr->pub_count + 1;
607         if (key == p_ptr->ref)
608                 return -EADDRINUSE;
609
610         publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
611                                     scope, p_ptr->ref, key);
612         if (publ) {
613                 list_add(&publ->pport_list, &p_ptr->publications);
614                 p_ptr->pub_count++;
615                 p_ptr->published = 1;
616                 return 0;
617         }
618         return -EINVAL;
619 }
620
621 int tipc_withdraw(struct tipc_port *p_ptr, unsigned int scope,
622                   struct tipc_name_seq const *seq)
623 {
624         struct publication *publ;
625         struct publication *tpubl;
626         int res = -EINVAL;
627
628         if (!seq) {
629                 list_for_each_entry_safe(publ, tpubl,
630                                          &p_ptr->publications, pport_list) {
631                         tipc_nametbl_withdraw(publ->type, publ->lower,
632                                               publ->ref, publ->key);
633                 }
634                 res = 0;
635         } else {
636                 list_for_each_entry_safe(publ, tpubl,
637                                          &p_ptr->publications, pport_list) {
638                         if (publ->scope != scope)
639                                 continue;
640                         if (publ->type != seq->type)
641                                 continue;
642                         if (publ->lower != seq->lower)
643                                 continue;
644                         if (publ->upper != seq->upper)
645                                 break;
646                         tipc_nametbl_withdraw(publ->type, publ->lower,
647                                               publ->ref, publ->key);
648                         res = 0;
649                         break;
650                 }
651         }
652         if (list_empty(&p_ptr->publications))
653                 p_ptr->published = 0;
654         return res;
655 }
656
657 int tipc_port_connect(u32 ref, struct tipc_portid const *peer)
658 {
659         struct tipc_port *p_ptr;
660         int res;
661
662         p_ptr = tipc_port_lock(ref);
663         if (!p_ptr)
664                 return -EINVAL;
665         res = __tipc_port_connect(ref, p_ptr, peer);
666         tipc_port_unlock(p_ptr);
667         return res;
668 }
669
670 /*
671  * __tipc_port_connect - connect to a remote peer
672  *
673  * Port must be locked.
674  */
675 int __tipc_port_connect(u32 ref, struct tipc_port *p_ptr,
676                         struct tipc_portid const *peer)
677 {
678         struct tipc_msg *msg;
679         int res = -EINVAL;
680
681         if (p_ptr->published || p_ptr->connected)
682                 goto exit;
683         if (!peer->ref)
684                 goto exit;
685
686         msg = &p_ptr->phdr;
687         msg_set_destnode(msg, peer->node);
688         msg_set_destport(msg, peer->ref);
689         msg_set_type(msg, TIPC_CONN_MSG);
690         msg_set_lookup_scope(msg, 0);
691         msg_set_hdr_sz(msg, SHORT_H_SIZE);
692
693         p_ptr->probing_interval = PROBING_INTERVAL;
694         p_ptr->probing_state = CONFIRMED;
695         p_ptr->connected = 1;
696         k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
697
698         tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
699                           (void *)(unsigned long)ref,
700                           (net_ev_handler)port_handle_node_down);
701         res = 0;
702 exit:
703         p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
704         return res;
705 }
706
707 /*
708  * __tipc_disconnect - disconnect port from peer
709  *
710  * Port must be locked.
711  */
712 int __tipc_port_disconnect(struct tipc_port *tp_ptr)
713 {
714         if (tp_ptr->connected) {
715                 tp_ptr->connected = 0;
716                 /* let timer expire on it's own to avoid deadlock! */
717                 tipc_nodesub_unsubscribe(&tp_ptr->subscription);
718                 return 0;
719         }
720
721         return -ENOTCONN;
722 }
723
724 /*
725  * tipc_port_disconnect(): Disconnect port form peer.
726  *                    This is a node local operation.
727  */
728 int tipc_port_disconnect(u32 ref)
729 {
730         struct tipc_port *p_ptr;
731         int res;
732
733         p_ptr = tipc_port_lock(ref);
734         if (!p_ptr)
735                 return -EINVAL;
736         res = __tipc_port_disconnect(p_ptr);
737         tipc_port_unlock(p_ptr);
738         return res;
739 }
740
741 /*
742  * tipc_port_shutdown(): Send a SHUTDOWN msg to peer and disconnect
743  */
744 int tipc_port_shutdown(u32 ref)
745 {
746         struct tipc_port *p_ptr;
747         struct sk_buff *buf = NULL;
748
749         p_ptr = tipc_port_lock(ref);
750         if (!p_ptr)
751                 return -EINVAL;
752
753         buf = port_build_peer_abort_msg(p_ptr, TIPC_CONN_SHUTDOWN);
754         tipc_port_unlock(p_ptr);
755         tipc_net_route_msg(buf);
756         return tipc_port_disconnect(ref);
757 }
758
759 /**
760  * tipc_port_rcv - receive message from lower layer and deliver to port user
761  */
762 int tipc_port_rcv(struct sk_buff *buf)
763 {
764         struct tipc_port *p_ptr;
765         struct tipc_msg *msg = buf_msg(buf);
766         u32 destport = msg_destport(msg);
767         u32 dsz = msg_data_sz(msg);
768         u32 err;
769
770         /* forward unresolved named message */
771         if (unlikely(!destport)) {
772                 tipc_net_route_msg(buf);
773                 return dsz;
774         }
775
776         /* validate destination & pass to port, otherwise reject message */
777         p_ptr = tipc_port_lock(destport);
778         if (likely(p_ptr)) {
779                 err = tipc_sk_rcv(tipc_port_to_sk(p_ptr), buf);
780                 tipc_port_unlock(p_ptr);
781                 if (likely(!err))
782                         return dsz;
783         } else {
784                 err = TIPC_ERR_NO_PORT;
785         }
786
787         return tipc_reject_msg(buf, err);
788 }
789
790 /*
791  *  tipc_port_iovec_rcv: Concatenate and deliver sectioned
792  *                       message for this node.
793  */
794 static int tipc_port_iovec_rcv(struct tipc_port *sender,
795                                struct iovec const *msg_sect,
796                                unsigned int len)
797 {
798         struct sk_buff *buf;
799         int res;
800
801         res = tipc_msg_build(&sender->phdr, msg_sect, len, MAX_MSG_SIZE, &buf);
802         if (likely(buf))
803                 tipc_port_rcv(buf);
804         return res;
805 }
806
807 /**
808  * tipc_send - send message sections on connection
809  */
810 int tipc_send(u32 ref, struct iovec const *msg_sect, unsigned int len)
811 {
812         struct tipc_port *p_ptr;
813         u32 destnode;
814         int res;
815
816         p_ptr = tipc_port_deref(ref);
817         if (!p_ptr || !p_ptr->connected)
818                 return -EINVAL;
819
820         p_ptr->congested = 1;
821         if (!tipc_port_congested(p_ptr)) {
822                 destnode = tipc_port_peernode(p_ptr);
823                 if (likely(!in_own_node(destnode)))
824                         res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
825                                                         destnode);
826                 else
827                         res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
828
829                 if (likely(res != -ELINKCONG)) {
830                         p_ptr->congested = 0;
831                         if (res > 0)
832                                 p_ptr->sent++;
833                         return res;
834                 }
835         }
836         if (tipc_port_unreliable(p_ptr)) {
837                 p_ptr->congested = 0;
838                 return len;
839         }
840         return -ELINKCONG;
841 }
842
843 /**
844  * tipc_send2name - send message sections to port name
845  */
846 int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
847                    struct iovec const *msg_sect, unsigned int len)
848 {
849         struct tipc_port *p_ptr;
850         struct tipc_msg *msg;
851         u32 destnode = domain;
852         u32 destport;
853         int res;
854
855         p_ptr = tipc_port_deref(ref);
856         if (!p_ptr || p_ptr->connected)
857                 return -EINVAL;
858
859         msg = &p_ptr->phdr;
860         msg_set_type(msg, TIPC_NAMED_MSG);
861         msg_set_hdr_sz(msg, NAMED_H_SIZE);
862         msg_set_nametype(msg, name->type);
863         msg_set_nameinst(msg, name->instance);
864         msg_set_lookup_scope(msg, tipc_addr_scope(domain));
865         destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
866         msg_set_destnode(msg, destnode);
867         msg_set_destport(msg, destport);
868
869         if (likely(destport || destnode)) {
870                 if (likely(in_own_node(destnode)))
871                         res = tipc_port_iovec_rcv(p_ptr, msg_sect, len);
872                 else if (tipc_own_addr)
873                         res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
874                                                         destnode);
875                 else
876                         res = tipc_port_iovec_reject(p_ptr, msg, msg_sect,
877                                                      len, TIPC_ERR_NO_NODE);
878                 if (likely(res != -ELINKCONG)) {
879                         if (res > 0)
880                                 p_ptr->sent++;
881                         return res;
882                 }
883                 if (tipc_port_unreliable(p_ptr))
884                         return len;
885
886                 return -ELINKCONG;
887         }
888         return tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
889                                       TIPC_ERR_NO_NAME);
890 }
891
892 /**
893  * tipc_send2port - send message sections to port identity
894  */
895 int tipc_send2port(u32 ref, struct tipc_portid const *dest,
896                    struct iovec const *msg_sect, unsigned int len)
897 {
898         struct tipc_port *p_ptr;
899         struct tipc_msg *msg;
900         int res;
901
902         p_ptr = tipc_port_deref(ref);
903         if (!p_ptr || p_ptr->connected)
904                 return -EINVAL;
905
906         msg = &p_ptr->phdr;
907         msg_set_type(msg, TIPC_DIRECT_MSG);
908         msg_set_lookup_scope(msg, 0);
909         msg_set_destnode(msg, dest->node);
910         msg_set_destport(msg, dest->ref);
911         msg_set_hdr_sz(msg, BASIC_H_SIZE);
912
913         if (in_own_node(dest->node))
914                 res =  tipc_port_iovec_rcv(p_ptr, msg_sect, len);
915         else if (tipc_own_addr)
916                 res = tipc_link_iovec_xmit_fast(p_ptr, msg_sect, len,
917                                                 dest->node);
918         else
919                 res = tipc_port_iovec_reject(p_ptr, msg, msg_sect, len,
920                                                 TIPC_ERR_NO_NODE);
921         if (likely(res != -ELINKCONG)) {
922                 if (res > 0)
923                         p_ptr->sent++;
924                 return res;
925         }
926         if (tipc_port_unreliable(p_ptr))
927                 return len;
928
929         return -ELINKCONG;
930 }
This page took 0.090913 seconds and 4 git commands to generate.