port.c 32.6 KB
Newer Older
P
Per Liden 已提交
1 2
/*
 * net/tipc/port.c: TIPC port code
3
 *
4
 * Copyright (c) 1992-2007, Ericsson AB
5
 * Copyright (c) 2004-2008, 2010-2011, Wind River Systems
P
Per Liden 已提交
6 7
 * All rights reserved.
 *
P
Per Liden 已提交
8
 * Redistribution and use in source and binary forms, with or without
P
Per Liden 已提交
9 10
 * modification, are permitted provided that the following conditions are met:
 *
P
Per Liden 已提交
11 12 13 14 15 16 17 18
 * 1. Redistributions of source code must retain the above copyright
 *    notice, this list of conditions and the following disclaimer.
 * 2. Redistributions in binary form must reproduce the above copyright
 *    notice, this list of conditions and the following disclaimer in the
 *    documentation and/or other materials provided with the distribution.
 * 3. Neither the names of the copyright holders nor the names of its
 *    contributors may be used to endorse or promote products derived from
 *    this software without specific prior written permission.
P
Per Liden 已提交
19
 *
P
Per Liden 已提交
20 21 22 23 24 25 26 27 28 29 30 31 32 33
 * Alternatively, this software may be distributed under the terms of the
 * GNU General Public License ("GPL") version 2 as published by the Free
 * Software Foundation.
 *
 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
P
Per Liden 已提交
34 35 36 37 38 39 40 41 42 43 44 45 46 47 48
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "config.h"
#include "port.h"
#include "name_table.h"

/* Connection management: */
#define PROBING_INTERVAL 3600000	/* [ms] => 1 h */
#define CONFIRMED 0
#define PROBING 1

#define MAX_REJECT_SIZE 1024

49 50
static struct sk_buff *msg_queue_head;
static struct sk_buff *msg_queue_tail;
P
Per Liden 已提交
51

I
Ingo Molnar 已提交
52 53
DEFINE_SPINLOCK(tipc_port_list_lock);
static DEFINE_SPINLOCK(queue_lock);
P
Per Liden 已提交
54

55
static LIST_HEAD(ports);
P
Per Liden 已提交
56
static void port_handle_node_down(unsigned long ref);
57 58
static struct sk_buff *port_build_self_abort_msg(struct tipc_port *, u32 err);
static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *, u32 err);
P
Per Liden 已提交
59 60 61
static void port_timeout(unsigned long ref);


62
static u32 port_peernode(struct tipc_port *p_ptr)
P
Per Liden 已提交
63
{
64
	return msg_destnode(&p_ptr->phdr);
P
Per Liden 已提交
65 66
}

67
static u32 port_peerport(struct tipc_port *p_ptr)
P
Per Liden 已提交
68
{
69
	return msg_destport(&p_ptr->phdr);
P
Per Liden 已提交
70 71
}

72
static u32 port_out_seqno(struct tipc_port *p_ptr)
P
Per Liden 已提交
73
{
74
	return msg_transp_seqno(&p_ptr->phdr);
P
Per Liden 已提交
75 76
}

77
static void port_incr_out_seqno(struct tipc_port *p_ptr)
P
Per Liden 已提交
78
{
79
	struct tipc_msg *m = &p_ptr->phdr;
P
Per Liden 已提交
80 81 82 83 84 85 86 87 88 89

	if (likely(!msg_routed(m)))
		return;
	msg_set_transp_seqno(m, (msg_transp_seqno(m) + 1));
}

/**
 * tipc_multicast - send a multicast message to local and remote destinations
 */

90
int tipc_multicast(u32 ref, struct tipc_name_seq const *seq,
P
Per Liden 已提交
91 92 93 94 95 96
		   u32 num_sect, struct iovec const *msg_sect)
{
	struct tipc_msg *hdr;
	struct sk_buff *buf;
	struct sk_buff *ibuf = NULL;
	struct port_list dports = {0, NULL, };
97
	struct tipc_port *oport = tipc_port_deref(ref);
P
Per Liden 已提交
98 99 100 101 102 103 104 105
	int ext_targets;
	int res;

	if (unlikely(!oport))
		return -EINVAL;

	/* Create multicast message */

106
	hdr = &oport->phdr;
P
Per Liden 已提交
107 108 109 110 111
	msg_set_type(hdr, TIPC_MCAST_MSG);
	msg_set_nametype(hdr, seq->type);
	msg_set_namelower(hdr, seq->lower);
	msg_set_nameupper(hdr, seq->upper);
	msg_set_hdr_sz(hdr, MCAST_H_SIZE);
112
	res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
P
Per Liden 已提交
113 114 115 116 117 118
			!oport->user_port, &buf);
	if (unlikely(!buf))
		return res;

	/* Figure out where to send multicast message */

119 120
	ext_targets = tipc_nametbl_mc_translate(seq->type, seq->lower, seq->upper,
						TIPC_NODE_SCOPE, &dports);
121 122

	/* Send message to destinations (duplicate it only if necessary) */
P
Per Liden 已提交
123 124 125 126 127

	if (ext_targets) {
		if (dports.count != 0) {
			ibuf = skb_copy(buf, GFP_ATOMIC);
			if (ibuf == NULL) {
128
				tipc_port_list_free(&dports);
P
Per Liden 已提交
129 130 131 132
				buf_discard(buf);
				return -ENOMEM;
			}
		}
133
		res = tipc_bclink_send_msg(buf);
134
		if ((res < 0) && (dports.count != 0))
P
Per Liden 已提交
135 136 137 138 139 140 141
			buf_discard(ibuf);
	} else {
		ibuf = buf;
	}

	if (res >= 0) {
		if (ibuf)
142
			tipc_port_recv_mcast(ibuf, &dports);
P
Per Liden 已提交
143
	} else {
144
		tipc_port_list_free(&dports);
P
Per Liden 已提交
145 146 147 148 149
	}
	return res;
}

/**
150
 * tipc_port_recv_mcast - deliver multicast message to all destination ports
151
 *
P
Per Liden 已提交
152 153 154
 * If there is no port list, perform a lookup to create one
 */

