blob: 3251c8d8e53c3bcc401a13e53d6636d410b6b5d0 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/port.c: TIPC port code
3 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 1992-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2004-2005, 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"
44#include "port.h"
45#include "name_table.h"
46#include "user_reg.h"
47#include "msg.h"
48#include "bcast.h"
49
50/* Connection management: */
51#define PROBING_INTERVAL 3600000 /* [ms] => 1 h */
52#define CONFIRMED 0
53#define PROBING 1
54
55#define MAX_REJECT_SIZE 1024
56
Sam Ravnborg1fc54d82006-03-20 22:36:47 -080057static struct sk_buff *msg_queue_head = NULL;
58static struct sk_buff *msg_queue_tail = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +010059
Ingo Molnar34af9462006-06-27 02:53:55 -070060DEFINE_SPINLOCK(tipc_port_list_lock);
61static DEFINE_SPINLOCK(queue_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +010062
Per Liden4323add2006-01-18 00:38:21 +010063static LIST_HEAD(ports);
Per Lidenb97bf3f2006-01-02 19:04:38 +010064static void port_handle_node_down(unsigned long ref);
65static struct sk_buff* port_build_self_abort_msg(struct port *,u32 err);
66static struct sk_buff* port_build_peer_abort_msg(struct port *,u32 err);
67static void port_timeout(unsigned long ref);
68
69
Sam Ravnborg05790c62006-03-20 22:37:04 -080070static u32 port_peernode(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010071{
72 return msg_destnode(&p_ptr->publ.phdr);
73}
74
Sam Ravnborg05790c62006-03-20 22:37:04 -080075static u32 port_peerport(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010076{
77 return msg_destport(&p_ptr->publ.phdr);
78}
79
Sam Ravnborg05790c62006-03-20 22:37:04 -080080static u32 port_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010081{
82 return msg_transp_seqno(&p_ptr->publ.phdr);
83}
84
Sam Ravnborg05790c62006-03-20 22:37:04 -080085static void port_incr_out_seqno(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +010086{
87 struct tipc_msg *m = &p_ptr->publ.phdr;
88
89 if (likely(!msg_routed(m)))
90 return;
91 msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
92}
93
94/**
95 * tipc_multicast - send a multicast message to local and remote destinations
96 */
97
98int tipc_multicast(u32 ref, struct tipc_name_seq const *seq, u32 domain,
99 u32 num_sect, struct iovec const *msg_sect)
100{
101 struct tipc_msg *hdr;
102 struct sk_buff *buf;
103 struct sk_buff *ibuf = NULL;
104 struct port_list dports = {0, NULL, };
Per Liden4323add2006-01-18 00:38:21 +0100105 struct port *oport = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100106 int ext_targets;
107 int res;
108
109 if (unlikely(!oport))
110 return -EINVAL;
111
112 /* Create multicast message */
113
114 hdr = &oport->publ.phdr;
115 msg_set_type(hdr, TIPC_MCAST_MSG);
116 msg_set_nametype(hdr, seq->type);
117 msg_set_namelower(hdr, seq->lower);
118 msg_set_nameupper(hdr, seq->upper);
119 msg_set_hdr_sz(hdr, MCAST_H_SIZE);
120 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
121 !oport->user_port, &buf);
122 if (unlikely(!buf))
123 return res;
124
125 /* Figure out where to send multicast message */
126
Per Liden4323add2006-01-18 00:38:21 +0100127 ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
128 TIPC_NODE_SCOPE, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129
130 /* Send message to destinations (duplicate it only if necessary) */
131
132 if (ext_targets) {
133 if (dports.count != 0) {
134 ibuf = skb_copy(buf, GFP_ATOMIC);
135 if (ibuf == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100136 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100137 buf_discard(buf);
138 return -ENOMEM;
139 }
140 }
Per Liden4323add2006-01-18 00:38:21 +0100141 res = tipc_bclink_send_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 if ((res < 0) && (dports.count != 0)) {
143 buf_discard(ibuf);
144 }
145 } else {
146 ibuf = buf;
147 }
148
149 if (res >= 0) {
150 if (ibuf)
Per Liden4323add2006-01-18 00:38:21 +0100151 tipc_port_recv_mcast(ibuf, &dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100152 } else {
Per Liden4323add2006-01-18 00:38:21 +0100153 tipc_port_list_free(&dports);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154 }
155 return res;
156}
157
158/**
Per Liden4323add2006-01-18 00:38:21 +0100159 * tipc_port_recv_mcast - deliver multicast message to all destination ports
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 *
161 * If there is no port list, perform a lookup to create one
162 */
163
Per Liden4323add2006-01-18 00:38:21 +0100164void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165{
166 struct tipc_msg* msg;
167 struct port_list dports = {0, NULL, };
168 struct port_list *item = dp;
169 int cnt = 0;
170
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171 msg = buf_msg(buf);
172
173 /* Create destination port list, if one wasn't supplied */
174
175 if (dp == NULL) {
Per Liden4323add2006-01-18 00:38:21 +0100176 tipc_nametbl_mc_translate(msg_nametype(msg),
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 msg_namelower(msg),
178 msg_nameupper(msg),
179 TIPC_CLUSTER_SCOPE,
180 &dports);
181 item = dp = &dports;
182 }
183
184 /* Deliver a copy of message to each destination port */
185
186 if (dp->count != 0) {
187 if (dp->count == 1) {
188 msg_set_destport(msg, dp->ports[0]);
Per Liden4323add2006-01-18 00:38:21 +0100189 tipc_port_recv_msg(buf);
190 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 return;
192 }
193 for (; cnt < dp->count; cnt++) {
194 int index = cnt % PLSIZE;
195 struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);
196
197 if (b == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700198 warn("Unable to deliver multicast message(s)\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100199 msg_dbg(msg, "LOST:");
200 goto exit;
201 }
202 if ((index == 0) && (cnt != 0)) {
203 item = item->next;
204 }
205 msg_set_destport(buf_msg(b),item->ports[index]);
Per Liden4323add2006-01-18 00:38:21 +0100206 tipc_port_recv_msg(b);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 }
208 }
209exit:
210 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100211 tipc_port_list_free(dp);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100212}
213
214/**
215 * tipc_createport_raw - create a native TIPC port
216 *
217 * Returns local port reference
218 */
219
220u32 tipc_createport_raw(void *usr_handle,
221 u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
222 void (*wakeup)(struct tipc_port *),
223 const u32 importance)
224{
225 struct port *p_ptr;
226 struct tipc_msg *msg;
227 u32 ref;
228
229 p_ptr = kmalloc(sizeof(*p_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700230 if (!p_ptr) {
231 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100232 return 0;
233 }
234 memset(p_ptr, 0, sizeof(*p_ptr));
Per Liden4323add2006-01-18 00:38:21 +0100235 ref = tipc_ref_acquire(p_ptr, &p_ptr->publ.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 if (!ref) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700237 warn("Port creation failed, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 kfree(p_ptr);
239 return 0;
240 }
241
Per Liden4323add2006-01-18 00:38:21 +0100242 tipc_port_lock(ref);
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;
252 p_ptr->publ.usr_handle = usr_handle;
253 INIT_LIST_HEAD(&p_ptr->wait_list);
254 INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800255 p_ptr->congested_link = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 p_ptr->max_pkt = MAX_PKT_DEFAULT;
257 p_ptr->dispatcher = dispatcher;
258 p_ptr->wakeup = wakeup;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800259 p_ptr->user_port = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
Per Liden4323add2006-01-18 00:38:21 +0100261 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 INIT_LIST_HEAD(&p_ptr->publications);
263 INIT_LIST_HEAD(&p_ptr->port_list);
264 list_add_tail(&p_ptr->port_list, &ports);
Per Liden4323add2006-01-18 00:38:21 +0100265 spin_unlock_bh(&tipc_port_list_lock);
266 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100267 return ref;
268}
269
270int tipc_deleteport(u32 ref)
271{
272 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800273 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800275 tipc_withdraw(ref, 0, NULL);
Per Liden4323add2006-01-18 00:38:21 +0100276 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100277 if (!p_ptr)
278 return -EINVAL;
279
Per Liden4323add2006-01-18 00:38:21 +0100280 tipc_ref_discard(ref);
281 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100282
283 k_cancel_timer(&p_ptr->timer);
284 if (p_ptr->publ.connected) {
285 buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
Per Liden4323add2006-01-18 00:38:21 +0100286 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100287 }
288 if (p_ptr->user_port) {
Per Liden4323add2006-01-18 00:38:21 +0100289 tipc_reg_remove_port(p_ptr->user_port);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100290 kfree(p_ptr->user_port);
291 }
292
Per Liden4323add2006-01-18 00:38:21 +0100293 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100294 list_del(&p_ptr->port_list);
295 list_del(&p_ptr->wait_list);
Per Liden4323add2006-01-18 00:38:21 +0100296 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100297 k_term_timer(&p_ptr->timer);
298 kfree(p_ptr);
299 dbg("Deleted port %u\n", ref);
Per Liden4323add2006-01-18 00:38:21 +0100300 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100301 return TIPC_OK;
302}
303
304/**
305 * tipc_get_port() - return port associated with 'ref'
306 *
307 * Note: Port is not locked.
308 */
309
310struct tipc_port *tipc_get_port(const u32 ref)
311{
Per Liden4323add2006-01-18 00:38:21 +0100312 return (struct tipc_port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313}
314
315/**
316 * tipc_get_handle - return user handle associated to port 'ref'
317 */
318
319void *tipc_get_handle(const u32 ref)
320{
321 struct port *p_ptr;
322 void * handle;
323
Per Liden4323add2006-01-18 00:38:21 +0100324 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100325 if (!p_ptr)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800326 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100327 handle = p_ptr->publ.usr_handle;
Per Liden4323add2006-01-18 00:38:21 +0100328 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100329 return handle;
330}
331
Sam Ravnborg05790c62006-03-20 22:37:04 -0800332static int port_unreliable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100333{
334 return msg_src_droppable(&p_ptr->publ.phdr);
335}
336
337int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
338{
339 struct port *p_ptr;
340
Per Liden4323add2006-01-18 00:38:21 +0100341 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 if (!p_ptr)
343 return -EINVAL;
344 *isunreliable = port_unreliable(p_ptr);
345 spin_unlock_bh(p_ptr->publ.lock);
346 return TIPC_OK;
347}
348
349int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
350{
351 struct port *p_ptr;
352
Per Liden4323add2006-01-18 00:38:21 +0100353 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 if (!p_ptr)
355 return -EINVAL;
356 msg_set_src_droppable(&p_ptr->publ.phdr, (isunreliable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100357 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 return TIPC_OK;
359}
360
Sam Ravnborg05790c62006-03-20 22:37:04 -0800361static int port_unreturnable(struct port *p_ptr)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362{
363 return msg_dest_droppable(&p_ptr->publ.phdr);
364}
365
366int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
367{
368 struct port *p_ptr;
369
Per Liden4323add2006-01-18 00:38:21 +0100370 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 if (!p_ptr)
372 return -EINVAL;
373 *isunrejectable = port_unreturnable(p_ptr);
374 spin_unlock_bh(p_ptr->publ.lock);
375 return TIPC_OK;
376}
377
378int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
379{
380 struct port *p_ptr;
381
Per Liden4323add2006-01-18 00:38:21 +0100382 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 if (!p_ptr)
384 return -EINVAL;
385 msg_set_dest_droppable(&p_ptr->publ.phdr, (isunrejectable != 0));
Per Liden4323add2006-01-18 00:38:21 +0100386 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387 return TIPC_OK;
388}
389
390/*
391 * port_build_proto_msg(): build a port level protocol
392 * or a connection abortion message. Called with
393 * tipc_port lock on.
394 */
395static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
396 u32 origport, u32 orignode,
397 u32 usr, u32 type, u32 err,
398 u32 seqno, u32 ack)
399{
400 struct sk_buff *buf;
401 struct tipc_msg *msg;
402
403 buf = buf_acquire(LONG_H_SIZE);
404 if (buf) {
405 msg = buf_msg(buf);
406 msg_init(msg, usr, type, err, LONG_H_SIZE, destnode);
407 msg_set_destport(msg, destport);
408 msg_set_origport(msg, origport);
409 msg_set_destnode(msg, destnode);
410 msg_set_orignode(msg, orignode);
411 msg_set_transp_seqno(msg, seqno);
412 msg_set_msgcnt(msg, ack);
413 msg_dbg(msg, "PORT>SEND>:");
414 }
415 return buf;
416}
417
418int tipc_set_msg_option(struct tipc_port *tp_ptr, const char *opt, const u32 sz)
419{
420 msg_expand(&tp_ptr->phdr, msg_destnode(&tp_ptr->phdr));
421 msg_set_options(&tp_ptr->phdr, opt, sz);
422 return TIPC_OK;
423}
424
425int tipc_reject_msg(struct sk_buff *buf, u32 err)
426{
427 struct tipc_msg *msg = buf_msg(buf);
428 struct sk_buff *rbuf;
429 struct tipc_msg *rmsg;
430 int hdr_sz;
431 u32 imp = msg_importance(msg);
432 u32 data_sz = msg_data_sz(msg);
433
434 if (data_sz > MAX_REJECT_SIZE)
435 data_sz = MAX_REJECT_SIZE;
436 if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
437 imp++;
438 msg_dbg(msg, "port->rej: ");
439
440 /* discard rejected message if it shouldn't be returned to sender */
441 if (msg_errcode(msg) || msg_dest_droppable(msg)) {
442 buf_discard(buf);
443 return data_sz;
444 }
445
446 /* construct rejected message */
447 if (msg_mcast(msg))
448 hdr_sz = MCAST_H_SIZE;
449 else
450 hdr_sz = LONG_H_SIZE;
451 rbuf = buf_acquire(data_sz + hdr_sz);
452 if (rbuf == NULL) {
453 buf_discard(buf);
454 return data_sz;
455 }
456 rmsg = buf_msg(rbuf);
457 msg_init(rmsg, imp, msg_type(msg), err, hdr_sz, msg_orignode(msg));
458 msg_set_destport(rmsg, msg_origport(msg));
459 msg_set_prevnode(rmsg, tipc_own_addr);
460 msg_set_origport(rmsg, msg_destport(msg));
461 if (msg_short(msg))
462 msg_set_orignode(rmsg, tipc_own_addr);
463 else
464 msg_set_orignode(rmsg, msg_destnode(msg));
465 msg_set_size(rmsg, data_sz + hdr_sz);
466 msg_set_nametype(rmsg, msg_nametype(msg));
467 msg_set_nameinst(rmsg, msg_nameinst(msg));
468 memcpy(rbuf->data + hdr_sz, msg_data(msg), data_sz);
469
470 /* send self-abort message when rejecting on a connected port */
471 if (msg_connected(msg)) {
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800472 struct sk_buff *abuf = NULL;
Per Liden4323add2006-01-18 00:38:21 +0100473 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474
475 if (p_ptr) {
476 if (p_ptr->publ.connected)
477 abuf = port_build_self_abort_msg(p_ptr, err);
Per Liden4323add2006-01-18 00:38:21 +0100478 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 }
Per Liden4323add2006-01-18 00:38:21 +0100480 tipc_net_route_msg(abuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 }
482
483 /* send rejected message */
484 buf_discard(buf);
Per Liden4323add2006-01-18 00:38:21 +0100485 tipc_net_route_msg(rbuf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 return data_sz;
487}
488
Per Liden4323add2006-01-18 00:38:21 +0100489int tipc_port_reject_sections(struct port *p_ptr, struct tipc_msg *hdr,
490 struct iovec const *msg_sect, u32 num_sect,
491 int err)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492{
493 struct sk_buff *buf;
494 int res;
495
496 res = msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
497 !p_ptr->user_port, &buf);
498 if (!buf)
499 return res;
500
501 return tipc_reject_msg(buf, err);
502}
503
504static void port_timeout(unsigned long ref)
505{
Per Liden4323add2006-01-18 00:38:21 +0100506 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800507 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508
509 if (!p_ptr || !p_ptr->publ.connected)
510 return;
511
512 /* Last probe answered ? */
513 if (p_ptr->probing_state == PROBING) {
514 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
515 } else {
516 buf = port_build_proto_msg(port_peerport(p_ptr),
517 port_peernode(p_ptr),
518 p_ptr->publ.ref,
519 tipc_own_addr,
520 CONN_MANAGER,
521 CONN_PROBE,
522 TIPC_OK,
523 port_out_seqno(p_ptr),
524 0);
525 port_incr_out_seqno(p_ptr);
526 p_ptr->probing_state = PROBING;
527 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
528 }
Per Liden4323add2006-01-18 00:38:21 +0100529 tipc_port_unlock(p_ptr);
530 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531}
532
533
534static void port_handle_node_down(unsigned long ref)
535{
Per Liden4323add2006-01-18 00:38:21 +0100536 struct port *p_ptr = tipc_port_lock(ref);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800537 struct sk_buff* buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538
539 if (!p_ptr)
540 return;
541 buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
Per Liden4323add2006-01-18 00:38:21 +0100542 tipc_port_unlock(p_ptr);
543 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544}
545
546
547static struct sk_buff *port_build_self_abort_msg(struct port *p_ptr, u32 err)
548{
549 u32 imp = msg_importance(&p_ptr->publ.phdr);
550
551 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800552 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 if (imp < TIPC_CRITICAL_IMPORTANCE)
554 imp++;
555 return port_build_proto_msg(p_ptr->publ.ref,
556 tipc_own_addr,
557 port_peerport(p_ptr),
558 port_peernode(p_ptr),
559 imp,
560 TIPC_CONN_MSG,
561 err,
562 p_ptr->last_in_seqno + 1,
563 0);
564}
565
566
567static struct sk_buff *port_build_peer_abort_msg(struct port *p_ptr, u32 err)
568{
569 u32 imp = msg_importance(&p_ptr->publ.phdr);
570
571 if (!p_ptr->publ.connected)
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800572 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 if (imp < TIPC_CRITICAL_IMPORTANCE)
574 imp++;
575 return port_build_proto_msg(port_peerport(p_ptr),
576 port_peernode(p_ptr),
577 p_ptr->publ.ref,
578 tipc_own_addr,
579 imp,
580 TIPC_CONN_MSG,
581 err,
582 port_out_seqno(p_ptr),
583 0);
584}
585
Per Liden4323add2006-01-18 00:38:21 +0100586void tipc_port_recv_proto_msg(struct sk_buff *buf)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587{
588 struct tipc_msg *msg = buf_msg(buf);
Per Liden4323add2006-01-18 00:38:21 +0100589 struct port *p_ptr = tipc_port_lock(msg_destport(msg));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100590 u32 err = TIPC_OK;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800591 struct sk_buff *r_buf = NULL;
592 struct sk_buff *abort_buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100593
594 msg_dbg(msg, "PORT<RECV<:");
595
596 if (!p_ptr) {
597 err = TIPC_ERR_NO_PORT;
598 } else if (p_ptr->publ.connected) {
599 if (port_peernode(p_ptr) != msg_orignode(msg))
600 err = TIPC_ERR_NO_PORT;
601 if (port_peerport(p_ptr) != msg_origport(msg))
602 err = TIPC_ERR_NO_PORT;
603 if (!err && msg_routed(msg)) {
604 u32 seqno = msg_transp_seqno(msg);
605 u32 myno = ++p_ptr->last_in_seqno;
606 if (seqno != myno) {
607 err = TIPC_ERR_NO_PORT;
608 abort_buf = port_build_self_abort_msg(p_ptr, err);
609 }
610 }
611 if (msg_type(msg) == CONN_ACK) {
Per Liden4323add2006-01-18 00:38:21 +0100612 int wakeup = tipc_port_congested(p_ptr) &&
Per Lidenb97bf3f2006-01-02 19:04:38 +0100613 p_ptr->publ.congested &&
614 p_ptr->wakeup;
615 p_ptr->acked += msg_msgcnt(msg);
Per Liden4323add2006-01-18 00:38:21 +0100616 if (tipc_port_congested(p_ptr))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100617 goto exit;
618 p_ptr->publ.congested = 0;
619 if (!wakeup)
620 goto exit;
621 p_ptr->wakeup(&p_ptr->publ);
622 goto exit;
623 }
624 } else if (p_ptr->publ.published) {
625 err = TIPC_ERR_NO_PORT;
626 }
627 if (err) {
628 r_buf = port_build_proto_msg(msg_origport(msg),
629 msg_orignode(msg),
630 msg_destport(msg),
631 tipc_own_addr,
632 DATA_HIGH,
633 TIPC_CONN_MSG,
634 err,
635 0,
636 0);
637 goto exit;
638 }
639
640 /* All is fine */
641 if (msg_type(msg) == CONN_PROBE) {
642 r_buf = port_build_proto_msg(msg_origport(msg),
643 msg_orignode(msg),
644 msg_destport(msg),
645 tipc_own_addr,
646 CONN_MANAGER,
647 CONN_PROBE_REPLY,
648 TIPC_OK,
649 port_out_seqno(p_ptr),
650 0);
651 }
652 p_ptr->probing_state = CONFIRMED;
653 port_incr_out_seqno(p_ptr);
654exit:
655 if (p_ptr)
Per Liden4323add2006-01-18 00:38:21 +0100656 tipc_port_unlock(p_ptr);
657 tipc_net_route_msg(r_buf);
658 tipc_net_route_msg(abort_buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100659 buf_discard(buf);
660}
661
662static void port_print(struct port *p_ptr, struct print_buf *buf, int full_id)
663{
664 struct publication *publ;
665
666 if (full_id)
667 tipc_printf(buf, "<%u.%u.%u:%u>:",
668 tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
669 tipc_node(tipc_own_addr), p_ptr->publ.ref);
670 else
671 tipc_printf(buf, "%-10u:", p_ptr->publ.ref);
672
673 if (p_ptr->publ.connected) {
674 u32 dport = port_peerport(p_ptr);
675 u32 destnode = port_peernode(p_ptr);
676
677 tipc_printf(buf, " connected to <%u.%u.%u:%u>",
678 tipc_zone(destnode), tipc_cluster(destnode),
679 tipc_node(destnode), dport);
680 if (p_ptr->publ.conn_type != 0)
681 tipc_printf(buf, " via {%u,%u}",
682 p_ptr->publ.conn_type,
683 p_ptr->publ.conn_instance);
684 }
685 else if (p_ptr->publ.published) {
686 tipc_printf(buf, " bound to");
687 list_for_each_entry(publ, &p_ptr->publications, pport_list) {
688 if (publ->lower == publ->upper)
689 tipc_printf(buf, " {%u,%u}", publ->type,
690 publ->lower);
691 else
692 tipc_printf(buf, " {%u,%u,%u}", publ->type,
693 publ->lower, publ->upper);
694 }
695 }
696 tipc_printf(buf, "\n");
697}
698
699#define MAX_PORT_QUERY 32768
700
Per Liden4323add2006-01-18 00:38:21 +0100701struct sk_buff *tipc_port_get_ports(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100702{
703 struct sk_buff *buf;
704 struct tlv_desc *rep_tlv;
705 struct print_buf pb;
706 struct port *p_ptr;
707 int str_len;
708
Per Liden4323add2006-01-18 00:38:21 +0100709 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100710 if (!buf)
711 return NULL;
712 rep_tlv = (struct tlv_desc *)buf->data;
713
Per Liden4323add2006-01-18 00:38:21 +0100714 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
715 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100716 list_for_each_entry(p_ptr, &ports, port_list) {
717 spin_lock_bh(p_ptr->publ.lock);
718 port_print(p_ptr, &pb, 0);
719 spin_unlock_bh(p_ptr->publ.lock);
720 }
Per Liden4323add2006-01-18 00:38:21 +0100721 spin_unlock_bh(&tipc_port_list_lock);
722 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100723
724 skb_put(buf, TLV_SPACE(str_len));
725 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
726
727 return buf;
728}
729
730#if 0
731
732#define MAX_PORT_STATS 2000
733
734struct sk_buff *port_show_stats(const void *req_tlv_area, int req_tlv_space)
735{
736 u32 ref;
737 struct port *p_ptr;
738 struct sk_buff *buf;
739 struct tlv_desc *rep_tlv;
740 struct print_buf pb;
741 int str_len;
742
743 if (!TLV_CHECK(req_tlv_area, req_tlv_space, TIPC_TLV_PORT_REF))
744 return cfg_reply_error_string(TIPC_CFG_TLV_ERROR);
745
746 ref = *(u32 *)TLV_DATA(req_tlv_area);
747 ref = ntohl(ref);
748
Per Liden4323add2006-01-18 00:38:21 +0100749 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100750 if (!p_ptr)
751 return cfg_reply_error_string("port not found");
752
Per Liden4323add2006-01-18 00:38:21 +0100753 buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_STATS));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100754 if (!buf) {
Per Liden4323add2006-01-18 00:38:21 +0100755 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100756 return NULL;
757 }
758 rep_tlv = (struct tlv_desc *)buf->data;
759
Per Liden4323add2006-01-18 00:38:21 +0100760 tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_STATS);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100761 port_print(p_ptr, &pb, 1);
762 /* NEED TO FILL IN ADDITIONAL PORT STATISTICS HERE */
Per Liden4323add2006-01-18 00:38:21 +0100763 tipc_port_unlock(p_ptr);
764 str_len = tipc_printbuf_validate(&pb);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100765
766 skb_put(buf, TLV_SPACE(str_len));
767 TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);
768
769 return buf;
770}
771
772#endif
773
Per Liden4323add2006-01-18 00:38:21 +0100774void tipc_port_reinit(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100775{
776 struct port *p_ptr;
777 struct tipc_msg *msg;
778
Per Liden4323add2006-01-18 00:38:21 +0100779 spin_lock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100780 list_for_each_entry(p_ptr, &ports, port_list) {
781 msg = &p_ptr->publ.phdr;
782 if (msg_orignode(msg) == tipc_own_addr)
783 break;
784 msg_set_orignode(msg, tipc_own_addr);
785 }
Per Liden4323add2006-01-18 00:38:21 +0100786 spin_unlock_bh(&tipc_port_list_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100787}
788
789
790/*
791 * port_dispatcher_sigh(): Signal handler for messages destinated
792 * to the tipc_port interface.
793 */
794
795static void port_dispatcher_sigh(void *dummy)
796{
797 struct sk_buff *buf;
798
799 spin_lock_bh(&queue_lock);
800 buf = msg_queue_head;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800801 msg_queue_head = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100802 spin_unlock_bh(&queue_lock);
803
804 while (buf) {
805 struct port *p_ptr;
806 struct user_port *up_ptr;
807 struct tipc_portid orig;
808 struct tipc_name_seq dseq;
809 void *usr_handle;
810 int connected;
811 int published;
Allan Stephens96882432006-06-25 23:38:58 -0700812 u32 message_type;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100813
814 struct sk_buff *next = buf->next;
815 struct tipc_msg *msg = buf_msg(buf);
816 u32 dref = msg_destport(msg);
817
Allan Stephens96882432006-06-25 23:38:58 -0700818 message_type = msg_type(msg);
819 if (message_type > TIPC_DIRECT_MSG)
820 goto reject; /* Unsupported message type */
821
Per Liden4323add2006-01-18 00:38:21 +0100822 p_ptr = tipc_port_lock(dref);
Allan Stephens96882432006-06-25 23:38:58 -0700823 if (!p_ptr)
824 goto reject; /* Port deleted while msg in queue */
825
Per Lidenb97bf3f2006-01-02 19:04:38 +0100826 orig.ref = msg_origport(msg);
827 orig.node = msg_orignode(msg);
828 up_ptr = p_ptr->user_port;
829 usr_handle = up_ptr->usr_handle;
830 connected = p_ptr->publ.connected;
831 published = p_ptr->publ.published;
832
833 if (unlikely(msg_errcode(msg)))
834 goto err;
835
Allan Stephens96882432006-06-25 23:38:58 -0700836 switch (message_type) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100837
838 case TIPC_CONN_MSG:{
839 tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
840 u32 peer_port = port_peerport(p_ptr);
841 u32 peer_node = port_peernode(p_ptr);
842
843 spin_unlock_bh(p_ptr->publ.lock);
844 if (unlikely(!connected)) {
845 if (unlikely(published))
846 goto reject;
847 tipc_connect2port(dref,&orig);
848 }
849 if (unlikely(msg_origport(msg) != peer_port))
850 goto reject;
851 if (unlikely(msg_orignode(msg) != peer_node))
852 goto reject;
853 if (unlikely(!cb))
854 goto reject;
855 if (unlikely(++p_ptr->publ.conn_unacked >=
856 TIPC_FLOW_CONTROL_WIN))
857 tipc_acknowledge(dref,
858 p_ptr->publ.conn_unacked);
859 skb_pull(buf, msg_hdr_sz(msg));
860 cb(usr_handle, dref, &buf, msg_data(msg),
861 msg_data_sz(msg));
862 break;
863 }
864 case TIPC_DIRECT_MSG:{
865 tipc_msg_event cb = up_ptr->msg_cb;
866
867 spin_unlock_bh(p_ptr->publ.lock);
868 if (unlikely(connected))
869 goto reject;
870 if (unlikely(!cb))
871 goto reject;
872 skb_pull(buf, msg_hdr_sz(msg));
873 cb(usr_handle, dref, &buf, msg_data(msg),
874 msg_data_sz(msg), msg_importance(msg),
875 &orig);
876 break;
877 }
Allan Stephens96882432006-06-25 23:38:58 -0700878 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100879 case TIPC_NAMED_MSG:{
880 tipc_named_msg_event cb = up_ptr->named_msg_cb;
881
882 spin_unlock_bh(p_ptr->publ.lock);
883 if (unlikely(connected))
884 goto reject;
885 if (unlikely(!cb))
886 goto reject;
887 if (unlikely(!published))
888 goto reject;
889 dseq.type = msg_nametype(msg);
890 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700891 dseq.upper = (message_type == TIPC_NAMED_MSG)
892 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100893 skb_pull(buf, msg_hdr_sz(msg));
894 cb(usr_handle, dref, &buf, msg_data(msg),
895 msg_data_sz(msg), msg_importance(msg),
896 &orig, &dseq);
897 break;
898 }
899 }
900 if (buf)
901 buf_discard(buf);
902 buf = next;
903 continue;
904err:
Allan Stephens96882432006-06-25 23:38:58 -0700905 switch (message_type) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100906
907 case TIPC_CONN_MSG:{
908 tipc_conn_shutdown_event cb =
909 up_ptr->conn_err_cb;
910 u32 peer_port = port_peerport(p_ptr);
911 u32 peer_node = port_peernode(p_ptr);
912
913 spin_unlock_bh(p_ptr->publ.lock);
914 if (!connected || !cb)
915 break;
916 if (msg_origport(msg) != peer_port)
917 break;
918 if (msg_orignode(msg) != peer_node)
919 break;
920 tipc_disconnect(dref);
921 skb_pull(buf, msg_hdr_sz(msg));
922 cb(usr_handle, dref, &buf, msg_data(msg),
923 msg_data_sz(msg), msg_errcode(msg));
924 break;
925 }
926 case TIPC_DIRECT_MSG:{
927 tipc_msg_err_event cb = up_ptr->err_cb;
928
929 spin_unlock_bh(p_ptr->publ.lock);
930 if (connected || !cb)
931 break;
932 skb_pull(buf, msg_hdr_sz(msg));
933 cb(usr_handle, dref, &buf, msg_data(msg),
934 msg_data_sz(msg), msg_errcode(msg), &orig);
935 break;
936 }
Allan Stephens96882432006-06-25 23:38:58 -0700937 case TIPC_MCAST_MSG:
Per Lidenb97bf3f2006-01-02 19:04:38 +0100938 case TIPC_NAMED_MSG:{
939 tipc_named_msg_err_event cb =
940 up_ptr->named_err_cb;
941
942 spin_unlock_bh(p_ptr->publ.lock);
943 if (connected || !cb)
944 break;
945 dseq.type = msg_nametype(msg);
946 dseq.lower = msg_nameinst(msg);
Allan Stephens96882432006-06-25 23:38:58 -0700947 dseq.upper = (message_type == TIPC_NAMED_MSG)
948 ? dseq.lower : msg_nameupper(msg);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100949 skb_pull(buf, msg_hdr_sz(msg));
950 cb(usr_handle, dref, &buf, msg_data(msg),
951 msg_data_sz(msg), msg_errcode(msg), &dseq);
952 break;
953 }
954 }
955 if (buf)
956 buf_discard(buf);
957 buf = next;
958 continue;
959reject:
960 tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
961 buf = next;
962 }
963}
964
965/*
966 * port_dispatcher(): Dispatcher for messages destinated
967 * to the tipc_port interface. Called with port locked.
968 */
969
970static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
971{
972 buf->next = NULL;
973 spin_lock_bh(&queue_lock);
974 if (msg_queue_head) {
975 msg_queue_tail->next = buf;
976 msg_queue_tail = buf;
977 } else {
978 msg_queue_tail = msg_queue_head = buf;
Per Liden4323add2006-01-18 00:38:21 +0100979 tipc_k_signal((Handler)port_dispatcher_sigh, 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100980 }
981 spin_unlock_bh(&queue_lock);
982 return TIPC_OK;
983}
984
985/*
986 * Wake up port after congestion: Called with port locked,
987 *
988 */
989
990static void port_wakeup_sh(unsigned long ref)
991{
992 struct port *p_ptr;
993 struct user_port *up_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800994 tipc_continue_event cb = NULL;
995 void *uh = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100996
Per Liden4323add2006-01-18 00:38:21 +0100997 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100998 if (p_ptr) {
999 up_ptr = p_ptr->user_port;
1000 if (up_ptr) {
1001 cb = up_ptr->continue_event_cb;
1002 uh = up_ptr->usr_handle;
1003 }
Per Liden4323add2006-01-18 00:38:21 +01001004 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001005 }
1006 if (cb)
1007 cb(uh, ref);
1008}
1009
1010
1011static void port_wakeup(struct tipc_port *p_ptr)
1012{
Per Liden4323add2006-01-18 00:38:21 +01001013 tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001014}
1015
1016void tipc_acknowledge(u32 ref, u32 ack)
1017{
1018 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001019 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001020
Per Liden4323add2006-01-18 00:38:21 +01001021 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001022 if (!p_ptr)
1023 return;
1024 if (p_ptr->publ.connected) {
1025 p_ptr->publ.conn_unacked -= ack;
1026 buf = port_build_proto_msg(port_peerport(p_ptr),
1027 port_peernode(p_ptr),
1028 ref,
1029 tipc_own_addr,
1030 CONN_MANAGER,
1031 CONN_ACK,
1032 TIPC_OK,
1033 port_out_seqno(p_ptr),
1034 ack);
1035 }
Per Liden4323add2006-01-18 00:38:21 +01001036 tipc_port_unlock(p_ptr);
1037 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001038}
1039
1040/*
1041 * tipc_createport(): user level call. Will add port to
1042 * registry if non-zero user_ref.
1043 */
1044
1045int tipc_createport(u32 user_ref,
1046 void *usr_handle,
1047 unsigned int importance,
1048 tipc_msg_err_event error_cb,
1049 tipc_named_msg_err_event named_error_cb,
1050 tipc_conn_shutdown_event conn_error_cb,
1051 tipc_msg_event msg_cb,
1052 tipc_named_msg_event named_msg_cb,
1053 tipc_conn_msg_event conn_msg_cb,
1054 tipc_continue_event continue_event_cb,/* May be zero */
1055 u32 *portref)
1056{
1057 struct user_port *up_ptr;
1058 struct port *p_ptr;
1059 u32 ref;
1060
1061 up_ptr = (struct user_port *)kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -07001062 if (!up_ptr) {
Allan Stephensa75bf872006-06-25 23:50:01 -07001063 warn("Port creation failed, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +01001064 return -ENOMEM;
1065 }
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001066 ref = tipc_createport_raw(NULL, port_dispatcher, port_wakeup, importance);
Per Liden4323add2006-01-18 00:38:21 +01001067 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001068 if (!p_ptr) {
1069 kfree(up_ptr);
1070 return -ENOMEM;
1071 }
1072
1073 p_ptr->user_port = up_ptr;
1074 up_ptr->user_ref = user_ref;
1075 up_ptr->usr_handle = usr_handle;
1076 up_ptr->ref = p_ptr->publ.ref;
1077 up_ptr->err_cb = error_cb;
1078 up_ptr->named_err_cb = named_error_cb;
1079 up_ptr->conn_err_cb = conn_error_cb;
1080 up_ptr->msg_cb = msg_cb;
1081 up_ptr->named_msg_cb = named_msg_cb;
1082 up_ptr->conn_msg_cb = conn_msg_cb;
1083 up_ptr->continue_event_cb = continue_event_cb;
1084 INIT_LIST_HEAD(&up_ptr->uport_list);
Per Liden4323add2006-01-18 00:38:21 +01001085 tipc_reg_add_port(up_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001086 *portref = p_ptr->publ.ref;
1087 dbg(" tipc_createport: %x with ref %u\n", p_ptr, p_ptr->publ.ref);
Per Liden4323add2006-01-18 00:38:21 +01001088 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001089 return TIPC_OK;
1090}
1091
1092int tipc_ownidentity(u32 ref, struct tipc_portid *id)
1093{
1094 id->ref = ref;
1095 id->node = tipc_own_addr;
1096 return TIPC_OK;
1097}
1098
1099int tipc_portimportance(u32 ref, unsigned int *importance)
1100{
1101 struct port *p_ptr;
1102
Per Liden4323add2006-01-18 00:38:21 +01001103 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001104 if (!p_ptr)
1105 return -EINVAL;
1106 *importance = (unsigned int)msg_importance(&p_ptr->publ.phdr);
1107 spin_unlock_bh(p_ptr->publ.lock);
1108 return TIPC_OK;
1109}
1110
1111int tipc_set_portimportance(u32 ref, unsigned int imp)
1112{
1113 struct port *p_ptr;
1114
1115 if (imp > TIPC_CRITICAL_IMPORTANCE)
1116 return -EINVAL;
1117
Per Liden4323add2006-01-18 00:38:21 +01001118 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001119 if (!p_ptr)
1120 return -EINVAL;
1121 msg_set_importance(&p_ptr->publ.phdr, (u32)imp);
1122 spin_unlock_bh(p_ptr->publ.lock);
1123 return TIPC_OK;
1124}
1125
1126
1127int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1128{
1129 struct port *p_ptr;
1130 struct publication *publ;
1131 u32 key;
1132 int res = -EINVAL;
1133
Per Liden4323add2006-01-18 00:38:21 +01001134 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001135 dbg("tipc_publ %u, p_ptr = %x, conn = %x, scope = %x, "
1136 "lower = %u, upper = %u\n",
1137 ref, p_ptr, p_ptr->publ.connected, scope, seq->lower, seq->upper);
1138 if (!p_ptr)
1139 return -EINVAL;
1140 if (p_ptr->publ.connected)
1141 goto exit;
1142 if (seq->lower > seq->upper)
1143 goto exit;
1144 if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
1145 goto exit;
1146 key = ref + p_ptr->pub_count + 1;
1147 if (key == ref) {
1148 res = -EADDRINUSE;
1149 goto exit;
1150 }
Per Liden4323add2006-01-18 00:38:21 +01001151 publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1152 scope, p_ptr->publ.ref, key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001153 if (publ) {
1154 list_add(&publ->pport_list, &p_ptr->publications);
1155 p_ptr->pub_count++;
1156 p_ptr->publ.published = 1;
1157 res = TIPC_OK;
1158 }
1159exit:
Per Liden4323add2006-01-18 00:38:21 +01001160 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001161 return res;
1162}
1163
1164int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
1165{
1166 struct port *p_ptr;
1167 struct publication *publ;
1168 struct publication *tpubl;
1169 int res = -EINVAL;
1170
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;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001174 if (!seq) {
1175 list_for_each_entry_safe(publ, tpubl,
1176 &p_ptr->publications, pport_list) {
Per Liden4323add2006-01-18 00:38:21 +01001177 tipc_nametbl_withdraw(publ->type, publ->lower,
1178 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001179 }
1180 res = TIPC_OK;
1181 } else {
1182 list_for_each_entry_safe(publ, tpubl,
1183 &p_ptr->publications, pport_list) {
1184 if (publ->scope != scope)
1185 continue;
1186 if (publ->type != seq->type)
1187 continue;
1188 if (publ->lower != seq->lower)
1189 continue;
1190 if (publ->upper != seq->upper)
1191 break;
Per Liden4323add2006-01-18 00:38:21 +01001192 tipc_nametbl_withdraw(publ->type, publ->lower,
1193 publ->ref, publ->key);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001194 res = TIPC_OK;
1195 break;
1196 }
1197 }
1198 if (list_empty(&p_ptr->publications))
1199 p_ptr->publ.published = 0;
Per Liden4323add2006-01-18 00:38:21 +01001200 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001201 return res;
1202}
1203
1204int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
1205{
1206 struct port *p_ptr;
1207 struct tipc_msg *msg;
1208 int res = -EINVAL;
1209
Per Liden4323add2006-01-18 00:38:21 +01001210 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001211 if (!p_ptr)
1212 return -EINVAL;
1213 if (p_ptr->publ.published || p_ptr->publ.connected)
1214 goto exit;
1215 if (!peer->ref)
1216 goto exit;
1217
1218 msg = &p_ptr->publ.phdr;
1219 msg_set_destnode(msg, peer->node);
1220 msg_set_destport(msg, peer->ref);
1221 msg_set_orignode(msg, tipc_own_addr);
1222 msg_set_origport(msg, p_ptr->publ.ref);
1223 msg_set_transp_seqno(msg, 42);
1224 msg_set_type(msg, TIPC_CONN_MSG);
1225 if (!may_route(peer->node))
1226 msg_set_hdr_sz(msg, SHORT_H_SIZE);
1227 else
1228 msg_set_hdr_sz(msg, LONG_H_SIZE);
1229
1230 p_ptr->probing_interval = PROBING_INTERVAL;
1231 p_ptr->probing_state = CONFIRMED;
1232 p_ptr->publ.connected = 1;
1233 k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
1234
Per Liden4323add2006-01-18 00:38:21 +01001235 tipc_nodesub_subscribe(&p_ptr->subscription,peer->node,
David S. Miller880b0052006-01-12 13:22:32 -08001236 (void *)(unsigned long)ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001237 (net_ev_handler)port_handle_node_down);
1238 res = TIPC_OK;
1239exit:
Per Liden4323add2006-01-18 00:38:21 +01001240 tipc_port_unlock(p_ptr);
1241 p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001242 return res;
1243}
1244
1245/*
1246 * tipc_disconnect(): Disconnect port form peer.
1247 * This is a node local operation.
1248 */
1249
1250int tipc_disconnect(u32 ref)
1251{
1252 struct port *p_ptr;
1253 int res = -ENOTCONN;
1254
Per Liden4323add2006-01-18 00:38:21 +01001255 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001256 if (!p_ptr)
1257 return -EINVAL;
1258 if (p_ptr->publ.connected) {
1259 p_ptr->publ.connected = 0;
1260 /* let timer expire on it's own to avoid deadlock! */
Per Liden4323add2006-01-18 00:38:21 +01001261 tipc_nodesub_unsubscribe(&p_ptr->subscription);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001262 res = TIPC_OK;
1263 }
Per Liden4323add2006-01-18 00:38:21 +01001264 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001265 return res;
1266}
1267
1268/*
1269 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
1270 */
1271int tipc_shutdown(u32 ref)
1272{
1273 struct port *p_ptr;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -08001274 struct sk_buff *buf = NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +01001275
Per Liden4323add2006-01-18 00:38:21 +01001276 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001277 if (!p_ptr)
1278 return -EINVAL;
1279
1280 if (p_ptr->publ.connected) {
1281 u32 imp = msg_importance(&p_ptr->publ.phdr);
1282 if (imp < TIPC_CRITICAL_IMPORTANCE)
1283 imp++;
1284 buf = port_build_proto_msg(port_peerport(p_ptr),
1285 port_peernode(p_ptr),
1286 ref,
1287 tipc_own_addr,
1288 imp,
1289 TIPC_CONN_MSG,
1290 TIPC_CONN_SHUTDOWN,
1291 port_out_seqno(p_ptr),
1292 0);
1293 }
Per Liden4323add2006-01-18 00:38:21 +01001294 tipc_port_unlock(p_ptr);
1295 tipc_net_route_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001296 return tipc_disconnect(ref);
1297}
1298
1299int tipc_isconnected(u32 ref, int *isconnected)
1300{
1301 struct port *p_ptr;
1302
Per Liden4323add2006-01-18 00:38:21 +01001303 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001304 if (!p_ptr)
1305 return -EINVAL;
1306 *isconnected = p_ptr->publ.connected;
Per Liden4323add2006-01-18 00:38:21 +01001307 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001308 return TIPC_OK;
1309}
1310
1311int tipc_peer(u32 ref, struct tipc_portid *peer)
1312{
1313 struct port *p_ptr;
1314 int res;
1315
Per Liden4323add2006-01-18 00:38:21 +01001316 p_ptr = tipc_port_lock(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001317 if (!p_ptr)
1318 return -EINVAL;
1319 if (p_ptr->publ.connected) {
1320 peer->ref = port_peerport(p_ptr);
1321 peer->node = port_peernode(p_ptr);
1322 res = TIPC_OK;
1323 } else
1324 res = -ENOTCONN;
Per Liden4323add2006-01-18 00:38:21 +01001325 tipc_port_unlock(p_ptr);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001326 return res;
1327}
1328
1329int tipc_ref_valid(u32 ref)
1330{
1331 /* Works irrespective of type */
Per Liden4323add2006-01-18 00:38:21 +01001332 return !!tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001333}
1334
1335
1336/*
Per Liden4323add2006-01-18 00:38:21 +01001337 * tipc_port_recv_sections(): Concatenate and deliver sectioned
Per Lidenb97bf3f2006-01-02 19:04:38 +01001338 * message for this node.
1339 */
1340
Per Liden4323add2006-01-18 00:38:21 +01001341int tipc_port_recv_sections(struct port *sender, unsigned int num_sect,
Per Lidenb97bf3f2006-01-02 19:04:38 +01001342 struct iovec const *msg_sect)
1343{
1344 struct sk_buff *buf;
1345 int res;
1346
1347 res = msg_build(&sender->publ.phdr, msg_sect, num_sect,
1348 MAX_MSG_SIZE, !sender->user_port, &buf);
1349 if (likely(buf))
Per Liden4323add2006-01-18 00:38:21 +01001350 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001351 return res;
1352}
1353
1354/**
1355 * tipc_send - send message sections on connection
1356 */
1357
1358int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
1359{
1360 struct port *p_ptr;
1361 u32 destnode;
1362 int res;
1363
Per Liden4323add2006-01-18 00:38:21 +01001364 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001365 if (!p_ptr || !p_ptr->publ.connected)
1366 return -EINVAL;
1367
1368 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001369 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001370 destnode = port_peernode(p_ptr);
1371 if (likely(destnode != tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001372 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1373 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001374 else
Per Liden4323add2006-01-18 00:38:21 +01001375 res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001376
1377 if (likely(res != -ELINKCONG)) {
1378 port_incr_out_seqno(p_ptr);
1379 p_ptr->publ.congested = 0;
1380 p_ptr->sent++;
1381 return res;
1382 }
1383 }
1384 if (port_unreliable(p_ptr)) {
1385 p_ptr->publ.congested = 0;
1386 /* Just calculate msg length and return */
1387 return msg_calc_data_size(msg_sect, num_sect);
1388 }
1389 return -ELINKCONG;
1390}
1391
1392/**
1393 * tipc_send_buf - send message buffer on connection
1394 */
1395
1396int tipc_send_buf(u32 ref, struct sk_buff *buf, unsigned int dsz)
1397{
1398 struct port *p_ptr;
1399 struct tipc_msg *msg;
1400 u32 destnode;
1401 u32 hsz;
1402 u32 sz;
1403 u32 res;
1404
Per Liden4323add2006-01-18 00:38:21 +01001405 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001406 if (!p_ptr || !p_ptr->publ.connected)
1407 return -EINVAL;
1408
1409 msg = &p_ptr->publ.phdr;
1410 hsz = msg_hdr_sz(msg);
1411 sz = hsz + dsz;
1412 msg_set_size(msg, sz);
1413 if (skb_cow(buf, hsz))
1414 return -ENOMEM;
1415
1416 skb_push(buf, hsz);
1417 memcpy(buf->data, (unchar *)msg, hsz);
1418 destnode = msg_destnode(msg);
1419 p_ptr->publ.congested = 1;
Per Liden4323add2006-01-18 00:38:21 +01001420 if (!tipc_port_congested(p_ptr)) {
Per Lidenb97bf3f2006-01-02 19:04:38 +01001421 if (likely(destnode != tipc_own_addr))
1422 res = tipc_send_buf_fast(buf, destnode);
1423 else {
Per Liden4323add2006-01-18 00:38:21 +01001424 tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001425 res = sz;
1426 }
1427 if (likely(res != -ELINKCONG)) {
1428 port_incr_out_seqno(p_ptr);
1429 p_ptr->sent++;
1430 p_ptr->publ.congested = 0;
1431 return res;
1432 }
1433 }
1434 if (port_unreliable(p_ptr)) {
1435 p_ptr->publ.congested = 0;
1436 return dsz;
1437 }
1438 return -ELINKCONG;
1439}
1440
1441/**
1442 * tipc_forward2name - forward message sections to port name
1443 */
1444
1445int tipc_forward2name(u32 ref,
1446 struct tipc_name const *name,
1447 u32 domain,
1448 u32 num_sect,
1449 struct iovec const *msg_sect,
1450 struct tipc_portid const *orig,
1451 unsigned int importance)
1452{
1453 struct port *p_ptr;
1454 struct tipc_msg *msg;
1455 u32 destnode = domain;
1456 u32 destport = 0;
1457 int res;
1458
Per Liden4323add2006-01-18 00:38:21 +01001459 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001460 if (!p_ptr || p_ptr->publ.connected)
1461 return -EINVAL;
1462
1463 msg = &p_ptr->publ.phdr;
1464 msg_set_type(msg, TIPC_NAMED_MSG);
1465 msg_set_orignode(msg, orig->node);
1466 msg_set_origport(msg, orig->ref);
1467 msg_set_hdr_sz(msg, LONG_H_SIZE);
1468 msg_set_nametype(msg, name->type);
1469 msg_set_nameinst(msg, name->instance);
1470 msg_set_lookup_scope(msg, addr_scope(domain));
1471 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1472 msg_set_importance(msg,importance);
Per Liden4323add2006-01-18 00:38:21 +01001473 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001474 msg_set_destnode(msg, destnode);
1475 msg_set_destport(msg, destport);
1476
1477 if (likely(destport || destnode)) {
1478 p_ptr->sent++;
1479 if (likely(destnode == tipc_own_addr))
Per Liden4323add2006-01-18 00:38:21 +01001480 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1481 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
1482 destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001483 if (likely(res != -ELINKCONG))
1484 return res;
1485 if (port_unreliable(p_ptr)) {
1486 /* Just calculate msg length and return */
1487 return msg_calc_data_size(msg_sect, num_sect);
1488 }
1489 return -ELINKCONG;
1490 }
Per Liden4323add2006-01-18 00:38:21 +01001491 return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1492 TIPC_ERR_NO_NAME);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001493}
1494
1495/**
1496 * tipc_send2name - send message sections to port name
1497 */
1498
1499int tipc_send2name(u32 ref,
1500 struct tipc_name const *name,
1501 unsigned int domain,
1502 unsigned int num_sect,
1503 struct iovec const *msg_sect)
1504{
1505 struct tipc_portid orig;
1506
1507 orig.ref = ref;
1508 orig.node = tipc_own_addr;
1509 return tipc_forward2name(ref, name, domain, num_sect, msg_sect, &orig,
1510 TIPC_PORT_IMPORTANCE);
1511}
1512
1513/**
1514 * tipc_forward_buf2name - forward message buffer to port name
1515 */
1516
1517int tipc_forward_buf2name(u32 ref,
1518 struct tipc_name const *name,
1519 u32 domain,
1520 struct sk_buff *buf,
1521 unsigned int dsz,
1522 struct tipc_portid const *orig,
1523 unsigned int importance)
1524{
1525 struct port *p_ptr;
1526 struct tipc_msg *msg;
1527 u32 destnode = domain;
1528 u32 destport = 0;
1529 int res;
1530
Per Liden4323add2006-01-18 00:38:21 +01001531 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001532 if (!p_ptr || p_ptr->publ.connected)
1533 return -EINVAL;
1534
1535 msg = &p_ptr->publ.phdr;
1536 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1537 msg_set_importance(msg, importance);
1538 msg_set_type(msg, TIPC_NAMED_MSG);
1539 msg_set_orignode(msg, orig->node);
1540 msg_set_origport(msg, orig->ref);
1541 msg_set_nametype(msg, name->type);
1542 msg_set_nameinst(msg, name->instance);
1543 msg_set_lookup_scope(msg, addr_scope(domain));
1544 msg_set_hdr_sz(msg, LONG_H_SIZE);
1545 msg_set_size(msg, LONG_H_SIZE + dsz);
Per Liden4323add2006-01-18 00:38:21 +01001546 destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001547 msg_set_destnode(msg, destnode);
1548 msg_set_destport(msg, destport);
1549 msg_dbg(msg, "forw2name ==> ");
1550 if (skb_cow(buf, LONG_H_SIZE))
1551 return -ENOMEM;
1552 skb_push(buf, LONG_H_SIZE);
1553 memcpy(buf->data, (unchar *)msg, LONG_H_SIZE);
1554 msg_dbg(buf_msg(buf),"PREP:");
1555 if (likely(destport || destnode)) {
1556 p_ptr->sent++;
1557 if (destnode == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001558 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001559 res = tipc_send_buf_fast(buf, destnode);
1560 if (likely(res != -ELINKCONG))
1561 return res;
1562 if (port_unreliable(p_ptr))
1563 return dsz;
1564 return -ELINKCONG;
1565 }
1566 return tipc_reject_msg(buf, TIPC_ERR_NO_NAME);
1567}
1568
1569/**
1570 * tipc_send_buf2name - send message buffer to port name
1571 */
1572
1573int tipc_send_buf2name(u32 ref,
1574 struct tipc_name const *dest,
1575 u32 domain,
1576 struct sk_buff *buf,
1577 unsigned int dsz)
1578{
1579 struct tipc_portid orig;
1580
1581 orig.ref = ref;
1582 orig.node = tipc_own_addr;
1583 return tipc_forward_buf2name(ref, dest, domain, buf, dsz, &orig,
1584 TIPC_PORT_IMPORTANCE);
1585}
1586
1587/**
1588 * tipc_forward2port - forward message sections to port identity
1589 */
1590
1591int tipc_forward2port(u32 ref,
1592 struct tipc_portid const *dest,
1593 unsigned int num_sect,
1594 struct iovec const *msg_sect,
1595 struct tipc_portid const *orig,
1596 unsigned int importance)
1597{
1598 struct port *p_ptr;
1599 struct tipc_msg *msg;
1600 int res;
1601
Per Liden4323add2006-01-18 00:38:21 +01001602 p_ptr = tipc_port_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001603 if (!p_ptr || p_ptr->publ.connected)
1604 return -EINVAL;
1605
1606 msg = &p_ptr->publ.phdr;
1607 msg_set_type(msg, TIPC_DIRECT_MSG);
1608 msg_set_orignode(msg, orig->node);
1609 msg_set_origport(msg, orig->ref);
1610 msg_set_destnode(msg, dest->node);
1611 msg_set_destport(msg, dest->ref);
1612 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1613 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1614 msg_set_importance(msg, importance);
1615 p_ptr->sent++;
1616 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001617 return tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
1618 res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect, dest->node);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001619 if (likely(res != -ELINKCONG))
1620 return res;
1621 if (port_unreliable(p_ptr)) {
1622 /* Just calculate msg length and return */
1623 return msg_calc_data_size(msg_sect, num_sect);
1624 }
1625 return -ELINKCONG;
1626}
1627
1628/**
1629 * tipc_send2port - send message sections to port identity
1630 */
1631
1632int tipc_send2port(u32 ref,
1633 struct tipc_portid const *dest,
1634 unsigned int num_sect,
1635 struct iovec const *msg_sect)
1636{
1637 struct tipc_portid orig;
1638
1639 orig.ref = ref;
1640 orig.node = tipc_own_addr;
1641 return tipc_forward2port(ref, dest, num_sect, msg_sect, &orig,
1642 TIPC_PORT_IMPORTANCE);
1643}
1644
1645/**
1646 * tipc_forward_buf2port - forward message buffer to port identity
1647 */
1648int tipc_forward_buf2port(u32 ref,
1649 struct tipc_portid const *dest,
1650 struct sk_buff *buf,
1651 unsigned int dsz,
1652 struct tipc_portid const *orig,
1653 unsigned int importance)
1654{
1655 struct port *p_ptr;
1656 struct tipc_msg *msg;
1657 int res;
1658
Per Liden4323add2006-01-18 00:38:21 +01001659 p_ptr = (struct port *)tipc_ref_deref(ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001660 if (!p_ptr || p_ptr->publ.connected)
1661 return -EINVAL;
1662
1663 msg = &p_ptr->publ.phdr;
1664 msg_set_type(msg, TIPC_DIRECT_MSG);
1665 msg_set_orignode(msg, orig->node);
1666 msg_set_origport(msg, orig->ref);
1667 msg_set_destnode(msg, dest->node);
1668 msg_set_destport(msg, dest->ref);
1669 msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1670 if (importance <= TIPC_CRITICAL_IMPORTANCE)
1671 msg_set_importance(msg, importance);
1672 msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
1673 if (skb_cow(buf, DIR_MSG_H_SIZE))
1674 return -ENOMEM;
1675
1676 skb_push(buf, DIR_MSG_H_SIZE);
1677 memcpy(buf->data, (unchar *)msg, DIR_MSG_H_SIZE);
1678 msg_dbg(msg, "buf2port: ");
1679 p_ptr->sent++;
1680 if (dest->node == tipc_own_addr)
Per Liden4323add2006-01-18 00:38:21 +01001681 return tipc_port_recv_msg(buf);
Per Lidenb97bf3f2006-01-02 19:04:38 +01001682 res = tipc_send_buf_fast(buf, dest->node);
1683 if (likely(res != -ELINKCONG))
1684 return res;
1685 if (port_unreliable(p_ptr))
1686 return dsz;
1687 return -ELINKCONG;
1688}
1689
1690/**
1691 * tipc_send_buf2port - send message buffer to port identity
1692 */
1693
1694int tipc_send_buf2port(u32 ref,
1695 struct tipc_portid const *dest,
1696 struct sk_buff *buf,
1697 unsigned int dsz)
1698{
1699 struct tipc_portid orig;
1700
1701 orig.ref = ref;
1702 orig.node = tipc_own_addr;
1703 return tipc_forward_buf2port(ref, dest, buf, dsz, &orig,
1704 TIPC_PORT_IMPORTANCE);
1705}
1706