blob: 2a16ca43e9534fb3a1c64a0f214700265ecbbb6d [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
5 * Copyright (c) 2004-2007, 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"
39#include "dbg.h"
40#include "port.h"
41#include "addr.h"
42#include "link.h"
43#include "node.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010044#include "name_table.h"
45#include "user_reg.h"
46#include "msg.h"
47#include "bcast.h"
48
49/* Connection management: */
50#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
51#define CONFIRMED 0
52#define PROBING 1
53
54#define MAX_REJECT_SIZE 1024
55
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080056static struct sk_buff *msg_queue_head = NULL;
57static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058
Ingo Molnar34af9462006-06-27 02:53:55 -070059DEFINE_SPINLOCK(tipc_port_list_lock);
60static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010061
Per Liden4323add2006-01-18 00:38:21 +010062static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010063static void port_handle_node_down(unsigned long ref);
64static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
65static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
66static void port_timeout(unsigned long ref);
67
68
Sam Ravnborg05790c62006-03-20 22:37:04 -080069static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010070{
71 return msg_destnode(&p_ptr->publ.phdr);
72}
73
Sam Ravnborg05790c62006-03-20 22:37:04 -080074static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010075{
76 return msg_destport(&p_ptr->publ.phdr);
77}
78
Sam Ravnborg05790c62006-03-20 22:37:04 -080079static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010080{
81 return msg_transp_seqno(&p_ptr->publ.phdr);
82}
83
Sam Ravnborg05790c62006-03-20 22:37:04 -080084static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010085{
86 struct tipc_msg *m = &p_ptr->publ.phdr;
87
88 if (likely(!msg_routed(m)))
89 return;
90 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
91}
92
93/**
94 * tipc_multicast - send a multicast message to local and remote destinations
95 */
96
97int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
98 u32 num_sect, struct iovec const *msg_sect)
99{
100 struct tipc_msg *hdr;
101 struct sk_buff *buf;
102 struct sk_buff *ibuf = NULL;
103 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +0100104 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 int ext_targets;
106 int res;
107
108 if (unlikely(!oport))
109 return -EINVAL;
110
111 /* Create multicast message */
112
113 hdr = &oport->publ.phdr;
114 msg_set_type(hdr, TIPC_MCAST_MSG);
115 msg_set_nametype(hdr, seq->type);
116 msg_set_namelower(hdr, seq->lower);
117 msg_set_nameupper(hdr, seq->upper);
118 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
119 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
120 !oport->user_port, &buf);
121 if (unlikely(!buf))
122 return res;
123
124 /* Figure out where to send multicast message */
125
Per Liden4323add2006-01-18 00:38:21 +0100126 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
127 TIPC_NODE_SCOPE, &dports);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900128
129 /* Send message to destinations (duplicate it only if necessary) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100130
131 if (ext_targets) {
132 if (dports.count != 0) {
133 ibuf = skb_copy(buf, GFP_ATOMIC);
134 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100135 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100136 buf_discard(buf);
137 return -ENOMEM;
138 }
139 }
Per Liden4323add2006-01-18 00:38:21 +0100140 res = tipc_bclink_send_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 if ((res < 0) && (dports.count != 0)) {
142 buf_discard(ibuf);
143 }
144 } else {
145 ibuf = buf;
146 }
147
148 if (res >= 0) {
149 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100150 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 } else {
Per Liden4323add2006-01-18 00:38:21 +0100152 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 }
154 return res;
155}
156
157/**
Per Liden4323add2006-01-18 00:38:21 +0100158 * tipc_port_recv_mcast - deliver multicast message to all destination ports
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900159 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 * If there is no port list, perform a lookup to create one
161 */
162
Per Liden4323add2006-01-18 00:38:21 +0100163void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100164{
165 struct tipc_msg* msg;
166 struct port_list dports = {0, NULL, };
167 struct port_list *item = dp;
168 int cnt = 0;
169
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 msg = buf_msg(buf);
171
172 /* Create destination port list, if one wasn't supplied */
173
174 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100175 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100176 msg_namelower(msg),
177 msg_nameupper(msg),
178 TIPC_CLUSTER_SCOPE,
179 &dports);
180 item = dp = &dports;
181 }
182
183 /* Deliver a copy of message to each destination port */
184
185 if (dp->count != 0) {
186 if (dp->count == 1) {
187 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100188 tipc_port_recv_msg(buf);
189 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190 return;
191 }
192 for (; cnt < dp->count; cnt++) {
193 int index = cnt % PLSIZE;
194 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
195
196 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700197 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 msg_dbg(msg, "LOST:");
199 goto exit;
200 }
201 if ((index == 0) && (cnt != 0)) {
202 item = item->next;
203 }
204 msg_set_destport(buf_msg(b),item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100205 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100206 }
207 }
208exit:
209 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100210 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211}
212
213/**
214 * tipc_createport_raw - create a native TIPC port
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900215 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100216 * Returns local port reference
217 */
218
219u32 tipc_createport_raw(void *usr_handle,
220 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
221 void (*wakeup)(struct tipc_port *),
222 const u32 importance)
223{
224 struct port *p_ptr;
225 struct tipc_msg *msg;
226 u32 ref;
227
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700228 p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700229 if (!p_ptr) {
230 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 return 0;
232 }
Per Liden4323add2006-01-18 00:38:21 +0100233 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700235 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 kfree(p_ptr);
237 return 0;
238 }
239
Per Liden4323add2006-01-18 00:38:21 +0100240 tipc_port_lock(ref);
Allan Stephens05646c92007-06-10 17:25:24 -0700241 p_ptr->publ.usr_handle = usr_handle;
242 p_ptr->publ.max_pkt = MAX_PKT_DEFAULT;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 p_ptr->publ.ref = ref;
244 msg = &p_ptr->publ.phdr;
245 msg_init(msg, DATA_LOW, TIPC_NAMED_MSG, TIPC_OK, LONG_H_SIZE, 0);
246 msg_set_orignode(msg, tipc_own_addr);
247 msg_set_prevnode(msg, tipc_own_addr);
248 msg_set_origport(msg, ref);
249 msg_set_importance(msg,importance);
250 p_ptr->last_in_seqno = 41;
251 p_ptr->sent = 1;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100252 INIT_LIST_HEAD(&p_ptr->wait_list);
253 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800254 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 p_ptr->dispatcher = dispatcher;
256 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800257 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100259 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 INIT_LIST_HEAD(&p_ptr->publications);
261 INIT_LIST_HEAD(&p_ptr->port_list);
262 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100263 spin_unlock_bh(&tipc_port_list_lock);
264 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265 return ref;
266}
267
268int tipc_deleteport(u32 ref)
269{
270 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800271 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100272
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800273 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100274 p_ptr = tipc_port_lock(ref);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900275 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100276 return -EINVAL;
277
Per Liden4323add2006-01-18 00:38:21 +0100278 tipc_ref_discard(ref);
279 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100280
281 k_cancel_timer(&p_ptr->timer);
282 if (p_ptr->publ.connected) {
283 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100284 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100285 }
286 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100287 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100288 kfree(p_ptr->user_port);
289 }
290
Per Liden4323add2006-01-18 00:38:21 +0100291 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292 list_del(&p_ptr->port_list);
293 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100294 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100295 k_term_timer(&p_ptr->timer);
296 kfree(p_ptr);
297 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100298 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100299 return TIPC_OK;
300}
301
302/**
303 * tipc_get_port() - return port associated with 'ref'
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900304 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100305 * Note: Port is not locked.
306 */
307
308struct tipc_port *tipc_get_port(const u32 ref)
309{
Per Liden4323add2006-01-18 00:38:21 +0100310 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100311}
312
313/**
314 * tipc_get_handle - return user handle associated to port 'ref'
315 */
316
317void *tipc_get_handle(const u32 ref)
318{
319 struct port *p_ptr;
320 void * handle;
321
Per Liden4323add2006-01-18 00:38:21 +0100322 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100323 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800324 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100326 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 return handle;
328}
329
Sam Ravnborg05790c62006-03-20 22:37:04 -0800330static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331{
332 return msg_src_droppable(&p_ptr->publ.phdr);
333}
334
335int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
336{
337 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900338
Per Liden4323add2006-01-18 00:38:21 +0100339 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 if (!p_ptr)
341 return -EINVAL;
342 *isunreliable = port_unreliable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800343 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 return TIPC_OK;
345}
346
347int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
348{
349 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900350
Per Liden4323add2006-01-18 00:38:21 +0100351 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100352 if (!p_ptr)
353 return -EINVAL;
354 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100355 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 return TIPC_OK;
357}
358
Sam Ravnborg05790c62006-03-20 22:37:04 -0800359static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360{
361 return msg_dest_droppable(&p_ptr->publ.phdr);
362}
363
364int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
365{
366 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900367
Per Liden4323add2006-01-18 00:38:21 +0100368 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 if (!p_ptr)
370 return -EINVAL;
371 *isunrejectable = port_unreturnable(p_ptr);
Julia Lawall4cec72c2008-01-08 23:48:20 -0800372 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 return TIPC_OK;
374}
375
376int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
377{
378 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900379
Per Liden4323add2006-01-18 00:38:21 +0100380 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100381 if (!p_ptr)
382 return -EINVAL;
383 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100384 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100385 return TIPC_OK;
386}
387
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900388/*
389 * port_build_proto_msg(): build a port level protocol
390 * or a connection abortion message. Called with
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391 * tipc_port lock on.
392 */
393static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
394 u32 origport, u32 orignode,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900395 u32 usr, u32 type, u32 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100396 u32 seqno, u32 ack)
397{
398 struct sk_buff *buf;
399 struct tipc_msg *msg;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900400
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 buf = buf_acquire(LONG_H_SIZE);
402 if (buf) {
403 msg = buf_msg(buf);
404 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
405 msg_set_destport(msg, destport);
406 msg_set_origport(msg, origport);
407 msg_set_destnode(msg, destnode);
408 msg_set_orignode(msg, orignode);
409 msg_set_transp_seqno(msg, seqno);
410 msg_set_msgcnt(msg, ack);
411 msg_dbg(msg, "PORT>SEND>:");
412 }
413 return buf;
414}
415
Per Lidenb97bf3f2006-01-02 19:04:38 +0100416int tipc_reject_msg(struct sk_buff *buf, u32 err)
417{
418 struct tipc_msg *msg = buf_msg(buf);
419 struct sk_buff *rbuf;
420 struct tipc_msg *rmsg;
421 int hdr_sz;
422 u32 imp = msg_importance(msg);
423 u32 data_sz = msg_data_sz(msg);
424
425 if (data_sz > MAX_REJECT_SIZE)
426 data_sz = MAX_REJECT_SIZE;
427 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
428 imp++;
429 msg_dbg(msg, "port->rej: ");
430
431 /* discard rejected message if it shouldn't be returned to sender */
432 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
433 buf_discard(buf);
434 return data_sz;
435 }
436
437 /* construct rejected message */
438 if (msg_mcast(msg))
439 hdr_sz = MCAST_H_SIZE;
440 else
441 hdr_sz = LONG_H_SIZE;
442 rbuf = buf_acquire(data_sz + hdr_sz);
443 if (rbuf == NULL) {
444 buf_discard(buf);
445 return data_sz;
446 }
447 rmsg = buf_msg(rbuf);
448 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
449 msg_set_destport(rmsg, msg_origport(msg));
450 msg_set_prevnode(rmsg, tipc_own_addr);
451 msg_set_origport(rmsg, msg_destport(msg));
452 if (msg_short(msg))
453 msg_set_orignode(rmsg, tipc_own_addr);
454 else
455 msg_set_orignode(rmsg, msg_destnode(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900456 msg_set_size(rmsg, data_sz + hdr_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 msg_set_nametype(rmsg, msg_nametype(msg));
458 msg_set_nameinst(rmsg, msg_nameinst(msg));
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -0300459 skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100460
461 /* send self-abort message when rejecting on a connected port */
462 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800463 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100464 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465
466 if (p_ptr) {
467 if (p_ptr->publ.connected)
468 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100469 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470 }
Per Liden4323add2006-01-18 00:38:21 +0100471 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100472 }
473
474 /* send rejected message */
475 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100476 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 return data_sz;
478}
479
Per Liden4323add2006-01-18 00:38:21 +0100480int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
481 struct iovec const *msg_sect, u32 num_sect,
482 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483{
484 struct sk_buff *buf;
485 int res;
486
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900487 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 !p_ptr->user_port, &buf);
489 if (!buf)
490 return res;
491
492 return tipc_reject_msg(buf, err);
493}
494
495static void port_timeout(unsigned long ref)
496{
Per Liden4323add2006-01-18 00:38:21 +0100497 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800498 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499
Allan Stephens065fd172006-10-16 21:38:05 -0700500 if (!p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 return;
502
Allan Stephens065fd172006-10-16 21:38:05 -0700503 if (!p_ptr->publ.connected) {
504 tipc_port_unlock(p_ptr);
505 return;
506 }
507
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 /* Last probe answered ? */
509 if (p_ptr->probing_state == PROBING) {
510 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
511 } else {
512 buf = port_build_proto_msg(port_peerport(p_ptr),
513 port_peernode(p_ptr),
514 p_ptr->publ.ref,
515 tipc_own_addr,
516 CONN_MANAGER,
517 CONN_PROBE,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900518 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100519 port_out_seqno(p_ptr),
520 0);
521 port_incr_out_seqno(p_ptr);
522 p_ptr->probing_state = PROBING;
523 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
524 }
Per Liden4323add2006-01-18 00:38:21 +0100525 tipc_port_unlock(p_ptr);
526 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100527}
528
529
530static void port_handle_node_down(unsigned long ref)
531{
Per Liden4323add2006-01-18 00:38:21 +0100532 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800533 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100534
535 if (!p_ptr)
536 return;
537 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100538 tipc_port_unlock(p_ptr);
539 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
542
543static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
544{
545 u32 imp = msg_importance(&p_ptr->publ.phdr);
546
547 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800548 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 if (imp < TIPC_CRITICAL_IMPORTANCE)
550 imp++;
551 return port_build_proto_msg(p_ptr->publ.ref,
552 tipc_own_addr,
553 port_peerport(p_ptr),
554 port_peernode(p_ptr),
555 imp,
556 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900557 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 p_ptr->last_in_seqno + 1,
559 0);
560}
561
562
563static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
564{
565 u32 imp = msg_importance(&p_ptr->publ.phdr);
566
567 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800568 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100569 if (imp < TIPC_CRITICAL_IMPORTANCE)
570 imp++;
571 return port_build_proto_msg(port_peerport(p_ptr),
572 port_peernode(p_ptr),
573 p_ptr->publ.ref,
574 tipc_own_addr,
575 imp,
576 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577 err,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 port_out_seqno(p_ptr),
579 0);
580}
581
Per Liden4323add2006-01-18 00:38:21 +0100582void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100583{
584 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100585 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100586 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800587 struct sk_buff *r_buf = NULL;
588 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589
590 msg_dbg(msg, "PORT<RECV<:");
591
592 if (!p_ptr) {
593 err = TIPC_ERR_NO_PORT;
594 } else if (p_ptr->publ.connected) {
595 if (port_peernode(p_ptr) != msg_orignode(msg))
596 err = TIPC_ERR_NO_PORT;
597 if (port_peerport(p_ptr) != msg_origport(msg))
598 err = TIPC_ERR_NO_PORT;
599 if (!err && msg_routed(msg)) {
600 u32 seqno = msg_transp_seqno(msg);
601 u32 myno = ++p_ptr->last_in_seqno;
602 if (seqno != myno) {
603 err = TIPC_ERR_NO_PORT;
604 abort_buf = port_build_self_abort_msg(p_ptr, err);
605 }
606 }
607 if (msg_type(msg) == CONN_ACK) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900608 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100609 p_ptr->publ.congested &&
610 p_ptr->wakeup;
611 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100612 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 goto exit;
614 p_ptr->publ.congested = 0;
615 if (!wakeup)
616 goto exit;
617 p_ptr->wakeup(&p_ptr->publ);
618 goto exit;
619 }
620 } else if (p_ptr->publ.published) {
621 err = TIPC_ERR_NO_PORT;
622 }
623 if (err) {
624 r_buf = port_build_proto_msg(msg_origport(msg),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900625 msg_orignode(msg),
626 msg_destport(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100627 tipc_own_addr,
628 DATA_HIGH,
629 TIPC_CONN_MSG,
630 err,
631 0,
632 0);
633 goto exit;
634 }
635
636 /* All is fine */
637 if (msg_type(msg) == CONN_PROBE) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900638 r_buf = port_build_proto_msg(msg_origport(msg),
639 msg_orignode(msg),
640 msg_destport(msg),
641 tipc_own_addr,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100642 CONN_MANAGER,
643 CONN_PROBE_REPLY,
644 TIPC_OK,
645 port_out_seqno(p_ptr),
646 0);
647 }
648 p_ptr->probing_state = CONFIRMED;
649 port_incr_out_seqno(p_ptr);
650exit:
651 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100652 tipc_port_unlock(p_ptr);
653 tipc_net_route_msg(r_buf);
654 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100655 buf_discard(buf);
656}
657
658static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
659{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900660 struct publication *publ;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100661
662 if (full_id)
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900663 tipc_printf(buf, "<%u.%u.%u:%u>:",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100664 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900665 tipc_node(tipc_own_addr), p_ptr->publ.ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100666 else
667 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
668
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900669 if (p_ptr->publ.connected) {
670 u32 dport = port_peerport(p_ptr);
671 u32 destnode = port_peernode(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100672
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900673 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
674 tipc_zone(destnode), tipc_cluster(destnode),
675 tipc_node(destnode), dport);
676 if (p_ptr->publ.conn_type != 0)
677 tipc_printf(buf, " via {%u,%u}",
678 p_ptr->publ.conn_type,
679 p_ptr->publ.conn_instance);
680 }
681 else if (p_ptr->publ.published) {
682 tipc_printf(buf, " bound to");
683 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100684 if (publ->lower == publ->upper)
685 tipc_printf(buf, " {%u,%u}", publ->type,
686 publ->lower);
687 else
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900688 tipc_printf(buf, " {%u,%u,%u}", publ->type,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100689 publ->lower, publ->upper);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900690 }
691 }
692 tipc_printf(buf, "\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100693}
694
695#define MAX_PORT_QUERY 32768
696
Per Liden4323add2006-01-18 00:38:21 +0100697struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100698{
699 struct sk_buff *buf;
700 struct tlv_desc *rep_tlv;
701 struct print_buf pb;
702 struct port *p_ptr;
703 int str_len;
704
Per Liden4323add2006-01-18 00:38:21 +0100705 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100706 if (!buf)
707 return NULL;
708 rep_tlv = (struct tlv_desc *)buf->data;
709
Per Liden4323add2006-01-18 00:38:21 +0100710 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
711 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100712 list_for_each_entry(p_ptr, &ports, port_list) {
713 spin_lock_bh(p_ptr->publ.lock);
714 port_print(p_ptr, &pb, 0);
715 spin_unlock_bh(p_ptr->publ.lock);
716 }
Per Liden4323add2006-01-18 00:38:21 +0100717 spin_unlock_bh(&tipc_port_list_lock);
718 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100719
720 skb_put(buf, TLV_SPACE(str_len));
721 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
722
723 return buf;
724}
725
726#if 0
727
728#define MAX_PORT_STATS 2000
729
730struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
731{
732 u32 ref;
733 struct port *p_ptr;
734 struct sk_buff *buf;
735 struct tlv_desc *rep_tlv;
736 struct print_buf pb;
737 int str_len;
738
739 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
740 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
741
742 ref = *(u32 *)TLV_DATA(req_tlv_area);
743 ref = ntohl(ref);
744
Per Liden4323add2006-01-18 00:38:21 +0100745 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100746 if (!p_ptr)
747 return cfg_reply_error_string("port not found");
748
Per Liden4323add2006-01-18 00:38:21 +0100749 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100751 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100752 return NULL;
753 }
754 rep_tlv = (struct tlv_desc *)buf->data;
755
Per Liden4323add2006-01-18 00:38:21 +0100756 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100757 port_print(p_ptr, &pb, 1);
758 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100759 tipc_port_unlock(p_ptr);
760 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761
762 skb_put(buf, TLV_SPACE(str_len));
763 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
764
765 return buf;
766}
767
768#endif
769
Per Liden4323add2006-01-18 00:38:21 +0100770void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100771{
772 struct port *p_ptr;
773 struct tipc_msg *msg;
774
Per Liden4323add2006-01-18 00:38:21 +0100775 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100776 list_for_each_entry(p_ptr, &ports, port_list) {
777 msg = &p_ptr->publ.phdr;
778 if (msg_orignode(msg) == tipc_own_addr)
779 break;
780 msg_set_orignode(msg, tipc_own_addr);
781 }
Per Liden4323add2006-01-18 00:38:21 +0100782 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100783}
784
785
786/*
787 * port_dispatcher_sigh(): Signal handler for messages destinated
788 * to the tipc_port interface.
789 */
790
791static void port_dispatcher_sigh(void *dummy)
792{
793 struct sk_buff *buf;
794
795 spin_lock_bh(&queue_lock);
796 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800797 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100798 spin_unlock_bh(&queue_lock);
799
800 while (buf) {
801 struct port *p_ptr;
802 struct user_port *up_ptr;
803 struct tipc_portid orig;
804 struct tipc_name_seq dseq;
805 void *usr_handle;
806 int connected;
807 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700808 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100809
810 struct sk_buff *next = buf->next;
811 struct tipc_msg *msg = buf_msg(buf);
812 u32 dref = msg_destport(msg);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900813
Allan Stephens96882432006-06-25 23:38:58 -0700814 message_type = msg_type(msg);
815 if (message_type > TIPC_DIRECT_MSG)
816 goto reject; /* Unsupported message type */
817
Per Liden4323add2006-01-18 00:38:21 +0100818 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700819 if (!p_ptr)
820 goto reject; /* Port deleted while msg in queue */
821
Per Lidenb97bf3f2006-01-02 19:04:38 +0100822 orig.ref = msg_origport(msg);
823 orig.node = msg_orignode(msg);
824 up_ptr = p_ptr->user_port;
825 usr_handle = up_ptr->usr_handle;
826 connected = p_ptr->publ.connected;
827 published = p_ptr->publ.published;
828
829 if (unlikely(msg_errcode(msg)))
830 goto err;
831
Allan Stephens96882432006-06-25 23:38:58 -0700832 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900833
Per Lidenb97bf3f2006-01-02 19:04:38 +0100834 case TIPC_CONN_MSG:{
835 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
836 u32 peer_port = port_peerport(p_ptr);
837 u32 peer_node = port_peernode(p_ptr);
838
Julia Lawall4cec72c2008-01-08 23:48:20 -0800839 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100840 if (unlikely(!connected)) {
841 if (unlikely(published))
842 goto reject;
843 tipc_connect2port(dref,&orig);
844 }
845 if (unlikely(msg_origport(msg) != peer_port))
846 goto reject;
847 if (unlikely(msg_orignode(msg) != peer_node))
848 goto reject;
849 if (unlikely(!cb))
850 goto reject;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900851 if (unlikely(++p_ptr->publ.conn_unacked >=
Per Lidenb97bf3f2006-01-02 19:04:38 +0100852 TIPC_FLOW_CONTROL_WIN))
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900853 tipc_acknowledge(dref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100854 p_ptr->publ.conn_unacked);
855 skb_pull(buf, msg_hdr_sz(msg));
856 cb(usr_handle, dref, &buf, msg_data(msg),
857 msg_data_sz(msg));
858 break;
859 }
860 case TIPC_DIRECT_MSG:{
861 tipc_msg_event cb = up_ptr->msg_cb;
862
Julia Lawall4cec72c2008-01-08 23:48:20 -0800863 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100864 if (unlikely(connected))
865 goto reject;
866 if (unlikely(!cb))
867 goto reject;
868 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900869 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100870 msg_data_sz(msg), msg_importance(msg),
871 &orig);
872 break;
873 }
Allan Stephens96882432006-06-25 23:38:58 -0700874 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100875 case TIPC_NAMED_MSG:{
876 tipc_named_msg_event cb = up_ptr->named_msg_cb;
877
Julia Lawall4cec72c2008-01-08 23:48:20 -0800878 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 if (unlikely(connected))
880 goto reject;
881 if (unlikely(!cb))
882 goto reject;
883 if (unlikely(!published))
884 goto reject;
885 dseq.type = msg_nametype(msg);
886 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700887 dseq.upper = (message_type == TIPC_NAMED_MSG)
888 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100889 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900890 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100891 msg_data_sz(msg), msg_importance(msg),
892 &orig, &dseq);
893 break;
894 }
895 }
896 if (buf)
897 buf_discard(buf);
898 buf = next;
899 continue;
900err:
Allan Stephens96882432006-06-25 23:38:58 -0700901 switch (message_type) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900902
Per Lidenb97bf3f2006-01-02 19:04:38 +0100903 case TIPC_CONN_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900904 tipc_conn_shutdown_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100905 up_ptr->conn_err_cb;
906 u32 peer_port = port_peerport(p_ptr);
907 u32 peer_node = port_peernode(p_ptr);
908
Julia Lawall4cec72c2008-01-08 23:48:20 -0800909 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100910 if (!connected || !cb)
911 break;
912 if (msg_origport(msg) != peer_port)
913 break;
914 if (msg_orignode(msg) != peer_node)
915 break;
916 tipc_disconnect(dref);
917 skb_pull(buf, msg_hdr_sz(msg));
918 cb(usr_handle, dref, &buf, msg_data(msg),
919 msg_data_sz(msg), msg_errcode(msg));
920 break;
921 }
922 case TIPC_DIRECT_MSG:{
923 tipc_msg_err_event cb = up_ptr->err_cb;
924
Julia Lawall4cec72c2008-01-08 23:48:20 -0800925 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100926 if (connected || !cb)
927 break;
928 skb_pull(buf, msg_hdr_sz(msg));
929 cb(usr_handle, dref, &buf, msg_data(msg),
930 msg_data_sz(msg), msg_errcode(msg), &orig);
931 break;
932 }
Allan Stephens96882432006-06-25 23:38:58 -0700933 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100934 case TIPC_NAMED_MSG:{
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900935 tipc_named_msg_err_event cb =
Per Lidenb97bf3f2006-01-02 19:04:38 +0100936 up_ptr->named_err_cb;
937
Julia Lawall4cec72c2008-01-08 23:48:20 -0800938 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100939 if (connected || !cb)
940 break;
941 dseq.type = msg_nametype(msg);
942 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700943 dseq.upper = (message_type == TIPC_NAMED_MSG)
944 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100945 skb_pull(buf, msg_hdr_sz(msg));
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900946 cb(usr_handle, dref, &buf, msg_data(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100947 msg_data_sz(msg), msg_errcode(msg), &dseq);
948 break;
949 }
950 }
951 if (buf)
952 buf_discard(buf);
953 buf = next;
954 continue;
955reject:
956 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
957 buf = next;
958 }
959}
960
961/*
962 * port_dispatcher(): Dispatcher for messages destinated
963 * to the tipc_port interface. Called with port locked.
964 */
965
966static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
967{
968 buf->next = NULL;
969 spin_lock_bh(&queue_lock);
970 if (msg_queue_head) {
971 msg_queue_tail->next = buf;
972 msg_queue_tail = buf;
973 } else {
974 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100975 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100976 }
977 spin_unlock_bh(&queue_lock);
978 return TIPC_OK;
979}
980
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900981/*
Per Lidenb97bf3f2006-01-02 19:04:38 +0100982 * Wake up port after congestion: Called with port locked,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900983 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100984 */
985
986static void port_wakeup_sh(unsigned long ref)
987{
988 struct port *p_ptr;
989 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800990 tipc_continue_event cb = NULL;
991 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100992
Per Liden4323add2006-01-18 00:38:21 +0100993 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100994 if (p_ptr) {
995 up_ptr = p_ptr->user_port;
996 if (up_ptr) {
997 cb = up_ptr->continue_event_cb;
998 uh = up_ptr->usr_handle;
999 }
Per Liden4323add2006-01-18 00:38:21 +01001000 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001001 }
1002 if (cb)
1003 cb(uh, ref);
1004}
1005
1006
1007static void port_wakeup(struct tipc_port *p_ptr)
1008{
Per Liden4323add2006-01-18 00:38:21 +01001009 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001010}
1011
1012void tipc_acknowledge(u32 ref, u32 ack)
1013{
1014 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001015 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001016
Per Liden4323add2006-01-18 00:38:21 +01001017 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001018 if (!p_ptr)
1019 return;
1020 if (p_ptr->publ.connected) {
1021 p_ptr->publ.conn_unacked -= ack;
1022 buf = port_build_proto_msg(port_peerport(p_ptr),
1023 port_peernode(p_ptr),
1024 ref,
1025 tipc_own_addr,
1026 CONN_MANAGER,
1027 CONN_ACK,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001028 TIPC_OK,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001029 port_out_seqno(p_ptr),
1030 ack);
1031 }
Per Liden4323add2006-01-18 00:38:21 +01001032 tipc_port_unlock(p_ptr);
1033 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001034}
1035
1036/*
1037 * tipc_createport(): user level call. Will add port to
1038 * registry if non-zero user_ref.
1039 */
1040
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001041int tipc_createport(u32 user_ref,
1042 void *usr_handle,
1043 unsigned int importance,
1044 tipc_msg_err_event error_cb,
1045 tipc_named_msg_err_event named_error_cb,
1046 tipc_conn_shutdown_event conn_error_cb,
1047 tipc_msg_event msg_cb,
1048 tipc_named_msg_event named_msg_cb,
1049 tipc_conn_msg_event conn_msg_cb,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001050 tipc_continue_event continue_event_cb,/* May be zero */
1051 u32 *portref)
1052{
1053 struct user_port *up_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001054 struct port *p_ptr;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001055 u32 ref;
1056
Panagiotis Issaris0da974f2006-07-21 14:51:30 -07001057 up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001058 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001059 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001060 return -ENOMEM;
1061 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001062 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
Per Liden4323add2006-01-18 00:38:21 +01001063 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 if (!p_ptr) {
1065 kfree(up_ptr);
1066 return -ENOMEM;
1067 }
1068
1069 p_ptr->user_port = up_ptr;
1070 up_ptr->user_ref = user_ref;
1071 up_ptr->usr_handle = usr_handle;
1072 up_ptr->ref = p_ptr->publ.ref;
1073 up_ptr->err_cb = error_cb;
1074 up_ptr->named_err_cb = named_error_cb;
1075 up_ptr->conn_err_cb = conn_error_cb;
1076 up_ptr->msg_cb = msg_cb;
1077 up_ptr->named_msg_cb = named_msg_cb;
1078 up_ptr->conn_msg_cb = conn_msg_cb;
1079 up_ptr->continue_event_cb = continue_event_cb;
1080 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001081 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001082 *portref = p_ptr->publ.ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001083 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
Per Liden4323add2006-01-18 00:38:21 +01001084 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001085 return TIPC_OK;
1086}
1087
1088int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1089{
1090 id->ref = ref;
1091 id->node = tipc_own_addr;
1092 return TIPC_OK;
1093}
1094
1095int tipc_portimportance(u32 ref, unsigned int *importance)
1096{
1097 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001098
Per Liden4323add2006-01-18 00:38:21 +01001099 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001100 if (!p_ptr)
1101 return -EINVAL;
1102 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001103 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 return TIPC_OK;
1105}
1106
1107int tipc_set_portimportance(u32 ref, unsigned int imp)
1108{
1109 struct port *p_ptr;
1110
1111 if (imp > TIPC_CRITICAL_IMPORTANCE)
1112 return -EINVAL;
1113
Per Liden4323add2006-01-18 00:38:21 +01001114 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001115 if (!p_ptr)
1116 return -EINVAL;
1117 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
Julia Lawall4cec72c2008-01-08 23:48:20 -08001118 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 return TIPC_OK;
1120}
1121
1122
1123int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1124{
1125 struct port *p_ptr;
1126 struct publication *publ;
1127 u32 key;
1128 int res = -EINVAL;
1129
Per Liden4323add2006-01-18 00:38:21 +01001130 p_ptr = tipc_port_lock(ref);
Adrian Bunkd55b4c62006-10-31 16:59:35 -08001131 if (!p_ptr)
1132 return -EINVAL;
1133
Per Lidenb97bf3f2006-01-02 19:04:38 +01001134 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1135 "lower = %u, upper = %u\n",
1136 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001137 if (p_ptr->publ.connected)
1138 goto exit;
1139 if (seq->lower > seq->upper)
1140 goto exit;
1141 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1142 goto exit;
1143 key = ref + p_ptr->pub_count + 1;
1144 if (key == ref) {
1145 res = -EADDRINUSE;
1146 goto exit;
1147 }
Per Liden4323add2006-01-18 00:38:21 +01001148 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1149 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001150 if (publ) {
1151 list_add(&publ->pport_list, &p_ptr->publications);
1152 p_ptr->pub_count++;
1153 p_ptr->publ.published = 1;
1154 res = TIPC_OK;
1155 }
1156exit:
Per Liden4323add2006-01-18 00:38:21 +01001157 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001158 return res;
1159}
1160
1161int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1162{
1163 struct port *p_ptr;
1164 struct publication *publ;
1165 struct publication *tpubl;
1166 int res = -EINVAL;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001167
Per Liden4323add2006-01-18 00:38:21 +01001168 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001169 if (!p_ptr)
1170 return -EINVAL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001171 if (!seq) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001172 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001173 &p_ptr->publications, pport_list) {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001174 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001175 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001176 }
1177 res = TIPC_OK;
1178 } else {
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001179 list_for_each_entry_safe(publ, tpubl,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001180 &p_ptr->publications, pport_list) {
1181 if (publ->scope != scope)
1182 continue;
1183 if (publ->type != seq->type)
1184 continue;
1185 if (publ->lower != seq->lower)
1186 continue;
1187 if (publ->upper != seq->upper)
1188 break;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001189 tipc_nametbl_withdraw(publ->type, publ->lower,
Per Liden4323add2006-01-18 00:38:21 +01001190 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001191 res = TIPC_OK;
1192 break;
1193 }
1194 }
1195 if (list_empty(&p_ptr->publications))
1196 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001197 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001198 return res;
1199}
1200
1201int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1202{
1203 struct port *p_ptr;
1204 struct tipc_msg *msg;
1205 int res = -EINVAL;
1206
Per Liden4323add2006-01-18 00:38:21 +01001207 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001208 if (!p_ptr)
1209 return -EINVAL;
1210 if (p_ptr->publ.published || p_ptr->publ.connected)
1211 goto exit;
1212 if (!peer->ref)
1213 goto exit;
1214
1215 msg = &p_ptr->publ.phdr;
1216 msg_set_destnode(msg, peer->node);
1217 msg_set_destport(msg, peer->ref);
1218 msg_set_orignode(msg, tipc_own_addr);
1219 msg_set_origport(msg, p_ptr->publ.ref);
1220 msg_set_transp_seqno(msg, 42);
1221 msg_set_type(msg, TIPC_CONN_MSG);
1222 if (!may_route(peer->node))
1223 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1224 else
1225 msg_set_hdr_sz(msg, LONG_H_SIZE);
1226
1227 p_ptr->probing_interval = PROBING_INTERVAL;
1228 p_ptr->probing_state = CONFIRMED;
1229 p_ptr->publ.connected = 1;
1230 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1231
Per Liden4323add2006-01-18 00:38:21 +01001232 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001233 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001234 (net_ev_handler)port_handle_node_down);
1235 res = TIPC_OK;
1236exit:
Per Liden4323add2006-01-18 00:38:21 +01001237 tipc_port_unlock(p_ptr);
Allan Stephens05646c92007-06-10 17:25:24 -07001238 p_ptr->publ.max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001239 return res;
1240}
1241
1242/*
1243 * tipc_disconnect(): Disconnect port form peer.
1244 * This is a node local operation.
1245 */
1246
1247int tipc_disconnect(u32 ref)
1248{
1249 struct port *p_ptr;
1250 int res = -ENOTCONN;
1251
Per Liden4323add2006-01-18 00:38:21 +01001252 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001253 if (!p_ptr)
1254 return -EINVAL;
1255 if (p_ptr->publ.connected) {
1256 p_ptr->publ.connected = 0;
1257 /* let timer expire on it's own to avoid deadlock! */
Per Liden4323add2006-01-18 00:38:21 +01001258 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001259 res = TIPC_OK;
1260 }
Per Liden4323add2006-01-18 00:38:21 +01001261 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 return res;
1263}
1264
1265/*
1266 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1267 */
1268int tipc_shutdown(u32 ref)
1269{
1270 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001271 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001272
Per Liden4323add2006-01-18 00:38:21 +01001273 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001274 if (!p_ptr)
1275 return -EINVAL;
1276
1277 if (p_ptr->publ.connected) {
1278 u32 imp = msg_importance(&p_ptr->publ.phdr);
1279 if (imp < TIPC_CRITICAL_IMPORTANCE)
1280 imp++;
1281 buf = port_build_proto_msg(port_peerport(p_ptr),
1282 port_peernode(p_ptr),
1283 ref,
1284 tipc_own_addr,
1285 imp,
1286 TIPC_CONN_MSG,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001287 TIPC_CONN_SHUTDOWN,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001288 port_out_seqno(p_ptr),
1289 0);
1290 }
Per Liden4323add2006-01-18 00:38:21 +01001291 tipc_port_unlock(p_ptr);
1292 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001293 return tipc_disconnect(ref);
1294}
1295
1296int tipc_isconnected(u32 ref, int *isconnected)
1297{
1298 struct port *p_ptr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001299
Per Liden4323add2006-01-18 00:38:21 +01001300 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001301 if (!p_ptr)
1302 return -EINVAL;
1303 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001304 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001305 return TIPC_OK;
1306}
1307
1308int tipc_peer(u32 ref, struct tipc_portid *peer)
1309{
1310 struct port *p_ptr;
1311 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001312
Per Liden4323add2006-01-18 00:38:21 +01001313 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001314 if (!p_ptr)
1315 return -EINVAL;
1316 if (p_ptr->publ.connected) {
1317 peer->ref = port_peerport(p_ptr);
1318 peer->node = port_peernode(p_ptr);
1319 res = TIPC_OK;
1320 } else
1321 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001322 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001323 return res;
1324}
1325
1326int tipc_ref_valid(u32 ref)
1327{
1328 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001329 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001330}
1331
1332
1333/*
Per Liden4323add2006-01-18 00:38:21 +01001334 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001335 * message for this node.
1336 */
1337
Per Liden4323add2006-01-18 00:38:21 +01001338int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001339 struct iovec const *msg_sect)
1340{
1341 struct sk_buff *buf;
1342 int res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001343
Per Lidenb97bf3f2006-01-02 19:04:38 +01001344 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1345 MAX_MSG_SIZE, !sender->user_port, &buf);
1346 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001347 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001348 return res;
1349}
1350
1351/**
1352 * tipc_send - send message sections on connection
1353 */
1354
1355int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1356{
1357 struct port *p_ptr;
1358 u32 destnode;
1359 int res;
1360
Per Liden4323add2006-01-18 00:38:21 +01001361 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001362 if (!p_ptr || !p_ptr->publ.connected)
1363 return -EINVAL;
1364
1365 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001366 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001367 destnode = port_peernode(p_ptr);
1368 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001369 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1370 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001371 else
Per Liden4323add2006-01-18 00:38:21 +01001372 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001373
1374 if (likely(res != -ELINKCONG)) {
1375 port_incr_out_seqno(p_ptr);
1376 p_ptr->publ.congested = 0;
1377 p_ptr->sent++;
1378 return res;
1379 }
1380 }
1381 if (port_unreliable(p_ptr)) {
1382 p_ptr->publ.congested = 0;
1383 /* Just calculate msg length and return */
1384 return msg_calc_data_size(msg_sect, num_sect);
1385 }
1386 return -ELINKCONG;
1387}
1388
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001389/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001390 * tipc_send_buf - send message buffer on connection
1391 */
1392
1393int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1394{
1395 struct port *p_ptr;
1396 struct tipc_msg *msg;
1397 u32 destnode;
1398 u32 hsz;
1399 u32 sz;
1400 u32 res;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001401
Per Liden4323add2006-01-18 00:38:21 +01001402 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001403 if (!p_ptr || !p_ptr->publ.connected)
1404 return -EINVAL;
1405
1406 msg = &p_ptr->publ.phdr;
1407 hsz = msg_hdr_sz(msg);
1408 sz = hsz + dsz;
1409 msg_set_size(msg, sz);
1410 if (skb_cow(buf, hsz))
1411 return -ENOMEM;
1412
1413 skb_push(buf, hsz);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001414 skb_copy_to_linear_data(buf, msg, hsz);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001415 destnode = msg_destnode(msg);
1416 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001417 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001418 if (likely(destnode != tipc_own_addr))
1419 res = tipc_send_buf_fast(buf, destnode);
1420 else {
Per Liden4323add2006-01-18 00:38:21 +01001421 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001422 res = sz;
1423 }
1424 if (likely(res != -ELINKCONG)) {
1425 port_incr_out_seqno(p_ptr);
1426 p_ptr->sent++;
1427 p_ptr->publ.congested = 0;
1428 return res;
1429 }
1430 }
1431 if (port_unreliable(p_ptr)) {
1432 p_ptr->publ.congested = 0;
1433 return dsz;
1434 }
1435 return -ELINKCONG;
1436}
1437
1438/**
1439 * tipc_forward2name - forward message sections to port name
1440 */
1441
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001442int tipc_forward2name(u32 ref,
1443 struct tipc_name const *name,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001444 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001445 u32 num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001446 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001447 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001448 unsigned int importance)
1449{
1450 struct port *p_ptr;
1451 struct tipc_msg *msg;
1452 u32 destnode = domain;
1453 u32 destport = 0;
1454 int res;
1455
Per Liden4323add2006-01-18 00:38:21 +01001456 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001457 if (!p_ptr || p_ptr->publ.connected)
1458 return -EINVAL;
1459
1460 msg = &p_ptr->publ.phdr;
1461 msg_set_type(msg, TIPC_NAMED_MSG);
1462 msg_set_orignode(msg, orig->node);
1463 msg_set_origport(msg, orig->ref);
1464 msg_set_hdr_sz(msg, LONG_H_SIZE);
1465 msg_set_nametype(msg, name->type);
1466 msg_set_nameinst(msg, name->instance);
1467 msg_set_lookup_scope(msg, addr_scope(domain));
1468 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1469 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001470 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001471 msg_set_destnode(msg, destnode);
1472 msg_set_destport(msg, destport);
1473
1474 if (likely(destport || destnode)) {
1475 p_ptr->sent++;
1476 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001477 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001478 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001479 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001480 if (likely(res != -ELINKCONG))
1481 return res;
1482 if (port_unreliable(p_ptr)) {
1483 /* Just calculate msg length and return */
1484 return msg_calc_data_size(msg_sect, num_sect);
1485 }
1486 return -ELINKCONG;
1487 }
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001488 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
Per Liden4323add2006-01-18 00:38:21 +01001489 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001490}
1491
1492/**
1493 * tipc_send2name - send message sections to port name
1494 */
1495
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001496int tipc_send2name(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001497 struct tipc_name const *name,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001498 unsigned int domain,
1499 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001500 struct iovec const *msg_sect)
1501{
1502 struct tipc_portid orig;
1503
1504 orig.ref = ref;
1505 orig.node = tipc_own_addr;
1506 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1507 TIPC_PORT_IMPORTANCE);
1508}
1509
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001510/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001511 * tipc_forward_buf2name - forward message buffer to port name
1512 */
1513
1514int tipc_forward_buf2name(u32 ref,
1515 struct tipc_name const *name,
1516 u32 domain,
1517 struct sk_buff *buf,
1518 unsigned int dsz,
1519 struct tipc_portid const *orig,
1520 unsigned int importance)
1521{
1522 struct port *p_ptr;
1523 struct tipc_msg *msg;
1524 u32 destnode = domain;
1525 u32 destport = 0;
1526 int res;
1527
Per Liden4323add2006-01-18 00:38:21 +01001528 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001529 if (!p_ptr || p_ptr->publ.connected)
1530 return -EINVAL;
1531
1532 msg = &p_ptr->publ.phdr;
1533 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1534 msg_set_importance(msg, importance);
1535 msg_set_type(msg, TIPC_NAMED_MSG);
1536 msg_set_orignode(msg, orig->node);
1537 msg_set_origport(msg, orig->ref);
1538 msg_set_nametype(msg, name->type);
1539 msg_set_nameinst(msg, name->instance);
1540 msg_set_lookup_scope(msg, addr_scope(domain));
1541 msg_set_hdr_sz(msg, LONG_H_SIZE);
1542 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001543 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001544 msg_set_destnode(msg, destnode);
1545 msg_set_destport(msg, destport);
1546 msg_dbg(msg, "forw2name ==> ");
1547 if (skb_cow(buf, LONG_H_SIZE))
1548 return -ENOMEM;
1549 skb_push(buf, LONG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001550 skb_copy_to_linear_data(buf, msg, LONG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001551 msg_dbg(buf_msg(buf),"PREP:");
1552 if (likely(destport || destnode)) {
1553 p_ptr->sent++;
1554 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001555 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001556 res = tipc_send_buf_fast(buf, destnode);
1557 if (likely(res != -ELINKCONG))
1558 return res;
1559 if (port_unreliable(p_ptr))
1560 return dsz;
1561 return -ELINKCONG;
1562 }
1563 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1564}
1565
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001566/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001567 * tipc_send_buf2name - send message buffer to port name
1568 */
1569
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001570int tipc_send_buf2name(u32 ref,
1571 struct tipc_name const *dest,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001572 u32 domain,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001573 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001574 unsigned int dsz)
1575{
1576 struct tipc_portid orig;
1577
1578 orig.ref = ref;
1579 orig.node = tipc_own_addr;
1580 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1581 TIPC_PORT_IMPORTANCE);
1582}
1583
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001584/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001585 * tipc_forward2port - forward message sections to port identity
1586 */
1587
1588int tipc_forward2port(u32 ref,
1589 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001590 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001591 struct iovec const *msg_sect,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001592 struct tipc_portid const *orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001593 unsigned int importance)
1594{
1595 struct port *p_ptr;
1596 struct tipc_msg *msg;
1597 int res;
1598
Per Liden4323add2006-01-18 00:38:21 +01001599 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001600 if (!p_ptr || p_ptr->publ.connected)
1601 return -EINVAL;
1602
1603 msg = &p_ptr->publ.phdr;
1604 msg_set_type(msg, TIPC_DIRECT_MSG);
1605 msg_set_orignode(msg, orig->node);
1606 msg_set_origport(msg, orig->ref);
1607 msg_set_destnode(msg, dest->node);
1608 msg_set_destport(msg, dest->ref);
1609 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1610 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1611 msg_set_importance(msg, importance);
1612 p_ptr->sent++;
1613 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001614 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1615 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001616 if (likely(res != -ELINKCONG))
1617 return res;
1618 if (port_unreliable(p_ptr)) {
1619 /* Just calculate msg length and return */
1620 return msg_calc_data_size(msg_sect, num_sect);
1621 }
1622 return -ELINKCONG;
1623}
1624
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001625/**
1626 * tipc_send2port - send message sections to port identity
Per Lidenb97bf3f2006-01-02 19:04:38 +01001627 */
1628
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001629int tipc_send2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001630 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001631 unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001632 struct iovec const *msg_sect)
1633{
1634 struct tipc_portid orig;
1635
1636 orig.ref = ref;
1637 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001638 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001639 TIPC_PORT_IMPORTANCE);
1640}
1641
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001642/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001643 * tipc_forward_buf2port - forward message buffer to port identity
1644 */
1645int tipc_forward_buf2port(u32 ref,
1646 struct tipc_portid const *dest,
1647 struct sk_buff *buf,
1648 unsigned int dsz,
1649 struct tipc_portid const *orig,
1650 unsigned int importance)
1651{
1652 struct port *p_ptr;
1653 struct tipc_msg *msg;
1654 int res;
1655
Per Liden4323add2006-01-18 00:38:21 +01001656 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001657 if (!p_ptr || p_ptr->publ.connected)
1658 return -EINVAL;
1659
1660 msg = &p_ptr->publ.phdr;
1661 msg_set_type(msg, TIPC_DIRECT_MSG);
1662 msg_set_orignode(msg, orig->node);
1663 msg_set_origport(msg, orig->ref);
1664 msg_set_destnode(msg, dest->node);
1665 msg_set_destport(msg, dest->ref);
1666 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1667 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1668 msg_set_importance(msg, importance);
1669 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1670 if (skb_cow(buf, DIR_MSG_H_SIZE))
1671 return -ENOMEM;
1672
1673 skb_push(buf, DIR_MSG_H_SIZE);
Arnaldo Carvalho de Melo27d7ff42007-03-31 11:55:19 -03001674 skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001675 msg_dbg(msg, "buf2port: ");
1676 p_ptr->sent++;
1677 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001678 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001679 res = tipc_send_buf_fast(buf, dest->node);
1680 if (likely(res != -ELINKCONG))
1681 return res;
1682 if (port_unreliable(p_ptr))
1683 return dsz;
1684 return -ELINKCONG;
1685}
1686
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001687/**
Per Lidenb97bf3f2006-01-02 19:04:38 +01001688 * tipc_send_buf2port - send message buffer to port identity
1689 */
1690
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001691int tipc_send_buf2port(u32 ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001692 struct tipc_portid const *dest,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001693 struct sk_buff *buf,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001694 unsigned int dsz)
1695{
1696 struct tipc_portid orig;
1697
1698 orig.ref = ref;
1699 orig.node = tipc_own_addr;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09001700 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001701 TIPC_PORT_IMPORTANCE);
1702}
1703