155
void tipc_port_recv_mcast(struct sk_buff *buf, struct port_list *dp)
P
Per Liden 已提交
156
{
157
	struct tipc_msg *msg;
P
Per Liden 已提交
158 159 160 161 162 163 164 165 166
	struct port_list dports = {0, NULL, };
	struct port_list *item = dp;
	int cnt = 0;

	msg = buf_msg(buf);

	/* Create destination port list, if one wasn't supplied */

	if (dp == NULL) {
167
		tipc_nametbl_mc_translate(msg_nametype(msg),
P
Per Liden 已提交
168 169 170 171 172 173 174 175 176 177 178 179
				     msg_namelower(msg),
				     msg_nameupper(msg),
				     TIPC_CLUSTER_SCOPE,
				     &dports);
		item = dp = &dports;
	}

	/* Deliver a copy of message to each destination port */

	if (dp->count != 0) {
		if (dp->count == 1) {
			msg_set_destport(msg, dp->ports[0]);
180 181
			tipc_port_recv_msg(buf);
			tipc_port_list_free(dp);
P
Per Liden 已提交
182 183 184 185 186 187 188
			return;
		}
		for (; cnt < dp->count; cnt++) {
			int index = cnt % PLSIZE;
			struct sk_buff *b = skb_clone(buf, GFP_ATOMIC);

			if (b == NULL) {
189
				warn("Unable to deliver multicast message(s)\n");
P
Per Liden 已提交
190 191
				goto exit;
			}
192
			if ((index == 0) && (cnt != 0))
P
Per Liden 已提交
193
				item = item->next;
194
			msg_set_destport(buf_msg(b), item->ports[index]);
195
			tipc_port_recv_msg(b);
P
Per Liden 已提交
196 197 198 199
		}
	}
exit:
	buf_discard(buf);
200
	tipc_port_list_free(dp);
P
Per Liden 已提交
201 202 203
}

/**
204
 * tipc_createport_raw - create a generic TIPC port
205
 *
206
 * Returns pointer to (locked) TIPC port, or NULL if unable to create it
P
Per Liden 已提交
207 208
 */

209
struct tipc_port *tipc_createport_raw(void *usr_handle,
P
Per Liden 已提交
210 211
			u32 (*dispatcher)(struct tipc_port *, struct sk_buff *),
			void (*wakeup)(struct tipc_port *),
212
			const u32 importance)
P
Per Liden 已提交
213
{
214
	struct tipc_port *p_ptr;
P
Per Liden 已提交
215 216 217
	struct tipc_msg *msg;
	u32 ref;

218
	p_ptr = kzalloc(sizeof(*p_ptr), GFP_ATOMIC);
219 220
	if (!p_ptr) {
		warn("Port creation failed, no memory\n");
221
		return NULL;
P
Per Liden 已提交
222
	}
223
	ref = tipc_ref_acquire(p_ptr, &p_ptr->lock);
P
Per Liden 已提交
224
	if (!ref) {
225
		warn("Port creation failed, reference table exhausted\n");
P
Per Liden 已提交
226
		kfree(p_ptr);
227
		return NULL;
P
Per Liden 已提交
228 229
	}

230 231 232 233
	p_ptr->usr_handle = usr_handle;
	p_ptr->max_pkt = MAX_PKT_DEFAULT;
	p_ptr->ref = ref;
	msg = &p_ptr->phdr;
234
	tipc_msg_init(msg, importance, TIPC_NAMED_MSG, LONG_H_SIZE, 0);
P
Per Liden 已提交
235 236 237 238 239 240
	msg_set_origport(msg, ref);
	p_ptr->last_in_seqno = 41;
	INIT_LIST_HEAD(&p_ptr->wait_list);
	INIT_LIST_HEAD(&p_ptr->subscription.nodesub_list);
	p_ptr->dispatcher = dispatcher;
	p_ptr->wakeup = wakeup;
241
	p_ptr->user_port = NULL;
P
Per Liden 已提交
242
	k_init_timer(&p_ptr->timer, (Handler)port_timeout, ref);
243
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
244 245 246
	INIT_LIST_HEAD(&p_ptr->publications);
	INIT_LIST_HEAD(&p_ptr->port_list);
	list_add_tail(&p_ptr->port_list, &ports);
247
	spin_unlock_bh(&tipc_port_list_lock);
248
	return p_ptr;
P
Per Liden 已提交
249 250 251 252
}

int tipc_deleteport(u32 ref)
{
253
	struct tipc_port *p_ptr;
254
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
255

256
	tipc_withdraw(ref, 0, NULL);
257
	p_ptr = tipc_port_lock(ref);
258
	if (!p_ptr)
P
Per Liden 已提交
259 260
		return -EINVAL;

261 262
	tipc_ref_discard(ref);
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
263 264

	k_cancel_timer(&p_ptr->timer);
265
	if (p_ptr->connected) {
P
Per Liden 已提交
266
		buf = port_build_peer_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
267
		tipc_nodesub_unsubscribe(&p_ptr->subscription);
P
Per Liden 已提交
268
	}
269
	kfree(p_ptr->user_port);
P
Per Liden 已提交
270

271
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
272 273
	list_del(&p_ptr->port_list);
	list_del(&p_ptr->wait_list);
274
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
275 276
	k_term_timer(&p_ptr->timer);
	kfree(p_ptr);
277
	tipc_net_route_msg(buf);
278
	return 0;
P
Per Liden 已提交
279 280
}

281
static int port_unreliable(struct tipc_port *p_ptr)
P
Per Liden 已提交
282
{
283
	return msg_src_droppable(&p_ptr->phdr);
P
Per Liden 已提交
284 285 286 287
}

int tipc_portunreliable(u32 ref, unsigned int *isunreliable)
{
288
	struct tipc_port *p_ptr;
289

290
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
291 292 293
	if (!p_ptr)
		return -EINVAL;
	*isunreliable = port_unreliable(p_ptr);
J
Julia Lawall 已提交
294
	tipc_port_unlock(p_ptr);
295
	return 0;
P
Per Liden 已提交
296 297 298 299
}

int tipc_set_portunreliable(u32 ref, unsigned int isunreliable)
{
300
	struct tipc_port *p_ptr;
301

302
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
303 304
	if (!p_ptr)
		return -EINVAL;
305
	msg_set_src_droppable(&p_ptr->phdr, (isunreliable != 0));
306
	tipc_port_unlock(p_ptr);
307
	return 0;
P
Per Liden 已提交
308 309
}

310
static int port_unreturnable(struct tipc_port *p_ptr)
P
Per Liden 已提交
311
{
312
	return msg_dest_droppable(&p_ptr->phdr);
P
Per Liden 已提交
313 314 315 316
}

int tipc_portunreturnable(u32 ref, unsigned int *isunrejectable)
{
317
	struct tipc_port *p_ptr;
318

319
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
320 321 322
	if (!p_ptr)
		return -EINVAL;
	*isunrejectable = port_unreturnable(p_ptr);
J
Julia Lawall 已提交
323
	tipc_port_unlock(p_ptr);
324
	return 0;
P
Per Liden 已提交
325 326 327 328
}

int tipc_set_portunreturnable(u32 ref, unsigned int isunrejectable)
{
329
	struct tipc_port *p_ptr;
330

331
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
332 333
	if (!p_ptr)
		return -EINVAL;
334
	msg_set_dest_droppable(&p_ptr->phdr, (isunrejectable != 0));
335
	tipc_port_unlock(p_ptr);
336
	return 0;
P
Per Liden 已提交
337 338
}

339 340 341
/*
 * port_build_proto_msg(): build a port level protocol
 * or a connection abortion message. Called with
P
Per Liden 已提交
342 343 344 345
 * tipc_port lock on.
 */
