blob: 33d0b3b7175f2fe0bd0e06dbd12e1710c446d637 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/port.c: TIPC port code
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Allan Stephens05646c92007-06-10 17:25:24 -07004 * Copyright (c) 1992-2007, Ericsson AB
Allan Stephens0ea52242008-07-14 22:42:19 -07005 * Copyright (c) 2004-2008, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "config.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010040#include "name_table.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42/* Connection management: */
43#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
44#define CONFIRMED 0
45#define PROBING 1
46
47#define MAX_REJECT_SIZE 1024
48
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080049static struct sk_buff *msg_queue_head = NULL;
50static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010051
Ingo Molnar34af9462006-06-27 02:53:55 -070052DEFINE_SPINLOCK(tipc_port_list_lock);
53static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010054
Per Liden4323add2006-01-18 00:38:21 +010055static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010056static void port_handle_node_down(unsigned long ref);
57static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
58static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
59static void port_timeout(unsigned long ref);
60
61
Sam Ravnborg05790c62006-03-20 22:37:04 -080062static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010063{
64 return msg_destnode(&p_ptr->publ.phdr);
65}
66
Sam Ravnborg05790c62006-03-20 22:37:04 -080067static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010068{
69 return msg_destport(&p_ptr->publ.phdr);
70}
71
Sam Ravnborg05790c62006-03-20 22:37:04 -080072static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010073{
74 return msg_transp_seqno(&p_ptr->publ.phdr);
75}
76
Sam Ravnborg05790c62006-03-20 22:37:04 -080077static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010078{
79 struct tipc_msg *m = &p_ptr->publ.phdr;
80
81 if (likely(!msg_routed(m)))
82 return;
83 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
84}
85
86/**
87 * tipc_multicast - send a multicast message to local and remote destinations
88 */
89
Allan Stephens38f232e2010-11-30 12:00:59 +000090int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
Per Lidenb97bf3f2006-01-02 19:04:38 +010091 u32 num_sect, struct iovec const *msg_sect)
92{
93 struct tipc_msg *hdr;
94 struct sk_buff *buf;
95 struct sk_buff *ibuf = NULL;
96 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +010097 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 int ext_targets;
99 int res;
100
101 if (unlikely(!oport))
102 return -EINVAL;
103
104 /* Create multicast message */
105
106 hdr = &oport->publ.phdr;
107 msg_set_type(hdr, TIPC_MCAST_MSG);
108 msg_set_nametype(hdr, seq->type);
109 msg_set_namelower(hdr, seq->lower);
110 msg_set_nameupper(hdr, seq->upper);
111 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000112 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 !oport->user_port, &buf);
114 if (unlikely(!buf))
115 return res;
116
117 /* Figure out where to send multicast message */
118
Per Liden4323add2006-01-18 00:38:21 +0100119 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
120 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900121
122 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100123
124 if (ext_targets) {
125 if (dports.count != 0) {
126 ibuf = skb_copy(buf, GFP_ATOMIC);
127 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100128 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129 buf_discard(buf);
130 return -ENOMEM;
131 }
132 }
Per Liden4323add2006-01-18 00:38:21 +0100133 res = tipc_bclink_send_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100134 if ((res < 0) && (dports.count != 0)) {
135 buf_discard(ibuf);
136 }
137 } else {
138 ibuf = buf;
139 }
140
141 if (res >= 0) {
142 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100143 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100144 } else {
Per Liden4323add2006-01-18 00:38:21 +0100145 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100146 }
147 return res;
148}
149
150/**
Per Liden4323add2006-01-18 00:38:21 +0100151 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900152 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 * If there is no port list, perform a lookup to create one
154 */
155
Per Liden4323add2006-01-18 00:38:21 +0100156void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157{
158 struct tipc_msg* msg;
159 struct port_list dports = {0, NULL, };
160 struct port_list *item = dp;
161 int cnt = 0;
162
Per Lidenb97bf3f2006-01-02 19:04:38 +0100163 msg = buf_msg(buf);
164
165 /* Create destination port list, if one wasn't supplied */
166
167 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100168 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169 msg_namelower(msg),
170 msg_nameupper(msg),
171 TIPC_CLUSTER_SCOPE,
172 &dports);
173 item = dp = &dports;
174 }
175
176 /* Deliver a copy of message to each destination port */
177
178 if (dp->count != 0) {
179 if (dp->count == 1) {
180 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100181 tipc_port_recv_msg(buf);
182 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 return;
184 }
185 for (; cnt < dp->count; cnt++) {
186 int index = cnt % PLSIZE;
187 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
188
189 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700190 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 msg_dbg(msg, "LOST:");
192 goto exit;
193 }
194 if ((index == 0) && (cnt != 0)) {
195 item = item->next;
196 }
197 msg_set_destport(buf_msg(b),item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100198 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 }
200 }
201exit:
202 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100203 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204}
205
206/**
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700207 * tipc_createport_raw - create a generic TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900208 *
Allan Stephens0ea52242008-07-14 22:42:19 -0700209 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
Per Lidenb97bf3f2006-01-02 19:04:38 +0100210 */
211
Allan Stephens0ea52242008-07-14 22:42:19 -0700212struct tipc_port *tipc_createport_raw(void *usr_handle,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
214 void (*wakeup)(struct tipc_port *),
Allan Stephens0ea52242008-07-14 22:42:19 -0700215 const u32 importance)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216{
217 struct port *p_ptr;
218 struct tipc_msg *msg;
219 u32 ref;
220
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700221 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700222 if (!p_ptr) {
223 warn("Port creation failed, no memory\n");
Allan Stephens0ea52242008-07-14 22:42:19 -0700224 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 }
Per Liden4323add2006-01-18 00:38:21 +0100226 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700228 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100229 kfree(p_ptr);
Allan Stephens0ea52242008-07-14 22:42:19 -0700230 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 }
232
Allan Stephens05646c92007-06-10 17:25:24 -0700233 p_ptr->publ.usr_handle = usr_handle;
234 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 p_ptr->publ.ref = ref;
236 msg = &p_ptr->publ.phdr;
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000237 tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100239 p_ptr->last_in_seqno = 41;
240 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 INIT_LIST_HEAD(&p_ptr->wait_list);
242 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 p_ptr->dispatcher = dispatcher;
244 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800245 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100247 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100248 INIT_LIST_HEAD(&p_ptr->publications);
249 INIT_LIST_HEAD(&p_ptr->port_list);
250 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100251 spin_unlock_bh(&tipc_port_list_lock);
Allan Stephens0ea52242008-07-14 22:42:19 -0700252 return &(p_ptr->publ);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253}
254
255int tipc_deleteport(u32 ref)
256{
257 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800258 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800260 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100261 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900262 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100263 return -EINVAL;
264
Per Liden4323add2006-01-18 00:38:21 +0100265 tipc_ref_discard(ref);
266 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267
268 k_cancel_timer(&p_ptr->timer);
269 if (p_ptr->publ.connected) {
270 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100271 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272 }
273 if (p_ptr->user_port) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 kfree(p_ptr->user_port);
275 }
276
Per Liden4323add2006-01-18 00:38:21 +0100277 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100278 list_del(&p_ptr->port_list);
279 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100280 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100281 k_term_timer(&p_ptr->timer);
282 kfree(p_ptr);
283 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100284 tipc_net_route_msg(buf);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700285 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286}
287
Sam Ravnborg05790c62006-03-20 22:37:04 -0800288static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289{
290 return msg_src_droppable(&p_ptr->publ.phdr);
291}
292
293int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
294{
295 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900296
Per Liden4323add2006-01-18 00:38:21 +0100297 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100298 if (!p_ptr)
299 return -EINVAL;
300 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800301 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700302 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100303}
304
305int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
306{
307 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900308
Per Liden4323add2006-01-18 00:38:21 +0100309 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 if (!p_ptr)
311 return -EINVAL;
312 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100313 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700314 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100315}
316
Sam Ravnborg05790c62006-03-20 22:37:04 -0800317static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318{
319 return msg_dest_droppable(&p_ptr->publ.phdr);
320}
321
322int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
323{
324 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900325
Per Liden4323add2006-01-18 00:38:21 +0100326 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 if (!p_ptr)
328 return -EINVAL;
329 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800330 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700331 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332}
333
334int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
335{
336 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900337
Per Liden4323add2006-01-18 00:38:21 +0100338 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100339 if (!p_ptr)
340 return -EINVAL;
341 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100342 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700343 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344}
345
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900346/*
347 * port_build_proto_msg(): build a port level protocol
348 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 * tipc_port lock on.
350 */
351static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
352 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900353 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 u32 seqno, u32 ack)
355{
356 struct sk_buff *buf;
357 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900358
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000359 buf = tipc_buf_acquire(LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 if (buf) {
361 msg = buf_msg(buf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000362 tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
Allan Stephens75715212008-06-04 17:37:34 -0700363 msg_set_errcode(msg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 msg_set_destport(msg, destport);
365 msg_set_origport(msg, origport);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 msg_set_orignode(msg, orignode);
367 msg_set_transp_seqno(msg, seqno);
368 msg_set_msgcnt(msg, ack);
369 msg_dbg(msg, "PORT>SEND>:");
370 }
371 return buf;
372}
373
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374int tipc_reject_msg(struct sk_buff *buf, u32 err)
375{
376 struct tipc_msg *msg = buf_msg(buf);
377 struct sk_buff *rbuf;
378 struct tipc_msg *rmsg;
379 int hdr_sz;
380 u32 imp = msg_importance(msg);
381 u32 data_sz = msg_data_sz(msg);
382
383 if (data_sz > MAX_REJECT_SIZE)
384 data_sz = MAX_REJECT_SIZE;
385 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
386 imp++;
387 msg_dbg(msg, "port->rej: ");
388
389 /* discard rejected message if it shouldn't be returned to sender */
390 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
391 buf_discard(buf);
392 return data_sz;
393 }
394
395 /* construct rejected message */
396 if (msg_mcast(msg))
397 hdr_sz = MCAST_H_SIZE;
398 else
399 hdr_sz = LONG_H_SIZE;
stephen hemminger31e3c3f2010-10-13 13:20:35 +0000400 rbuf = tipc_buf_acquire(data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 if (rbuf == NULL) {
402 buf_discard(buf);
403 return data_sz;
404 }
405 rmsg = buf_msg(rbuf);
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000406 tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
Allan Stephens75715212008-06-04 17:37:34 -0700407 msg_set_errcode(rmsg, err);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100408 msg_set_destport(rmsg, msg_origport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100409 msg_set_origport(rmsg, msg_destport(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700410 if (msg_short(msg)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100411 msg_set_orignode(rmsg, tipc_own_addr);
Allan Stephens99c14592008-06-04 17:48:25 -0700412 /* leave name type & instance as zeroes */
413 } else {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100414 msg_set_orignode(rmsg, msg_destnode(msg));
Allan Stephens99c14592008-06-04 17:48:25 -0700415 msg_set_nametype(rmsg, msg_nametype(msg));
416 msg_set_nameinst(rmsg, msg_nameinst(msg));
417 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900418 msg_set_size(rmsg, data_sz + hdr_sz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300419 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420
421 /* send self-abort message when rejecting on a connected port */
422 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800423 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100424 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425
426 if (p_ptr) {
427 if (p_ptr->publ.connected)
428 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100429 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 }
Per Liden4323add2006-01-18 00:38:21 +0100431 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 }
433
434 /* send rejected message */
435 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100436 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 return data_sz;
438}
439
Per Liden4323add2006-01-18 00:38:21 +0100440int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
441 struct iovec const *msg_sect, u32 num_sect,
442 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100443{
444 struct sk_buff *buf;
445 int res;
446
Allan Stephensc68ca7b2010-05-11 14:30:12 +0000447 res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448 !p_ptr->user_port, &buf);
449 if (!buf)
450 return res;
451
452 return tipc_reject_msg(buf, err);
453}
454
455static void port_timeout(unsigned long ref)
456{
Per Liden4323add2006-01-18 00:38:21 +0100457 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800458 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459
Allan Stephens065fd172006-10-16 21:38:05 -0700460 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 return;
462
Allan Stephens065fd172006-10-16 21:38:05 -0700463 if (!p_ptr->publ.connected) {
464 tipc_port_unlock(p_ptr);
465 return;
466 }
467
Per Lidenb97bf3f2006-01-02 19:04:38 +0100468 /* Last probe answered ? */
469 if (p_ptr->probing_state == PROBING) {
470 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
471 } else {
472 buf = port_build_proto_msg(port_peerport(p_ptr),
473 port_peernode(p_ptr),
474 p_ptr->publ.ref,
475 tipc_own_addr,
476 CONN_MANAGER,
477 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900478 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 port_out_seqno(p_ptr),
480 0);
481 port_incr_out_seqno(p_ptr);
482 p_ptr->probing_state = PROBING;
483 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
484 }
Per Liden4323add2006-01-18 00:38:21 +0100485 tipc_port_unlock(p_ptr);
486 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487}
488
489
490static void port_handle_node_down(unsigned long ref)
491{
Per Liden4323add2006-01-18 00:38:21 +0100492 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800493 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494
495 if (!p_ptr)
496 return;
497 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100498 tipc_port_unlock(p_ptr);
499 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500}
501
502
503static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
504{
505 u32 imp = msg_importance(&p_ptr->publ.phdr);
506
507 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800508 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 if (imp < TIPC_CRITICAL_IMPORTANCE)
510 imp++;
511 return port_build_proto_msg(p_ptr->publ.ref,
512 tipc_own_addr,
513 port_peerport(p_ptr),
514 port_peernode(p_ptr),
515 imp,
516 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900517 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518 p_ptr->last_in_seqno + 1,
519 0);
520}
521
522
523static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
524{
525 u32 imp = msg_importance(&p_ptr->publ.phdr);
526
527 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800528 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529 if (imp < TIPC_CRITICAL_IMPORTANCE)
530 imp++;
531 return port_build_proto_msg(port_peerport(p_ptr),
532 port_peernode(p_ptr),
533 p_ptr->publ.ref,
534 tipc_own_addr,
535 imp,
536 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900537 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 port_out_seqno(p_ptr),
539 0);
540}
541
Per Liden4323add2006-01-18 00:38:21 +0100542void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543{
544 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100545 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800547 struct sk_buff *r_buf = NULL;
548 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549
550 msg_dbg(msg, "PORT<RECV<:");
551
552 if (!p_ptr) {
553 err = TIPC_ERR_NO_PORT;
554 } else if (p_ptr->publ.connected) {
Allan Stephens96d841b2010-08-17 11:00:11 +0000555 if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
556 (port_peerport(p_ptr) != msg_origport(msg))) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 err = TIPC_ERR_NO_PORT;
Allan Stephens96d841b2010-08-17 11:00:11 +0000558 } else if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900559 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100560 p_ptr->publ.congested &&
561 p_ptr->wakeup;
562 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100563 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100564 goto exit;
565 p_ptr->publ.congested = 0;
566 if (!wakeup)
567 goto exit;
568 p_ptr->wakeup(&p_ptr->publ);
569 goto exit;
570 }
571 } else if (p_ptr->publ.published) {
572 err = TIPC_ERR_NO_PORT;
573 }
574 if (err) {
575 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 msg_orignode(msg),
577 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 tipc_own_addr,
Allan Stephens06d82c92008-03-06 15:06:55 -0800579 TIPC_HIGH_IMPORTANCE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100580 TIPC_CONN_MSG,
581 err,
582 0,
583 0);
584 goto exit;
585 }
586
587 /* All is fine */
588 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900589 r_buf = port_build_proto_msg(msg_origport(msg),
590 msg_orignode(msg),
591 msg_destport(msg),
592 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593 CONN_MANAGER,
594 CONN_PROBE_REPLY,
595 TIPC_OK,
596 port_out_seqno(p_ptr),
597 0);
598 }
599 p_ptr->probing_state = CONFIRMED;
600 port_incr_out_seqno(p_ptr);
601exit:
602 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100603 tipc_port_unlock(p_ptr);
604 tipc_net_route_msg(r_buf);
605 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100606 buf_discard(buf);
607}
608
609static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
610{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900611 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100612
613 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900614 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100615 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900616 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 else
618 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
619
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900620 if (p_ptr->publ.connected) {
621 u32 dport = port_peerport(p_ptr);
622 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100623
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900624 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
625 tipc_zone(destnode), tipc_cluster(destnode),
626 tipc_node(destnode), dport);
627 if (p_ptr->publ.conn_type != 0)
628 tipc_printf(buf, " via {%u,%u}",
629 p_ptr->publ.conn_type,
630 p_ptr->publ.conn_instance);
631 }
632 else if (p_ptr->publ.published) {
633 tipc_printf(buf, " bound to");
634 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100635 if (publ->lower == publ->upper)
636 tipc_printf(buf, " {%u,%u}", publ->type,
637 publ->lower);
638 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900639 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100640 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900641 }
642 }
643 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100644}
645
646#define MAX_PORT_QUERY 32768
647
Per Liden4323add2006-01-18 00:38:21 +0100648struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100649{
650 struct sk_buff *buf;
651 struct tlv_desc *rep_tlv;
652 struct print_buf pb;
653 struct port *p_ptr;
654 int str_len;
655
Per Liden4323add2006-01-18 00:38:21 +0100656 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100657 if (!buf)
658 return NULL;
659 rep_tlv = (struct tlv_desc *)buf->data;
660
Per Liden4323add2006-01-18 00:38:21 +0100661 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
662 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100663 list_for_each_entry(p_ptr, &ports, port_list) {
664 spin_lock_bh(p_ptr->publ.lock);
665 port_print(p_ptr, &pb, 0);
666 spin_unlock_bh(p_ptr->publ.lock);
667 }
Per Liden4323add2006-01-18 00:38:21 +0100668 spin_unlock_bh(&tipc_port_list_lock);
669 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100670
671 skb_put(buf, TLV_SPACE(str_len));
672 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
673
674 return buf;
675}
676
Per Liden4323add2006-01-18 00:38:21 +0100677void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100678{
679 struct port *p_ptr;
680 struct tipc_msg *msg;
681
Per Liden4323add2006-01-18 00:38:21 +0100682 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100683 list_for_each_entry(p_ptr, &ports, port_list) {
684 msg = &p_ptr->publ.phdr;
685 if (msg_orignode(msg) == tipc_own_addr)
686 break;
Allan Stephens6d4a6672008-05-21 14:54:12 -0700687 msg_set_prevnode(msg, tipc_own_addr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100688 msg_set_orignode(msg, tipc_own_addr);
689 }
Per Liden4323add2006-01-18 00:38:21 +0100690 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100691}
692
693
694/*
695 * port_dispatcher_sigh(): Signal handler for messages destinated
696 * to the tipc_port interface.
697 */
698
699static void port_dispatcher_sigh(void *dummy)
700{
701 struct sk_buff *buf;
702
703 spin_lock_bh(&queue_lock);
704 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800705 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 spin_unlock_bh(&queue_lock);
707
708 while (buf) {
709 struct port *p_ptr;
710 struct user_port *up_ptr;
711 struct tipc_portid orig;
712 struct tipc_name_seq dseq;
713 void *usr_handle;
714 int connected;
715 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700716 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100717
718 struct sk_buff *next = buf->next;
719 struct tipc_msg *msg = buf_msg(buf);
720 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900721
Allan Stephens96882432006-06-25 23:38:58 -0700722 message_type = msg_type(msg);
723 if (message_type > TIPC_DIRECT_MSG)
724 goto reject; /* Unsupported message type */
725
Per Liden4323add2006-01-18 00:38:21 +0100726 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700727 if (!p_ptr)
728 goto reject; /* Port deleted while msg in queue */
729
Per Lidenb97bf3f2006-01-02 19:04:38 +0100730 orig.ref = msg_origport(msg);
731 orig.node = msg_orignode(msg);
732 up_ptr = p_ptr->user_port;
733 usr_handle = up_ptr->usr_handle;
734 connected = p_ptr->publ.connected;
735 published = p_ptr->publ.published;
736
737 if (unlikely(msg_errcode(msg)))
738 goto err;
739
Allan Stephens96882432006-06-25 23:38:58 -0700740 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900741
Per Lidenb97bf3f2006-01-02 19:04:38 +0100742 case TIPC_CONN_MSG:{
743 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
744 u32 peer_port = port_peerport(p_ptr);
745 u32 peer_node = port_peernode(p_ptr);
746
Julia Lawall4cec72c2008-01-08 23:48:20 -0800747 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700748 if (unlikely(!cb))
749 goto reject;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (unlikely(!connected)) {
Allan Stephens84b07c12008-06-04 17:28:21 -0700751 if (tipc_connect2port(dref, &orig))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 goto reject;
Allan Stephens84b07c12008-06-04 17:28:21 -0700753 } else if ((msg_origport(msg) != peer_port) ||
754 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100755 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900756 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900758 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100759 p_ptr->publ.conn_unacked);
760 skb_pull(buf, msg_hdr_sz(msg));
761 cb(usr_handle, dref, &buf, msg_data(msg),
762 msg_data_sz(msg));
763 break;
764 }
765 case TIPC_DIRECT_MSG:{
766 tipc_msg_event cb = up_ptr->msg_cb;
767
Julia Lawall4cec72c2008-01-08 23:48:20 -0800768 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700769 if (unlikely(!cb || connected))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100770 goto reject;
771 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900772 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100773 msg_data_sz(msg), msg_importance(msg),
774 &orig);
775 break;
776 }
Allan Stephens96882432006-06-25 23:38:58 -0700777 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100778 case TIPC_NAMED_MSG:{
779 tipc_named_msg_event cb = up_ptr->named_msg_cb;
780
Julia Lawall4cec72c2008-01-08 23:48:20 -0800781 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700782 if (unlikely(!cb || connected || !published))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783 goto reject;
784 dseq.type = msg_nametype(msg);
785 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700786 dseq.upper = (message_type == TIPC_NAMED_MSG)
787 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100788 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900789 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100790 msg_data_sz(msg), msg_importance(msg),
791 &orig, &dseq);
792 break;
793 }
794 }
795 if (buf)
796 buf_discard(buf);
797 buf = next;
798 continue;
799err:
Allan Stephens96882432006-06-25 23:38:58 -0700800 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900801
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900803 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100804 up_ptr->conn_err_cb;
805 u32 peer_port = port_peerport(p_ptr);
806 u32 peer_node = port_peernode(p_ptr);
807
Julia Lawall4cec72c2008-01-08 23:48:20 -0800808 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700809 if (!cb || !connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100810 break;
Allan Stephens5307e462008-06-04 17:28:45 -0700811 if ((msg_origport(msg) != peer_port) ||
812 (msg_orignode(msg) != peer_node))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813 break;
814 tipc_disconnect(dref);
815 skb_pull(buf, msg_hdr_sz(msg));
816 cb(usr_handle, dref, &buf, msg_data(msg),
817 msg_data_sz(msg), msg_errcode(msg));
818 break;
819 }
820 case TIPC_DIRECT_MSG:{
821 tipc_msg_err_event cb = up_ptr->err_cb;
822
Julia Lawall4cec72c2008-01-08 23:48:20 -0800823 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700824 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100825 break;
826 skb_pull(buf, msg_hdr_sz(msg));
827 cb(usr_handle, dref, &buf, msg_data(msg),
828 msg_data_sz(msg), msg_errcode(msg), &orig);
829 break;
830 }
Allan Stephens96882432006-06-25 23:38:58 -0700831 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100832 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 up_ptr->named_err_cb;
835
Julia Lawall4cec72c2008-01-08 23:48:20 -0800836 tipc_port_unlock(p_ptr);
Allan Stephens5307e462008-06-04 17:28:45 -0700837 if (!cb || connected)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100838 break;
839 dseq.type = msg_nametype(msg);
840 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700841 dseq.upper = (message_type == TIPC_NAMED_MSG)
842 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100843 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900844 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100845 msg_data_sz(msg), msg_errcode(msg), &dseq);
846 break;
847 }
848 }
849 if (buf)
850 buf_discard(buf);
851 buf = next;
852 continue;
853reject:
854 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
855 buf = next;
856 }
857}
858
859/*
860 * port_dispatcher(): Dispatcher for messages destinated
861 * to the tipc_port interface. Called with port locked.
862 */
863
864static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
865{
866 buf->next = NULL;
867 spin_lock_bh(&queue_lock);
868 if (msg_queue_head) {
869 msg_queue_tail->next = buf;
870 msg_queue_tail = buf;
871 } else {
872 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100873 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100874 }
875 spin_unlock_bh(&queue_lock);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700876 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100877}
878
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900879/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100880 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900881 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100882 */
883
884static void port_wakeup_sh(unsigned long ref)
885{
886 struct port *p_ptr;
887 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800888 tipc_continue_event cb = NULL;
889 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100890
Per Liden4323add2006-01-18 00:38:21 +0100891 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100892 if (p_ptr) {
893 up_ptr = p_ptr->user_port;
894 if (up_ptr) {
895 cb = up_ptr->continue_event_cb;
896 uh = up_ptr->usr_handle;
897 }
Per Liden4323add2006-01-18 00:38:21 +0100898 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100899 }
900 if (cb)
901 cb(uh, ref);
902}
903
904
905static void port_wakeup(struct tipc_port *p_ptr)
906{
Per Liden4323add2006-01-18 00:38:21 +0100907 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100908}
909
910void tipc_acknowledge(u32 ref, u32 ack)
911{
912 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800913 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100914
Per Liden4323add2006-01-18 00:38:21 +0100915 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100916 if (!p_ptr)
917 return;
918 if (p_ptr->publ.connected) {
919 p_ptr->publ.conn_unacked -= ack;
920 buf = port_build_proto_msg(port_peerport(p_ptr),
921 port_peernode(p_ptr),
922 ref,
923 tipc_own_addr,
924 CONN_MANAGER,
925 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900926 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100927 port_out_seqno(p_ptr),
928 ack);
929 }
Per Liden4323add2006-01-18 00:38:21 +0100930 tipc_port_unlock(p_ptr);
931 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100932}
933
934/*
Allan Stephensb0c1e922010-12-31 18:59:22 +0000935 * tipc_createport(): user level call.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 */
937
Allan Stephensb0c1e922010-12-31 18:59:22 +0000938int tipc_createport(void *usr_handle,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900939 unsigned int importance,
940 tipc_msg_err_event error_cb,
941 tipc_named_msg_err_event named_error_cb,
942 tipc_conn_shutdown_event conn_error_cb,
943 tipc_msg_event msg_cb,
944 tipc_named_msg_event named_msg_cb,
945 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100946 tipc_continue_event continue_event_cb,/* May be zero */
947 u32 *portref)
948{
949 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900950 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100951
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700952 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700953 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -0700954 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100955 return -ENOMEM;
956 }
Allan Stephens0ea52242008-07-14 22:42:19 -0700957 p_ptr = (struct port *)tipc_createport_raw(NULL, port_dispatcher,
958 port_wakeup, importance);
959 if (!p_ptr) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100960 kfree(up_ptr);
961 return -ENOMEM;
962 }
963
964 p_ptr->user_port = up_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100965 up_ptr->usr_handle = usr_handle;
966 up_ptr->ref = p_ptr->publ.ref;
967 up_ptr->err_cb = error_cb;
968 up_ptr->named_err_cb = named_error_cb;
969 up_ptr->conn_err_cb = conn_error_cb;
970 up_ptr->msg_cb = msg_cb;
971 up_ptr->named_msg_cb = named_msg_cb;
972 up_ptr->conn_msg_cb = conn_msg_cb;
973 up_ptr->continue_event_cb = continue_event_cb;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100974 *portref = p_ptr->publ.ref;
Per Liden4323add2006-01-18 00:38:21 +0100975 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700976 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100977}
978
Per Lidenb97bf3f2006-01-02 19:04:38 +0100979int tipc_portimportance(u32 ref, unsigned int *importance)
980{
981 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900982
Per Liden4323add2006-01-18 00:38:21 +0100983 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 if (!p_ptr)
985 return -EINVAL;
986 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800987 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -0700988 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100989}
990
991int tipc_set_portimportance(u32 ref, unsigned int imp)
992{
993 struct port *p_ptr;
994
995 if (imp > TIPC_CRITICAL_IMPORTANCE)
996 return -EINVAL;
997
Per Liden4323add2006-01-18 00:38:21 +0100998 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100999 if (!p_ptr)
1000 return -EINVAL;
1001 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001002 tipc_port_unlock(p_ptr);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001003 return 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001004}
1005
1006
1007int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1008{
1009 struct port *p_ptr;
1010 struct publication *publ;
1011 u32 key;
1012 int res = -EINVAL;
1013
Per Liden4323add2006-01-18 00:38:21 +01001014 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001015 if (!p_ptr)
1016 return -EINVAL;
1017
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1019 "lower = %u, upper = %u\n",
1020 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001021 if (p_ptr->publ.connected)
1022 goto exit;
1023 if (seq->lower > seq->upper)
1024 goto exit;
1025 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1026 goto exit;
1027 key = ref + p_ptr->pub_count + 1;
1028 if (key == ref) {
1029 res = -EADDRINUSE;
1030 goto exit;
1031 }
Per Liden4323add2006-01-18 00:38:21 +01001032 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1033 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034 if (publ) {
1035 list_add(&publ->pport_list, &p_ptr->publications);
1036 p_ptr->pub_count++;
1037 p_ptr->publ.published = 1;
Allan Stephens0e35fd52008-07-14 22:44:01 -07001038 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001039 }
1040exit:
Per Liden4323add2006-01-18 00:38:21 +01001041 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001042 return res;
1043}
1044
1045int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1046{
1047 struct port *p_ptr;
1048 struct publication *publ;
1049 struct publication *tpubl;
1050 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001051
Per Liden4323add2006-01-18 00:38:21 +01001052 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001053 if (!p_ptr)
1054 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001056 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001057 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001058 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001059 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 }
Allan Stephens0e35fd52008-07-14 22:44:01 -07001061 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001062 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001063 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 &p_ptr->publications, pport_list) {
1065 if (publ->scope != scope)
1066 continue;
1067 if (publ->type != seq->type)
1068 continue;
1069 if (publ->lower != seq->lower)
1070 continue;
1071 if (publ->upper != seq->upper)
1072 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001073 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001074 publ->ref, publ->key);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001075 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001076 break;
1077 }
1078 }
1079 if (list_empty(&p_ptr->publications))
1080 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001081 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 return res;
1083}
1084
1085int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1086{
1087 struct port *p_ptr;
1088 struct tipc_msg *msg;
1089 int res = -EINVAL;
1090
Per Liden4323add2006-01-18 00:38:21 +01001091 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001092 if (!p_ptr)
1093 return -EINVAL;
1094 if (p_ptr->publ.published || p_ptr->publ.connected)
1095 goto exit;
1096 if (!peer->ref)
1097 goto exit;
1098
1099 msg = &p_ptr->publ.phdr;
1100 msg_set_destnode(msg, peer->node);
1101 msg_set_destport(msg, peer->ref);
1102 msg_set_orignode(msg, tipc_own_addr);
1103 msg_set_origport(msg, p_ptr->publ.ref);
1104 msg_set_transp_seqno(msg, 42);
1105 msg_set_type(msg, TIPC_CONN_MSG);
Allan Stephens08c80e92010-12-31 18:59:17 +00001106 msg_set_hdr_sz(msg, SHORT_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001107
1108 p_ptr->probing_interval = PROBING_INTERVAL;
1109 p_ptr->probing_state = CONFIRMED;
1110 p_ptr->publ.connected = 1;
1111 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1112
Per Liden4323add2006-01-18 00:38:21 +01001113 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001114 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 (net_ev_handler)port_handle_node_down);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001116 res = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001117exit:
Per Liden4323add2006-01-18 00:38:21 +01001118 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001119 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001120 return res;
1121}
1122
Allan Stephens0c3141e2008-04-15 00:22:02 -07001123/**
1124 * tipc_disconnect_port - disconnect port from peer
1125 *
1126 * Port must be locked.
1127 */
1128
1129int tipc_disconnect_port(struct tipc_port *tp_ptr)
1130{
1131 int res;
1132
1133 if (tp_ptr->connected) {
1134 tp_ptr->connected = 0;
1135 /* let timer expire on it's own to avoid deadlock! */
1136 tipc_nodesub_unsubscribe(
1137 &((struct port *)tp_ptr)->subscription);
Allan Stephens0e35fd52008-07-14 22:44:01 -07001138 res = 0;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001139 } else {
1140 res = -ENOTCONN;
1141 }
1142 return res;
1143}
1144
Per Lidenb97bf3f2006-01-02 19:04:38 +01001145/*
1146 * tipc_disconnect(): Disconnect port form peer.
1147 * This is a node local operation.
1148 */
1149
1150int tipc_disconnect(u32 ref)
1151{
1152 struct port *p_ptr;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001153 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001154
Per Liden4323add2006-01-18 00:38:21 +01001155 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001156 if (!p_ptr)
1157 return -EINVAL;
Allan Stephens0c3141e2008-04-15 00:22:02 -07001158 res = tipc_disconnect_port((struct tipc_port *)p_ptr);
Per Liden4323add2006-01-18 00:38:21 +01001159 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001160 return res;
1161}
1162
1163/*
1164 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1165 */
1166int tipc_shutdown(u32 ref)
1167{
1168 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001169 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001170
Per Liden4323add2006-01-18 00:38:21 +01001171 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001172 if (!p_ptr)
1173 return -EINVAL;
1174
1175 if (p_ptr->publ.connected) {
1176 u32 imp = msg_importance(&p_ptr->publ.phdr);
1177 if (imp < TIPC_CRITICAL_IMPORTANCE)
1178 imp++;
1179 buf = port_build_proto_msg(port_peerport(p_ptr),
1180 port_peernode(p_ptr),
1181 ref,
1182 tipc_own_addr,
1183 imp,
1184 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001185 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001186 port_out_seqno(p_ptr),
1187 0);
1188 }
Per Liden4323add2006-01-18 00:38:21 +01001189 tipc_port_unlock(p_ptr);
1190 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 return tipc_disconnect(ref);
1192}
1193
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194/*
Per Liden4323add2006-01-18 00:38:21 +01001195 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001196 * message for this node.
1197 */
1198
stephen hemminger31e3c3f2010-10-13 13:20:35 +00001199static int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
1200 struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201{
1202 struct sk_buff *buf;
1203 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001204
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001205 res = tipc_msg_build(&sender->publ.phdr, msg_sect, num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001206 MAX_MSG_SIZE, !sender->user_port, &buf);
1207 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001208 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001209 return res;
1210}
1211
1212/**
1213 * tipc_send - send message sections on connection
1214 */
1215
1216int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1217{
1218 struct port *p_ptr;
1219 u32 destnode;
1220 int res;
1221
Per Liden4323add2006-01-18 00:38:21 +01001222 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001223 if (!p_ptr || !p_ptr->publ.connected)
1224 return -EINVAL;
1225
1226 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001227 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001228 destnode = port_peernode(p_ptr);
1229 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001230 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1231 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001232 else
Per Liden4323add2006-01-18 00:38:21 +01001233 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234
1235 if (likely(res != -ELINKCONG)) {
1236 port_incr_out_seqno(p_ptr);
1237 p_ptr->publ.congested = 0;
1238 p_ptr->sent++;
1239 return res;
1240 }
1241 }
1242 if (port_unreliable(p_ptr)) {
1243 p_ptr->publ.congested = 0;
1244 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001245 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001246 }
1247 return -ELINKCONG;
1248}
1249
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001250/**
Allan Stephens12bae472010-11-30 12:01:02 +00001251 * tipc_send2name - send message sections to port name
Per Lidenb97bf3f2006-01-02 19:04:38 +01001252 */
1253
Allan Stephens12bae472010-11-30 12:01:02 +00001254int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
1255 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256{
1257 struct port *p_ptr;
1258 struct tipc_msg *msg;
1259 u32 destnode = domain;
Allan Stephens9ccc2eb2010-05-11 14:30:06 +00001260 u32 destport;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001261 int res;
1262
Per Liden4323add2006-01-18 00:38:21 +01001263 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001264 if (!p_ptr || p_ptr->publ.connected)
1265 return -EINVAL;
1266
1267 msg = &p_ptr->publ.phdr;
1268 msg_set_type(msg, TIPC_NAMED_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001269 msg_set_orignode(msg, tipc_own_addr);
1270 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001271 msg_set_hdr_sz(msg, LONG_H_SIZE);
1272 msg_set_nametype(msg, name->type);
1273 msg_set_nameinst(msg, name->instance);
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001274 msg_set_lookup_scope(msg, tipc_addr_scope(domain));
Per Liden4323add2006-01-18 00:38:21 +01001275 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001276 msg_set_destnode(msg, destnode);
1277 msg_set_destport(msg, destport);
1278
Allan Stephens5d9c54c2010-09-03 08:33:39 +00001279 if (likely(destport)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001280 p_ptr->sent++;
1281 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001282 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001283 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001284 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001285 if (likely(res != -ELINKCONG))
1286 return res;
1287 if (port_unreliable(p_ptr)) {
1288 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001289 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001290 }
1291 return -ELINKCONG;
1292 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001293 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001294 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001295}
1296
1297/**
Allan Stephens12bae472010-11-30 12:01:02 +00001298 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001299 */
1300
Allan Stephens12bae472010-11-30 12:01:02 +00001301int tipc_send2port(u32 ref, struct tipc_portid const *dest,
1302 unsigned int num_sect, struct iovec const *msg_sect)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001303{
1304 struct port *p_ptr;
1305 struct tipc_msg *msg;
1306 int res;
1307
Per Liden4323add2006-01-18 00:38:21 +01001308 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001309 if (!p_ptr || p_ptr->publ.connected)
1310 return -EINVAL;
1311
1312 msg = &p_ptr->publ.phdr;
1313 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001314 msg_set_orignode(msg, tipc_own_addr);
1315 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001316 msg_set_destnode(msg, dest->node);
1317 msg_set_destport(msg, dest->ref);
1318 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001319 p_ptr->sent++;
1320 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001321 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1322 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 if (likely(res != -ELINKCONG))
1324 return res;
1325 if (port_unreliable(p_ptr)) {
1326 /* Just calculate msg length and return */
Allan Stephensc68ca7b2010-05-11 14:30:12 +00001327 return tipc_msg_calc_data_size(msg_sect, num_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001328 }
1329 return -ELINKCONG;
1330}
1331
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001332/**
Allan Stephens12bae472010-11-30 12:01:02 +00001333 * tipc_send_buf2port - send message buffer to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001334 */
1335
Allan Stephens12bae472010-11-30 12:01:02 +00001336int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
1337 struct sk_buff *buf, unsigned int dsz)
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338{
1339 struct port *p_ptr;
1340 struct tipc_msg *msg;
1341 int res;
1342
Per Liden4323add2006-01-18 00:38:21 +01001343 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 if (!p_ptr || p_ptr->publ.connected)
1345 return -EINVAL;
1346
1347 msg = &p_ptr->publ.phdr;
1348 msg_set_type(msg, TIPC_DIRECT_MSG);
Allan Stephens12bae472010-11-30 12:01:02 +00001349 msg_set_orignode(msg, tipc_own_addr);
1350 msg_set_origport(msg, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 msg_set_destnode(msg, dest->node);
1352 msg_set_destport(msg, dest->ref);
1353 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001354 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1355 if (skb_cow(buf, DIR_MSG_H_SIZE))
1356 return -ENOMEM;
1357
1358 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001359 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001360 msg_dbg(msg, "buf2port: ");
1361 p_ptr->sent++;
1362 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001363 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001364 res = tipc_send_buf_fast(buf, dest->node);
1365 if (likely(res != -ELINKCONG))
1366 return res;
1367 if (port_unreliable(p_ptr))
1368 return dsz;
1369 return -ELINKCONG;
1370}
1371