static struct sk_buff *port_build_proto_msg(u32 destport, u32 destnode,
					    u32 origport, u32 orignode,
346
					    u32 usr, u32 type, u32 err,
P
Per Liden 已提交
347 348 349 350
					    u32 seqno, u32 ack)
{
	struct sk_buff *buf;
	struct tipc_msg *msg;
351

352
	buf = tipc_buf_acquire(LONG_H_SIZE);
P
Per Liden 已提交
353 354
	if (buf) {
		msg = buf_msg(buf);
355
		tipc_msg_init(msg, usr, type, LONG_H_SIZE, destnode);
356
		msg_set_errcode(msg, err);
P
Per Liden 已提交
357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390
		msg_set_destport(msg, destport);
		msg_set_origport(msg, origport);
		msg_set_orignode(msg, orignode);
		msg_set_transp_seqno(msg, seqno);
		msg_set_msgcnt(msg, ack);
	}
	return buf;
}

int tipc_reject_msg(struct sk_buff *buf, u32 err)
{
	struct tipc_msg *msg = buf_msg(buf);
	struct sk_buff *rbuf;
	struct tipc_msg *rmsg;
	int hdr_sz;
	u32 imp = msg_importance(msg);
	u32 data_sz = msg_data_sz(msg);

	if (data_sz > MAX_REJECT_SIZE)
		data_sz = MAX_REJECT_SIZE;
	if (msg_connected(msg) && (imp < TIPC_CRITICAL_IMPORTANCE))
		imp++;

	/* discard rejected message if it shouldn't be returned to sender */
	if (msg_errcode(msg) || msg_dest_droppable(msg)) {
		buf_discard(buf);
		return data_sz;
	}

	/* construct rejected message */
	if (msg_mcast(msg))
		hdr_sz = MCAST_H_SIZE;
	else
		hdr_sz = LONG_H_SIZE;
391
	rbuf = tipc_buf_acquire(data_sz + hdr_sz);
P
Per Liden 已提交
392 393 394 395 396
	if (rbuf == NULL) {
		buf_discard(buf);
		return data_sz;
	}
	rmsg = buf_msg(rbuf);
397
	tipc_msg_init(rmsg, imp, msg_type(msg), hdr_sz, msg_orignode(msg));
398
	msg_set_errcode(rmsg, err);
P
Per Liden 已提交
399 400
	msg_set_destport(rmsg, msg_origport(msg));
	msg_set_origport(rmsg, msg_destport(msg));
401
	if (msg_short(msg)) {
P
Per Liden 已提交
402
		msg_set_orignode(rmsg, tipc_own_addr);
403 404
		/* leave name type & instance as zeroes */
	} else {
P
Per Liden 已提交
405
		msg_set_orignode(rmsg, msg_destnode(msg));
406 407 408
		msg_set_nametype(rmsg, msg_nametype(msg));
		msg_set_nameinst(rmsg, msg_nameinst(msg));
	}
409
	msg_set_size(rmsg, data_sz + hdr_sz);
410
	skb_copy_to_linear_data_offset(rbuf, hdr_sz, msg_data(msg), data_sz);
P
Per Liden 已提交
411 412 413

	/* send self-abort message when rejecting on a connected port */
	if (msg_connected(msg)) {
414
		struct sk_buff *abuf = NULL;
415
		struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
P
Per Liden 已提交
416 417

		if (p_ptr) {
418
			if (p_ptr->connected)
P
Per Liden 已提交
419
				abuf = port_build_self_abort_msg(p_ptr, err);
420
			tipc_port_unlock(p_ptr);
P
Per Liden 已提交
421
		}
422
		tipc_net_route_msg(abuf);
P
Per Liden 已提交
423 424 425 426
	}

	/* send rejected message */
	buf_discard(buf);
427
	tipc_net_route_msg(rbuf);
P
Per Liden 已提交
428 429 430
	return data_sz;
}

431
int tipc_port_reject_sections(struct tipc_port *p_ptr, struct tipc_msg *hdr,
432 433
			      struct iovec const *msg_sect, u32 num_sect,
			      int err)
P
Per Liden 已提交
434 435 436 437
{
	struct sk_buff *buf;
	int res;

438
	res = tipc_msg_build(hdr, msg_sect, num_sect, MAX_MSG_SIZE,
P
Per Liden 已提交
439 440 441 442 443 444 445 446 447
			!p_ptr->user_port, &buf);
	if (!buf)
		return res;

	return tipc_reject_msg(buf, err);
}

static void port_timeout(unsigned long ref)
{
448
	struct tipc_port *p_ptr = tipc_port_lock(ref);
449
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
450

451 452 453
	if (!p_ptr)
		return;

454
	if (!p_ptr->connected) {
455
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
456
		return;
457
	}
P
Per Liden 已提交
458 459 460 461 462 463 464

	/* Last probe answered ? */
	if (p_ptr->probing_state == PROBING) {
		buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_PORT);
	} else {
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
465
					   p_ptr->ref,
P
Per Liden 已提交
466 467 468
					   tipc_own_addr,
					   CONN_MANAGER,
					   CONN_PROBE,
469
					   TIPC_OK,
P
Per Liden 已提交
470 471 472 473 474 475
					   port_out_seqno(p_ptr),
					   0);
		port_incr_out_seqno(p_ptr);
		p_ptr->probing_state = PROBING;
		k_start_timer(&p_ptr->timer, p_ptr->probing_interval);
	}
476 477
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
478 479 480 481 482
}


static void port_handle_node_down(unsigned long ref)
{
483
	struct tipc_port *p_ptr = tipc_port_lock(ref);
484
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
485 486 487 488

	if (!p_ptr)
		return;
	buf = port_build_self_abort_msg(p_ptr, TIPC_ERR_NO_NODE);
489 490
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
491 492 493
}


494
static struct sk_buff *port_build_self_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
495
{
496
	u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
497

498
	if (!p_ptr->connected)
499
		return NULL;
P
Per Liden 已提交
500 501
	if (imp < TIPC_CRITICAL_IMPORTANCE)
		imp++;
502
	return port_build_proto_msg(p_ptr->ref,
P
Per Liden 已提交
503 504 505 506 507
				    tipc_own_addr,
				    port_peerport(p_ptr),
				    port_peernode(p_ptr),
				    imp,
				    TIPC_CONN_MSG,
508
				    err,
P
Per Liden 已提交
509 510 511 512 513
				    p_ptr->last_in_seqno + 1,
				    0);
}


514
static struct sk_buff *port_build_peer_abort_msg(struct tipc_port *p_ptr, u32 err)
P
Per Liden 已提交
515
{
516
	u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
517

518
	if (!p_ptr->connected)
519
		return NULL;
P
Per Liden 已提交
520 521 522 523
	if (imp < TIPC_CRITICAL_IMPORTANCE)
		imp++;
	return port_build_proto_msg(port_peerport(p_ptr),
				    port_peernode(p_ptr),
524
				    p_ptr->ref,
P
Per Liden 已提交
525 526 527
				    tipc_own_addr,
				    imp,
				    TIPC_CONN_MSG,
528
				    err,
P
Per Liden 已提交
529 530 531 532
				    port_out_seqno(p_ptr),
				    0);
}

533
void tipc_port_recv_proto_msg(struct sk_buff *buf)
P
Per Liden 已提交
534 535
{
	struct tipc_msg *msg = buf_msg(buf);
536
	struct tipc_port *p_ptr = tipc_port_lock(msg_destport(msg));
P
Per Liden 已提交
537
	u32 err = TIPC_OK;
538 539
	struct sk_buff *r_buf = NULL;
	struct sk_buff *abort_buf = NULL;
P
Per Liden 已提交
540 541 542

	if (!p_ptr) {
		err = TIPC_ERR_NO_PORT;
543
	} else if (p_ptr->connected) {
544 545
		if ((port_peernode(p_ptr) != msg_orignode(msg)) ||
		    (port_peerport(p_ptr) != msg_origport(msg))) {
P
Per Liden 已提交
546
			err = TIPC_ERR_NO_PORT;
547
		} else if (msg_type(msg) == CONN_ACK) {
548
			int wakeup = tipc_port_congested(p_ptr) &&
549
				     p_ptr->congested &&
P
Per Liden 已提交
550 551
				     p_ptr->wakeup;
			p_ptr->acked += msg_msgcnt(msg);
552
			if (tipc_port_congested(p_ptr))
P
Per Liden 已提交
553
				goto exit;
554
			p_ptr->congested = 0;
P
Per Liden 已提交
555 556
			if (!wakeup)
				goto exit;
557
			p_ptr->wakeup(p_ptr);
P
Per Liden 已提交
558 559
			goto exit;
		}
560
	} else if (p_ptr->published) {
P
Per Liden 已提交
561 562 563 564
		err = TIPC_ERR_NO_PORT;
	}
	if (err) {
		r_buf = port_build_proto_msg(msg_origport(msg),
565 566
					     msg_orignode(msg),
					     msg_destport(msg),
P
Per Liden 已提交
567
					     tipc_own_addr,
568
					     TIPC_HIGH_IMPORTANCE,
P
Per Liden 已提交
569 570 571 572 573 574 575 576 577
					     TIPC_CONN_MSG,
					     err,
					     0,
					     0);
		goto exit;
	}

	/* All is fine */
	if (msg_type(msg) == CONN_PROBE) {
578 579 580 581
		r_buf = port_build_proto_msg(msg_origport(msg),
					     msg_orignode(msg),
					     msg_destport(msg),
					     tipc_own_addr,
P
Per Liden 已提交
582 583 584 585 586 587 588 589 590 591
					     CONN_MANAGER,
					     CONN_PROBE_REPLY,
					     TIPC_OK,
					     port_out_seqno(p_ptr),
					     0);
	}
	p_ptr->probing_state = CONFIRMED;
	port_incr_out_seqno(p_ptr);
exit:
	if (p_ptr)
592 593 594
		tipc_port_unlock(p_ptr);
	tipc_net_route_msg(r_buf);
	tipc_net_route_msg(abort_buf);
P
Per Liden 已提交
595 596 597
	buf_discard(buf);
}

598
static void port_print(struct tipc_port *p_ptr, struct print_buf *buf, int full_id)
P
Per Liden 已提交
599
{
600
	struct publication *publ;
P
Per Liden 已提交
601 602

	if (full_id)
603
		tipc_printf(buf, "<%u.%u.%u:%u>:",
P
Per Liden 已提交
604
			    tipc_zone(tipc_own_addr), tipc_cluster(tipc_own_addr),
605
			    tipc_node(tipc_own_addr), p_ptr->ref);
P
Per Liden 已提交
606
	else
607
		tipc_printf(buf, "%-10u:", p_ptr->ref);
P
Per Liden 已提交
608

609
	if (p_ptr->connected) {
610 611 612 613 614 615
		u32 dport = port_peerport(p_ptr);
		u32 destnode = port_peernode(p_ptr);

		tipc_printf(buf, " connected to <%u.%u.%u:%u>",
			    tipc_zone(destnode), tipc_cluster(destnode),
			    tipc_node(destnode), dport);
616
		if (p_ptr->conn_type != 0)
617
			tipc_printf(buf, " via {%u,%u}",
618 619 620
				    p_ptr->conn_type,
				    p_ptr->conn_instance);
	} else if (p_ptr->published) {
621 622
		tipc_printf(buf, " bound to");
		list_for_each_entry(publ, &p_ptr->publications, pport_list) {
P
Per Liden 已提交
623 624 625 626
			if (publ->lower == publ->upper)
				tipc_printf(buf, " {%u,%u}", publ->type,
					    publ->lower);
			else
627
				tipc_printf(buf, " {%u,%u,%u}", publ->type,
P
Per Liden 已提交
628
					    publ->lower, publ->upper);
629 630 631
		}
	}
	tipc_printf(buf, "\n");
P
Per Liden 已提交
632 633 634 635
}

#define MAX_PORT_QUERY 32768

636
struct sk_buff *tipc_port_get_ports(void)
P
Per Liden 已提交
637 638 639 640
{
	struct sk_buff *buf;
	struct tlv_desc *rep_tlv;
	struct print_buf pb;
641
	struct tipc_port *p_ptr;
P
Per Liden 已提交
642 643
	int str_len;

644
	buf = tipc_cfg_reply_alloc(TLV_SPACE(MAX_PORT_QUERY));
P
Per Liden 已提交
645 646 647 648
	if (!buf)
		return NULL;
	rep_tlv = (struct tlv_desc *)buf->data;

649 650
	tipc_printbuf_init(&pb, TLV_DATA(rep_tlv), MAX_PORT_QUERY);
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
651
	list_for_each_entry(p_ptr, &ports, port_list) {
652
		spin_lock_bh(p_ptr->lock);
P
Per Liden 已提交
653
		port_print(p_ptr, &pb, 0);
654
		spin_unlock_bh(p_ptr->lock);
P
Per Liden 已提交
655
	}
656 657
	spin_unlock_bh(&tipc_port_list_lock);
	str_len = tipc_printbuf_validate(&pb);
P
Per Liden 已提交
658 659 660 661 662 663 664

	skb_put(buf, TLV_SPACE(str_len));
	TLV_SET(rep_tlv, TIPC_TLV_ULTRA_STRING, NULL, str_len);

	return buf;
}

665
void tipc_port_reinit(void)
P
Per Liden 已提交
666
{
667
	struct tipc_port *p_ptr;
P
Per Liden 已提交
668 669
	struct tipc_msg *msg;

670
	spin_lock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
671
	list_for_each_entry(p_ptr, &ports, port_list) {
672
		msg = &p_ptr->phdr;
P
Per Liden 已提交
673 674
		if (msg_orignode(msg) == tipc_own_addr)
			break;
675
		msg_set_prevnode(msg, tipc_own_addr);
P
Per Liden 已提交
676 677
		msg_set_orignode(msg, tipc_own_addr);
	}
678
	spin_unlock_bh(&tipc_port_list_lock);
P
Per Liden 已提交
679 680 681 682 683 684 685 686 687 688 689 690 691 692
}


/*
 *  port_dispatcher_sigh(): Signal handler for messages destinated
 *                          to the tipc_port interface.
 */

static void port_dispatcher_sigh(void *dummy)
{
	struct sk_buff *buf;

	spin_lock_bh(&queue_lock);
	buf = msg_queue_head;
693
	msg_queue_head = NULL;
P
Per Liden 已提交
694 695 696
	spin_unlock_bh(&queue_lock);

	while (buf) {
697
		struct tipc_port *p_ptr;
P
Per Liden 已提交
698 699 700 701 702 703
		struct user_port *up_ptr;
		struct tipc_portid orig;
		struct tipc_name_seq dseq;
		void *usr_handle;
		int connected;
		int published;
704
		u32 message_type;
P
Per Liden 已提交
705 706 707 708

		struct sk_buff *next = buf->next;
		struct tipc_msg *msg = buf_msg(buf);
		u32 dref = msg_destport(msg);
709

710 711 712 713
		message_type = msg_type(msg);
		if (message_type > TIPC_DIRECT_MSG)
			goto reject;	/* Unsupported message type */

714
		p_ptr = tipc_port_lock(dref);
715 716 717
		if (!p_ptr)
			goto reject;	/* Port deleted while msg in queue */

P
Per Liden 已提交
718 719 720 721
		orig.ref = msg_origport(msg);
		orig.node = msg_orignode(msg);
		up_ptr = p_ptr->user_port;
		usr_handle = up_ptr->usr_handle;
722 723
		connected = p_ptr->connected;
		published = p_ptr->published;
P
Per Liden 已提交
724 725 726 727

		if (unlikely(msg_errcode(msg)))
			goto err;

728
		switch (message_type) {
729

P
Per Liden 已提交
730 731 732 733
		case TIPC_CONN_MSG:{
				tipc_conn_msg_event cb = up_ptr->conn_msg_cb;
				u32 peer_port = port_peerport(p_ptr);
				u32 peer_node = port_peernode(p_ptr);
734
				u32 dsz;
P
Per Liden 已提交
735

J
Julia Lawall 已提交
736
				tipc_port_unlock(p_ptr);
737 738
				if (unlikely(!cb))
					goto reject;
P
Per Liden 已提交
739
				if (unlikely(!connected)) {
740
					if (tipc_connect2port(dref, &orig))
P
Per Liden 已提交
741
						goto reject;
742 743
				} else if ((msg_origport(msg) != peer_port) ||
					   (msg_orignode(msg) != peer_node))
P
Per Liden 已提交
744
					goto reject;
745 746 747 748
				dsz = msg_data_sz(msg);
				if (unlikely(dsz &&
					     (++p_ptr->conn_unacked >=
					      TIPC_FLOW_CONTROL_WIN)))
749
					tipc_acknowledge(dref,
750
							 p_ptr->conn_unacked);
P
Per Liden 已提交
751
				skb_pull(buf, msg_hdr_sz(msg));
752
				cb(usr_handle, dref, &buf, msg_data(msg), dsz);
P
Per Liden 已提交
753 754 755 756 757
				break;
			}
		case TIPC_DIRECT_MSG:{
				tipc_msg_event cb = up_ptr->msg_cb;

J
Julia Lawall 已提交
758
				tipc_port_unlock(p_ptr);
759
				if (unlikely(!cb || connected))
P
Per Liden 已提交
760 761
					goto reject;
				skb_pull(buf, msg_hdr_sz(msg));
762
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
763 764 765 766
				   msg_data_sz(msg), msg_importance(msg),
				   &orig);
				break;
			}
767
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
768 769 770
		case TIPC_NAMED_MSG:{
				tipc_named_msg_event cb = up_ptr->named_msg_cb;

J
Julia Lawall 已提交
771
				tipc_port_unlock(p_ptr);
772
				if (unlikely(!cb || connected || !published))
P
Per Liden 已提交
773 774 775
					goto reject;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
776 777
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
778
				skb_pull(buf, msg_hdr_sz(msg));
779
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
780 781 782 783 784 785 786 787 788 789
				   msg_data_sz(msg), msg_importance(msg),
				   &orig, &dseq);
				break;
			}
		}
		if (buf)
			buf_discard(buf);
		buf = next;
		continue;
err:
790
		switch (message_type) {
791

P
Per Liden 已提交
792
		case TIPC_CONN_MSG:{
793
				tipc_conn_shutdown_event cb =
P
Per Liden 已提交
794 795 796 797
					up_ptr->conn_err_cb;
				u32 peer_port = port_peerport(p_ptr);
				u32 peer_node = port_peernode(p_ptr);

J
Julia Lawall 已提交
798
				tipc_port_unlock(p_ptr);
799
				if (!cb || !connected)
P
Per Liden 已提交
800
					break;
801 802
				if ((msg_origport(msg) != peer_port) ||
				    (msg_orignode(msg) != peer_node))
P
Per Liden 已提交
803 804 805 806 807 808 809 810 811 812
					break;
				tipc_disconnect(dref);
				skb_pull(buf, msg_hdr_sz(msg));
				cb(usr_handle, dref, &buf, msg_data(msg),
				   msg_data_sz(msg), msg_errcode(msg));
				break;
			}
		case TIPC_DIRECT_MSG:{
				tipc_msg_err_event cb = up_ptr->err_cb;

J
Julia Lawall 已提交
813
				tipc_port_unlock(p_ptr);
814
				if (!cb || connected)
P
Per Liden 已提交
815 816 817 818 819 820
					break;
				skb_pull(buf, msg_hdr_sz(msg));
				cb(usr_handle, dref, &buf, msg_data(msg),
				   msg_data_sz(msg), msg_errcode(msg), &orig);
				break;
			}
821
		case TIPC_MCAST_MSG:
P
Per Liden 已提交
822
		case TIPC_NAMED_MSG:{
823
				tipc_named_msg_err_event cb =
P
Per Liden 已提交
824 825
					up_ptr->named_err_cb;

J
Julia Lawall 已提交
826
				tipc_port_unlock(p_ptr);
827
				if (!cb || connected)
P
Per Liden 已提交
828 829 830
					break;
				dseq.type =  msg_nametype(msg);
				dseq.lower = msg_nameinst(msg);
831 832
				dseq.upper = (message_type == TIPC_NAMED_MSG)
					? dseq.lower : msg_nameupper(msg);
P
Per Liden 已提交
833
				skb_pull(buf, msg_hdr_sz(msg));
834
				cb(usr_handle, dref, &buf, msg_data(msg),
P
Per Liden 已提交
835 836 837 838 839 840 841 842 843 844 845 846 847 848 849 850 851 852 853 854 855 856 857 858 859 860 861 862
				   msg_data_sz(msg), msg_errcode(msg), &dseq);
				break;
			}
		}
		if (buf)
			buf_discard(buf);
		buf = next;
		continue;
reject:
		tipc_reject_msg(buf, TIPC_ERR_NO_PORT);
		buf = next;
	}
}

/*
 *  port_dispatcher(): Dispatcher for messages destinated
 *  to the tipc_port interface. Called with port locked.
 */

static u32 port_dispatcher(struct tipc_port *dummy, struct sk_buff *buf)
{
	buf->next = NULL;
	spin_lock_bh(&queue_lock);
	if (msg_queue_head) {
		msg_queue_tail->next = buf;
		msg_queue_tail = buf;
	} else {
		msg_queue_tail = msg_queue_head = buf;
863
		tipc_k_signal((Handler)port_dispatcher_sigh, 0);
P
Per Liden 已提交
864 865
	}
	spin_unlock_bh(&queue_lock);
866
	return 0;
P
Per Liden 已提交
867 868
}

869
/*
P
Per Liden 已提交
870
 * Wake up port after congestion: Called with port locked,
871
 *
P
Per Liden 已提交
872 873 874 875
 */

static void port_wakeup_sh(unsigned long ref)
{
876
	struct tipc_port *p_ptr;
P
Per Liden 已提交
877
	struct user_port *up_ptr;
878 879
	tipc_continue_event cb = NULL;
	void *uh = NULL;
P
Per Liden 已提交
880

881
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
882 883 884 885 886 887
	if (p_ptr) {
		up_ptr = p_ptr->user_port;
		if (up_ptr) {
			cb = up_ptr->continue_event_cb;
			uh = up_ptr->usr_handle;
		}
888
		tipc_port_unlock(p_ptr);
P
Per Liden 已提交
889 890 891 892 893 894 895 896
	}
	if (cb)
		cb(uh, ref);
}


static void port_wakeup(struct tipc_port *p_ptr)
{
897
	tipc_k_signal((Handler)port_wakeup_sh, p_ptr->ref);
P
Per Liden 已提交
898 899 900 901
}

void tipc_acknowledge(u32 ref, u32 ack)
{
902
	struct tipc_port *p_ptr;
903
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
904

905
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
906 907
	if (!p_ptr)
		return;
908 909
	if (p_ptr->connected) {
		p_ptr->conn_unacked -= ack;
P
Per Liden 已提交
910 911 912 913 914 915
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
					   ref,
					   tipc_own_addr,
					   CONN_MANAGER,
					   CONN_ACK,
916
					   TIPC_OK,
P
Per Liden 已提交
917 918 919
					   port_out_seqno(p_ptr),
					   ack);
	}
920 921
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
922 923 924
}

/*
925
 * tipc_createport(): user level call.
P
Per Liden 已提交
926 927
 */

928
int tipc_createport(void *usr_handle,
929 930 931 932 933 934 935
		    unsigned int importance,
		    tipc_msg_err_event error_cb,
		    tipc_named_msg_err_event named_error_cb,
		    tipc_conn_shutdown_event conn_error_cb,
		    tipc_msg_event msg_cb,
		    tipc_named_msg_event named_msg_cb,
		    tipc_conn_msg_event conn_msg_cb,
P
Per Liden 已提交
936 937 938 939
		    tipc_continue_event continue_event_cb,/* May be zero */
		    u32 *portref)
{
	struct user_port *up_ptr;
940
	struct tipc_port *p_ptr;
P
Per Liden 已提交
941

942
	up_ptr = kmalloc(sizeof(*up_ptr), GFP_ATOMIC);
943
	if (!up_ptr) {
944
		warn("Port creation failed, no memory\n");
P
Per Liden 已提交
945 946
		return -ENOMEM;
	}
947
	p_ptr = (struct tipc_port *)tipc_createport_raw(NULL, port_dispatcher,
948 949
						   port_wakeup, importance);
	if (!p_ptr) {
P
Per Liden 已提交
950 951 952 953 954 955
		kfree(up_ptr);
		return -ENOMEM;
	}

	p_ptr->user_port = up_ptr;
	up_ptr->usr_handle = usr_handle;
956
	up_ptr->ref = p_ptr->ref;
P
Per Liden 已提交
957 958 959 960 961 962 963
	up_ptr->err_cb = error_cb;
	up_ptr->named_err_cb = named_error_cb;
	up_ptr->conn_err_cb = conn_error_cb;
	up_ptr->msg_cb = msg_cb;
	up_ptr->named_msg_cb = named_msg_cb;
	up_ptr->conn_msg_cb = conn_msg_cb;
	up_ptr->continue_event_cb = continue_event_cb;
964
	*portref = p_ptr->ref;
965
	tipc_port_unlock(p_ptr);
966
	return 0;
P
Per Liden 已提交
967 968 969 970
}

int tipc_portimportance(u32 ref, unsigned int *importance)
{
971
	struct tipc_port *p_ptr;
972

973
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
974 975
	if (!p_ptr)
		return -EINVAL;
976
	*importance = (unsigned int)msg_importance(&p_ptr->phdr);
J
Julia Lawall 已提交
977
	tipc_port_unlock(p_ptr);
978
	return 0;
P
Per Liden 已提交
979 980 981 982
}

int tipc_set_portimportance(u32 ref, unsigned int imp)
{
983
	struct tipc_port *p_ptr;
P
Per Liden 已提交
984 985 986 987

	if (imp > TIPC_CRITICAL_IMPORTANCE)
		return -EINVAL;

988
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
989 990
	if (!p_ptr)
		return -EINVAL;
991
	msg_set_importance(&p_ptr->phdr, (u32)imp);
J
Julia Lawall 已提交
992
	tipc_port_unlock(p_ptr);
993
	return 0;
P
Per Liden 已提交
994 995 996 997 998
}


int tipc_publish(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
999
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1000 1001 1002 1003
	struct publication *publ;
	u32 key;
	int res = -EINVAL;

1004
	p_ptr = tipc_port_lock(ref);
1005 1006 1007
	if (!p_ptr)
		return -EINVAL;

1008
	if (p_ptr->connected)
P
Per Liden 已提交
1009 1010 1011 1012 1013 1014 1015 1016 1017 1018
		goto exit;
	if (seq->lower > seq->upper)
		goto exit;
	if ((scope < TIPC_ZONE_SCOPE) || (scope > TIPC_NODE_SCOPE))
		goto exit;
	key = ref + p_ptr->pub_count + 1;
	if (key == ref) {
		res = -EADDRINUSE;
		goto exit;
	}
1019
	publ = tipc_nametbl_publish(seq->type, seq->lower, seq->upper,
1020
				    scope, p_ptr->ref, key);
P
Per Liden 已提交
1021 1022 1023
	if (publ) {
		list_add(&publ->pport_list, &p_ptr->publications);
		p_ptr->pub_count++;
1024
		p_ptr->published = 1;
1025
		res = 0;
P
Per Liden 已提交
1026 1027
	}
exit:
1028
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1029 1030 1031 1032 1033
	return res;
}

int tipc_withdraw(u32 ref, unsigned int scope, struct tipc_name_seq const *seq)
{
1034
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1035 1036 1037
	struct publication *publ;
	struct publication *tpubl;
	int res = -EINVAL;
1038

1039
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1040 1041 1042
	if (!p_ptr)
		return -EINVAL;
	if (!seq) {
1043
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1044
					 &p_ptr->publications, pport_list) {
1045
			tipc_nametbl_withdraw(publ->type, publ->lower,
1046
					      publ->ref, publ->key);
P
Per Liden 已提交
1047
		}
1048
		res = 0;
P
Per Liden 已提交
1049
	} else {
1050
		list_for_each_entry_safe(publ, tpubl,
P
Per Liden 已提交
1051 1052 1053 1054 1055 1056 1057 1058 1059
					 &p_ptr->publications, pport_list) {
			if (publ->scope != scope)
				continue;
			if (publ->type != seq->type)
				continue;
			if (publ->lower != seq->lower)
				continue;
			if (publ->upper != seq->upper)
				break;
1060
			tipc_nametbl_withdraw(publ->type, publ->lower,
1061
					      publ->ref, publ->key);
1062
			res = 0;
P
Per Liden 已提交
1063 1064 1065 1066
			break;
		}
	}
	if (list_empty(&p_ptr->publications))
1067
		p_ptr->published = 0;
1068
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1069 1070 1071 1072 1073
	return res;
}

int tipc_connect2port(u32 ref, struct tipc_portid const *peer)
{
1074
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1075 1076 1077
	struct tipc_msg *msg;
	int res = -EINVAL;

1078
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1079 1080
	if (!p_ptr)
		return -EINVAL;
1081
	if (p_ptr->published || p_ptr->connected)
P
Per Liden 已提交
1082 1083 1084 1085
		goto exit;
	if (!peer->ref)
		goto exit;

1086
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1087 1088 1089
	msg_set_destnode(msg, peer->node);
	msg_set_destport(msg, peer->ref);
	msg_set_orignode(msg, tipc_own_addr);
1090
	msg_set_origport(msg, p_ptr->ref);
P
Per Liden 已提交
1091 1092
	msg_set_transp_seqno(msg, 42);
	msg_set_type(msg, TIPC_CONN_MSG);
1093
	msg_set_hdr_sz(msg, SHORT_H_SIZE);
P
Per Liden 已提交
1094 1095 1096

	p_ptr->probing_interval = PROBING_INTERVAL;
	p_ptr->probing_state = CONFIRMED;
1097
	p_ptr->connected = 1;
P
Per Liden 已提交
1098 1099
	k_start_timer(&p_ptr->timer, p_ptr->probing_interval);

1100
	tipc_nodesub_subscribe(&p_ptr->subscription, peer->node,
1101
			  (void *)(unsigned long)ref,
P
Per Liden 已提交
1102
			  (net_ev_handler)port_handle_node_down);
1103
	res = 0;
P
Per Liden 已提交
1104
exit:
1105
	tipc_port_unlock(p_ptr);
1106
	p_ptr->max_pkt = tipc_link_get_max_pkt(peer->node, ref);
P
Per Liden 已提交
1107 1108 1109
	return res;
}

1110 1111 1112 1113 1114 1115 1116 1117 1118 1119 1120 1121 1122 1123
/**
 * tipc_disconnect_port - disconnect port from peer
 *
 * Port must be locked.
 */

int tipc_disconnect_port(struct tipc_port *tp_ptr)
{
	int res;

	if (tp_ptr->connected) {
		tp_ptr->connected = 0;
		/* let timer expire on it's own to avoid deadlock! */
		tipc_nodesub_unsubscribe(
1124
			&((struct tipc_port *)tp_ptr)->subscription);
1125
		res = 0;
1126 1127 1128 1129 1130 1131
	} else {
		res = -ENOTCONN;
	}
	return res;
}

P
Per Liden 已提交
1132 1133 1134 1135 1136 1137 1138
/*
 * tipc_disconnect(): Disconnect port form peer.
 *                    This is a node local operation.
 */

int tipc_disconnect(u32 ref)
{
1139
	struct tipc_port *p_ptr;
1140
	int res;
P
Per Liden 已提交
1141

1142
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1143 1144
	if (!p_ptr)
		return -EINVAL;
1145
	res = tipc_disconnect_port((struct tipc_port *)p_ptr);
1146
	tipc_port_unlock(p_ptr);
P
Per Liden 已提交
1147 1148 1149 1150 1151 1152 1153 1154
	return res;
}

/*
 * tipc_shutdown(): Send a SHUTDOWN msg to peer and disconnect
 */
int tipc_shutdown(u32 ref)
{
1155
	struct tipc_port *p_ptr;
1156
	struct sk_buff *buf = NULL;
P
Per Liden 已提交
1157

1158
	p_ptr = tipc_port_lock(ref);
P
Per Liden 已提交
1159 1160 1161
	if (!p_ptr)
		return -EINVAL;

1162 1163
	if (p_ptr->connected) {
		u32 imp = msg_importance(&p_ptr->phdr);
P
Per Liden 已提交
1164 1165 1166 1167 1168 1169 1170 1171
		if (imp < TIPC_CRITICAL_IMPORTANCE)
			imp++;
		buf = port_build_proto_msg(port_peerport(p_ptr),
					   port_peernode(p_ptr),
					   ref,
					   tipc_own_addr,
					   imp,
					   TIPC_CONN_MSG,
1172
					   TIPC_CONN_SHUTDOWN,
P
Per Liden 已提交
1173 1174 1175
					   port_out_seqno(p_ptr),
					   0);
	}
1176 1177
	tipc_port_unlock(p_ptr);
	tipc_net_route_msg(buf);
P
Per Liden 已提交
1178 1179 1180 1181
	return tipc_disconnect(ref);
}

/*
1182
 *  tipc_port_recv_sections(): Concatenate and deliver sectioned
P
Per Liden 已提交
1183 1184 1185
 *                        message for this node.
 */

1186
static int tipc_port_recv_sections(struct tipc_port *sender, unsigned int num_sect,
1187
				   struct iovec const *msg_sect)
P
Per Liden 已提交
1188 1189 1190
{
	struct sk_buff *buf;
	int res;
1191

1192
	res = tipc_msg_build(&sender->phdr, msg_sect, num_sect,
P
Per Liden 已提交
1193 1194
			MAX_MSG_SIZE, !sender->user_port, &buf);
	if (likely(buf))
1195
		tipc_port_recv_msg(buf);
P
Per Liden 已提交
1196 1197 1198 1199 1200 1201 1202 1203 1204
	return res;
}

/**
 * tipc_send - send message sections on connection
 */

int tipc_send(u32 ref, unsigned int num_sect, struct iovec const *msg_sect)
{
1205
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1206 1207 1208
	u32 destnode;
	int res;

1209
	p_ptr = tipc_port_deref(ref);
1210
	if (!p_ptr || !p_ptr->connected)
P
Per Liden 已提交
1211 1212
		return -EINVAL;

1213
	p_ptr->congested = 1;
1214
	if (!tipc_port_congested(p_ptr)) {
P
Per Liden 已提交
1215 1216
		destnode = port_peernode(p_ptr);
		if (likely(destnode != tipc_own_addr))
1217 1218
			res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
							   destnode);
P
Per Liden 已提交
1219
		else
1220
			res = tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
P
Per Liden 已提交
1221 1222 1223

		if (likely(res != -ELINKCONG)) {
			port_incr_out_seqno(p_ptr);
1224
			p_ptr->congested = 0;
1225 1226
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1227 1228 1229 1230
			return res;
		}
	}
	if (port_unreliable(p_ptr)) {
1231
		p_ptr->congested = 0;
P
Per Liden 已提交
1232
		/* Just calculate msg length and return */
1233
		return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1234 1235 1236 1237 1238
	}
	return -ELINKCONG;
}

/**
1239
 * tipc_send2name - send message sections to port name
P
Per Liden 已提交
1240 1241
 */

1242 1243
int tipc_send2name(u32 ref, struct tipc_name const *name, unsigned int domain,
	   unsigned int num_sect, struct iovec const *msg_sect)
P
Per Liden 已提交
1244
{
1245
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1246 1247
	struct tipc_msg *msg;
	u32 destnode = domain;
1248
	u32 destport;
P
Per Liden 已提交
1249 1250
	int res;

1251
	p_ptr = tipc_port_deref(ref);
1252
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1253 1254
		return -EINVAL;

1255
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1256
	msg_set_type(msg, TIPC_NAMED_MSG);
1257 1258
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1259 1260 1261
	msg_set_hdr_sz(msg, LONG_H_SIZE);
	msg_set_nametype(msg, name->type);
	msg_set_nameinst(msg, name->instance);
1262
	msg_set_lookup_scope(msg, tipc_addr_scope(domain));
1263
	destport = tipc_nametbl_translate(name->type, name->instance, &destnode);
P
Per Liden 已提交
1264 1265 1266
	msg_set_destnode(msg, destnode);
	msg_set_destport(msg, destport);

1267
	if (likely(destport)) {
P
Per Liden 已提交
1268
		if (likely(destnode == tipc_own_addr))
1269 1270 1271 1272 1273 1274 1275 1276
			res = tipc_port_recv_sections(p_ptr, num_sect,
						      msg_sect);
		else
			res = tipc_link_send_sections_fast(p_ptr, msg_sect,
							   num_sect, destnode);
		if (likely(res != -ELINKCONG)) {
			if (res > 0)
				p_ptr->sent++;
P
Per Liden 已提交
1277
			return res;
1278
		}
P
Per Liden 已提交
1279 1280
		if (port_unreliable(p_ptr)) {
			/* Just calculate msg length and return */
1281
			return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1282 1283 1284
		}
		return -ELINKCONG;
	}
1285
	return tipc_port_reject_sections(p_ptr, msg, msg_sect, num_sect,
1286
					 TIPC_ERR_NO_NAME);
P
Per Liden 已提交
1287 1288 1289
}

/**
1290
 * tipc_send2port - send message sections to port identity
P
Per Liden 已提交
1291 1292
 */

1293 1294
int tipc_send2port(u32 ref, struct tipc_portid const *dest,
	   unsigned int num_sect, struct iovec const *msg_sect)
P
Per Liden 已提交
1295
{
1296
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1297 1298 1299
	struct tipc_msg *msg;
	int res;

1300
	p_ptr = tipc_port_deref(ref);
1301
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1302 1303
		return -EINVAL;

1304
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1305
	msg_set_type(msg, TIPC_DIRECT_MSG);
1306 1307
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1308 1309 1310
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
	msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
1311

P
Per Liden 已提交
1312
	if (dest->node == tipc_own_addr)
1313 1314 1315 1316 1317 1318 1319
		res =  tipc_port_recv_sections(p_ptr, num_sect, msg_sect);
	else
		res = tipc_link_send_sections_fast(p_ptr, msg_sect, num_sect,
						   dest->node);
	if (likely(res != -ELINKCONG)) {
		if (res > 0)
			p_ptr->sent++;
P
Per Liden 已提交
1320
		return res;
1321
	}
P
Per Liden 已提交
1322 1323
	if (port_unreliable(p_ptr)) {
		/* Just calculate msg length and return */
1324
		return tipc_msg_calc_data_size(msg_sect, num_sect);
P
Per Liden 已提交
1325 1326 1327 1328
	}
	return -ELINKCONG;
}

1329
/**
1330
 * tipc_send_buf2port - send message buffer to port identity
P
Per Liden 已提交
1331 1332
 */

1333 1334
int tipc_send_buf2port(u32 ref, struct tipc_portid const *dest,
	       struct sk_buff *buf, unsigned int dsz)
P
Per Liden 已提交
1335
{
1336
	struct tipc_port *p_ptr;
P
Per Liden 已提交
1337 1338 1339
	struct tipc_msg *msg;
	int res;

1340 1341
	p_ptr = (struct tipc_port *)tipc_ref_deref(ref);
	if (!p_ptr || p_ptr->connected)
P
Per Liden 已提交
1342 1343
		return -EINVAL;

1344
	msg = &p_ptr->phdr;
P
Per Liden 已提交
1345
	msg_set_type(msg, TIPC_DIRECT_MSG);
1346 1347
	msg_set_orignode(msg, tipc_own_addr);
	msg_set_origport(msg, ref);
P
Per Liden 已提交
1348 1349 1350 1351 1352 1353 1354 1355
	msg_set_destnode(msg, dest->node);
	msg_set_destport(msg, dest->ref);
	msg_set_hdr_sz(msg, DIR_MSG_H_SIZE);
	msg_set_size(msg, DIR_MSG_H_SIZE + dsz);
	if (skb_cow(buf, DIR_MSG_H_SIZE))
		return -ENOMEM;

	skb_push(buf, DIR_MSG_H_SIZE);
1356
	skb_copy_to_linear_data(buf, msg, DIR_MSG_H_SIZE);
1357

P
Per Liden 已提交
1358
	if (dest->node == tipc_own_addr)
1359 1360 1361 1362 1363 1364
		res = tipc_port_recv_msg(buf);
	else
		res = tipc_send_buf_fast(buf, dest->node);
	if (likely(res != -ELINKCONG)) {
		if (res > 0)
			p_ptr->sent++;
P
Per Liden 已提交
1365
		return res;
1366
	}
P
Per Liden 已提交
1367 1368 1369 1370 1371
	if (port_unreliable(p_ptr))
		return dsz;
	return -ELINKCONG;
}