subscr.c 15.5 KB
Newer Older
P
Per Liden 已提交
1
/*
2
 * net/tipc/subscr.c: TIPC network topology service
3
 *
P
Per Liden 已提交
4
 * Copyright (c) 2000-2006, Ericsson AB
5
 * Copyright (c) 2005-2007, 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
 * POSSIBILITY OF SUCH DAMAGE.
 */

#include "core.h"
#include "name_table.h"
39
#include "port.h"
40
#include "subscr.h"
P
Per Liden 已提交
41 42

/**
43
 * struct tipc_subscriber - TIPC network topology subscriber
44 45
 * @port_ref: object reference to server port connecting to subscriber
 * @lock: pointer to spinlock controlling access to subscriber's server port
P
Per Liden 已提交
46 47 48
 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
 * @subscription_list: list of subscription objects for this subscriber
 */
49
struct tipc_subscriber {
50
	u32 port_ref;
51
	spinlock_t *lock;
P
Per Liden 已提交
52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69
	struct list_head subscriber_list;
	struct list_head subscription_list;
};

/**
 * struct top_srv - TIPC network topology subscription service
 * @setup_port: reference to TIPC port that handles subscription requests
 * @subscription_count: number of active subscriptions (not subscribers!)
 * @subscriber_list: list of ports subscribing to service
 * @lock: spinlock govering access to subscriber list
 */
struct top_srv {
	u32 setup_port;
	atomic_t subscription_count;
	struct list_head subscriber_list;
	spinlock_t lock;
};

70
static struct top_srv topsrv;
P
Per Liden 已提交
71

72 73 74 75 76 77 78 79 80 81 82 83
/**
 * htohl - convert value to endianness used by destination
 * @in: value to convert
 * @swap: non-zero if endianness must be reversed
 *
 * Returns converted value
 */
static u32 htohl(u32 in, int swap)
{
	return swap ? swab32(in) : in;
}

P
Per Liden 已提交
84 85
/**
 * subscr_send_event - send a message containing a tipc_event to the subscriber
86 87 88
 *
 * Note: Must not hold subscriber's server port lock, since tipc_send() will
 *       try to take the lock if the message is rejected and returned!
P
Per Liden 已提交
89
 */
90
static void subscr_send_event(struct tipc_subscription *sub,
91
			      u32 found_lower,
P
Per Liden 已提交
92
			      u32 found_upper,
93 94
			      u32 event,
			      u32 port_ref,
P
Per Liden 已提交
95 96 97 98 99 100 101
			      u32 node)
{
	struct iovec msg_sect;

	msg_sect.iov_base = (void *)&sub->evt;
	msg_sect.iov_len = sizeof(struct tipc_event);

102 103 104 105 106
	sub->evt.event = htohl(event, sub->swap);
	sub->evt.found_lower = htohl(found_lower, sub->swap);
	sub->evt.found_upper = htohl(found_upper, sub->swap);
	sub->evt.port.ref = htohl(port_ref, sub->swap);
	sub->evt.port.node = htohl(node, sub->swap);
107
	tipc_send(sub->server_ref, 1, &msg_sect, msg_sect.iov_len);
P
Per Liden 已提交
108 109 110
}

/**
111
 * tipc_subscr_overlap - test for subscription overlap with the given values
P
Per Liden 已提交
112 113 114
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
115
int tipc_subscr_overlap(struct tipc_subscription *sub,
116
			u32 found_lower,
117
			u32 found_upper)
P
Per Liden 已提交
118 119 120 121 122 123 124 125 126 127 128 129

{
	if (found_lower < sub->seq.lower)
		found_lower = sub->seq.lower;
	if (found_upper > sub->seq.upper)
		found_upper = sub->seq.upper;
	if (found_lower > found_upper)
		return 0;
	return 1;
}

/**
130
 * tipc_subscr_report_overlap - issue event if there is subscription overlap
131
 *
P
Per Liden 已提交
132 133
 * Protected by nameseq.lock in name_table.c
 */
134
void tipc_subscr_report_overlap(struct tipc_subscription *sub,
135
				u32 found_lower,
136
				u32 found_upper,
137 138
				u32 event,
				u32 port_ref,
139 140
				u32 node,
				int must)
P
Per Liden 已提交
141
{
142
	if (!tipc_subscr_overlap(sub, found_lower, found_upper))
P
Per Liden 已提交
143
		return;
144
	if (!must && !(sub->filter & TIPC_SUB_PORTS))
P
Per Liden 已提交
145
		return;
146

147
	subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
P
Per Liden 已提交
148 149 150 151 152
}

/**
 * subscr_timeout - subscription timeout has occurred
 */
153
static void subscr_timeout(struct tipc_subscription *sub)
P
Per Liden 已提交
154
{
155
	struct tipc_port *server_port;
P
Per Liden 已提交
156

157 158 159
	/* Validate server port reference (in case subscriber is terminating) */
	server_port = tipc_port_lock(sub->server_ref);
	if (server_port == NULL)
P
Per Liden 已提交
160 161
		return;

162 163
	/* Validate timeout (in case subscription is being cancelled) */
	if (sub->timeout == TIPC_WAIT_FOREVER) {
164
		tipc_port_unlock(server_port);
165 166 167
		return;
	}

P
Per Liden 已提交
168
	/* Unlink subscription from name table */
169
	tipc_nametbl_unsubscribe(sub);
P
Per Liden 已提交
170

171
	/* Unlink subscription from subscriber */
P
Per Liden 已提交
172 173
	list_del(&sub->subscription_list);

174 175 176 177 178 179 180
	/* Release subscriber's server port */
	tipc_port_unlock(server_port);

	/* Notify subscriber of timeout */
	subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
			  TIPC_SUBSCR_TIMEOUT, 0, 0);

P
Per Liden 已提交
181 182 183 184 185 186
	/* Now destroy subscription */
	k_term_timer(&sub->timer);
	kfree(sub);
	atomic_dec(&topsrv.subscription_count);
}

187 188 189
/**
 * subscr_del - delete a subscription within a subscription list
 *
190
 * Called with subscriber port locked.
191
 */
192
static void subscr_del(struct tipc_subscription *sub)
193 194 195 196 197 198 199
{
	tipc_nametbl_unsubscribe(sub);
	list_del(&sub->subscription_list);
	kfree(sub);
	atomic_dec(&topsrv.subscription_count);
}

P
Per Liden 已提交
200 201
/**
 * subscr_terminate - terminate communication with a subscriber
202
 *
203
 * Called with subscriber port locked.  Routine must temporarily release lock
204
 * to enable subscription timeout routine(s) to finish without deadlocking;
P
Per Liden 已提交
205
 * the lock is then reclaimed to allow caller to release it upon return.
206
 * (This should work even in the unlikely event some other thread creates
P
Per Liden 已提交
207 208 209
 * a new object reference in the interim that uses this lock; this routine will
 * simply wait for it to be released, then claim it.)
 */
210
static void subscr_terminate(struct tipc_subscriber *subscriber)
P
Per Liden 已提交
211
{
212
	u32 port_ref;
213 214
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
P
Per Liden 已提交
215 216

	/* Invalidate subscriber reference */
217 218
	port_ref = subscriber->port_ref;
	subscriber->port_ref = 0;
P
Per Liden 已提交
219 220
	spin_unlock_bh(subscriber->lock);

221 222 223 224
	/* Sever connection to subscriber */
	tipc_shutdown(port_ref);
	tipc_deleteport(port_ref);

P
Per Liden 已提交
225 226 227 228 229 230 231
	/* Destroy any existing subscriptions for subscriber */
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
				 subscription_list) {
		if (sub->timeout != TIPC_WAIT_FOREVER) {
			k_cancel_timer(&sub->timer);
			k_term_timer(&sub->timer);
		}
232
		subscr_del(sub);
P
Per Liden 已提交
233 234 235 236 237 238 239
	}

	/* Remove subscriber from topology server's subscriber list */
	spin_lock_bh(&topsrv.lock);
	list_del(&subscriber->subscriber_list);
	spin_unlock_bh(&topsrv.lock);

240
	/* Reclaim subscriber lock */
P
Per Liden 已提交
241
	spin_lock_bh(subscriber->lock);
242 243

	/* Now destroy subscriber */
P
Per Liden 已提交
244 245 246
	kfree(subscriber);
}

247 248 249
/**
 * subscr_cancel - handle subscription cancellation request
 *
250
 * Called with subscriber port locked.  Routine must temporarily release lock
251 252 253 254 255 256
 * to enable the subscription timeout routine to finish without deadlocking;
 * the lock is then reclaimed to allow caller to release it upon return.
 *
 * Note that fields of 's' use subscriber's endianness!
 */
static void subscr_cancel(struct tipc_subscr *s,
257
			  struct tipc_subscriber *subscriber)
258
{
259 260
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
261 262 263 264 265
	int found = 0;

	/* Find first matching subscription, exit if not found */
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
				 subscription_list) {
266 267 268 269
		if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
			found = 1;
			break;
		}
270 271 272 273 274 275 276 277 278 279 280 281 282 283 284
	}
	if (!found)
		return;

	/* Cancel subscription timer (if used), then delete subscription */
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		sub->timeout = TIPC_WAIT_FOREVER;
		spin_unlock_bh(subscriber->lock);
		k_cancel_timer(&sub->timer);
		k_term_timer(&sub->timer);
		spin_lock_bh(subscriber->lock);
	}
	subscr_del(sub);
}

P
Per Liden 已提交
285 286
/**
 * subscr_subscribe - create subscription for subscriber
287
 *
288
 * Called with subscriber port locked.
P
Per Liden 已提交
289
 */
290
static struct tipc_subscription *subscr_subscribe(struct tipc_subscr *s,
291
					     struct tipc_subscriber *subscriber)
P
Per Liden 已提交
292
{
293
	struct tipc_subscription *sub;
294 295 296 297
	int swap;

	/* Determine subscriber's endianness */
	swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
298 299

	/* Detect & process a subscription cancellation request */
300 301
	if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
		s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
302
		subscr_cancel(s, subscriber);
303
		return NULL;
304 305
	}

P
Per Liden 已提交
306
	/* Refuse subscription if global limit exceeded */
307
	if (atomic_read(&topsrv.subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
308
		pr_warn("Subscription rejected, limit reached (%u)\n",
309
			TIPC_MAX_SUBSCRIPTIONS);
P
Per Liden 已提交
310
		subscr_terminate(subscriber);
311
		return NULL;
P
Per Liden 已提交
312 313 314
	}

	/* Allocate subscription object */
315
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
316
	if (!sub) {
317
		pr_warn("Subscription rejected, no memory\n");
P
Per Liden 已提交
318
		subscr_terminate(subscriber);
319
		return NULL;
P
Per Liden 已提交
320 321 322
	}

	/* Initialize subscription object */
323 324 325 326 327
	sub->seq.type = htohl(s->seq.type, swap);
	sub->seq.lower = htohl(s->seq.lower, swap);
	sub->seq.upper = htohl(s->seq.upper, swap);
	sub->timeout = htohl(s->timeout, swap);
	sub->filter = htohl(s->filter, swap);
328 329
	if ((!(sub->filter & TIPC_SUB_PORTS) ==
	     !(sub->filter & TIPC_SUB_SERVICE)) ||
330
	    (sub->seq.lower > sub->seq.upper)) {
331
		pr_warn("Subscription rejected, illegal request\n");
P
Per Liden 已提交
332 333
		kfree(sub);
		subscr_terminate(subscriber);
334
		return NULL;
P
Per Liden 已提交
335 336 337
	}
	INIT_LIST_HEAD(&sub->nameseq_list);
	list_add(&sub->subscription_list, &subscriber->subscription_list);
338
	sub->server_ref = subscriber->port_ref;
339
	sub->swap = swap;
340
	memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
P
Per Liden 已提交
341 342 343 344 345 346
	atomic_inc(&topsrv.subscription_count);
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		k_init_timer(&sub->timer,
			     (Handler)subscr_timeout, (unsigned long)sub);
		k_start_timer(&sub->timer, sub->timeout);
	}
347 348

	return sub;
P
Per Liden 已提交
349 350 351 352
}

/**
 * subscr_conn_shutdown_event - handle termination request from subscriber
353 354
 *
 * Called with subscriber's server port unlocked.
P
Per Liden 已提交
355 356
 */
static void subscr_conn_shutdown_event(void *usr_handle,
357
				       u32 port_ref,
P
Per Liden 已提交
358 359 360 361 362
				       struct sk_buff **buf,
				       unsigned char const *data,
				       unsigned int size,
				       int reason)
{
363
	struct tipc_subscriber *subscriber = usr_handle;
P
Per Liden 已提交
364 365
	spinlock_t *subscriber_lock;

366
	if (tipc_port_lock(port_ref) == NULL)
P
Per Liden 已提交
367 368 369 370 371 372 373 374 375
		return;

	subscriber_lock = subscriber->lock;
	subscr_terminate(subscriber);
	spin_unlock_bh(subscriber_lock);
}

/**
 * subscr_conn_msg_event - handle new subscription request from subscriber
376 377
 *
 * Called with subscriber's server port unlocked.
P
Per Liden 已提交
378 379 380 381 382 383 384
 */
static void subscr_conn_msg_event(void *usr_handle,
				  u32 port_ref,
				  struct sk_buff **buf,
				  const unchar *data,
				  u32 size)
{
385
	struct tipc_subscriber *subscriber = usr_handle;
P
Per Liden 已提交
386
	spinlock_t *subscriber_lock;
387
	struct tipc_subscription *sub;
388 389 390 391 392 393

	/*
	 * Lock subscriber's server port (& make a local copy of lock pointer,
	 * in case subscriber is deleted while processing subscription request)
	 */
	if (tipc_port_lock(port_ref) == NULL)
P
Per Liden 已提交
394 395 396
		return;

	subscriber_lock = subscriber->lock;
397

398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422
	if (size != sizeof(struct tipc_subscr)) {
		subscr_terminate(subscriber);
		spin_unlock_bh(subscriber_lock);
	} else {
		sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
		spin_unlock_bh(subscriber_lock);
		if (sub != NULL) {

			/*
			 * We must release the server port lock before adding a
			 * subscription to the name table since TIPC needs to be
			 * able to (re)acquire the port lock if an event message
			 * issued by the subscription process is rejected and
			 * returned.  The subscription cannot be deleted while
			 * it is being added to the name table because:
			 * a) the single-threading of the native API port code
			 *    ensures the subscription cannot be cancelled and
			 *    the subscriber connection cannot be broken, and
			 * b) the name table lock ensures the subscription
			 *    timeout code cannot delete the subscription,
			 * so the subscription object is still protected.
			 */
			tipc_nametbl_subscribe(sub);
		}
	}
P
Per Liden 已提交
423 424 425 426 427 428 429 430 431 432
}

/**
 * subscr_named_msg_event - handle request to establish a new subscriber
 */
static void subscr_named_msg_event(void *usr_handle,
				   u32 port_ref,
				   struct sk_buff **buf,
				   const unchar *data,
				   u32 size,
433
				   u32 importance,
P
Per Liden 已提交
434 435 436
				   struct tipc_portid const *orig,
				   struct tipc_name_seq const *dest)
{
437
	struct tipc_subscriber *subscriber;
438
	u32 server_port_ref;
P
Per Liden 已提交
439 440

	/* Create subscriber object */
441
	subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
P
Per Liden 已提交
442
	if (subscriber == NULL) {
443
		pr_warn("Subscriber rejected, no memory\n");
P
Per Liden 已提交
444 445 446 447 448
		return;
	}
	INIT_LIST_HEAD(&subscriber->subscription_list);
	INIT_LIST_HEAD(&subscriber->subscriber_list);

449
	/* Create server port & establish connection to subscriber */
450
	tipc_createport(subscriber,
P
Per Liden 已提交
451
			importance,
452 453
			NULL,
			NULL,
P
Per Liden 已提交
454
			subscr_conn_shutdown_event,
455 456
			NULL,
			NULL,
P
Per Liden 已提交
457
			subscr_conn_msg_event,
458
			NULL,
P
Per Liden 已提交
459 460
			&subscriber->port_ref);
	if (subscriber->port_ref == 0) {
461
		pr_warn("Subscriber rejected, unable to create port\n");
P
Per Liden 已提交
462 463 464
		kfree(subscriber);
		return;
	}
465
	tipc_connect(subscriber->port_ref, orig);
P
Per Liden 已提交
466

467
	/* Lock server port (& save lock address for future use) */
468
	subscriber->lock = tipc_port_lock(subscriber->port_ref)->lock;
P
Per Liden 已提交
469 470 471 472 473 474

	/* Add subscriber to topology server's subscriber list */
	spin_lock_bh(&topsrv.lock);
	list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
	spin_unlock_bh(&topsrv.lock);

475 476 477
	/* Unlock server port */
	server_port_ref = subscriber->port_ref;
	spin_unlock_bh(subscriber->lock);
P
Per Liden 已提交
478

479
	/* Send an ACK- to complete connection handshaking */
480
	tipc_send(server_port_ref, 0, NULL, 0);
481 482 483 484 485 486

	/* Handle optional subscription request */
	if (size != 0) {
		subscr_conn_msg_event(subscriber, server_port_ref,
				      buf, data, size);
	}
P
Per Liden 已提交
487 488
}

489
int tipc_subscr_start(void)
P
Per Liden 已提交
490 491
{
	struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
492
	int res;
P
Per Liden 已提交
493

I
Ingo Molnar 已提交
494
	spin_lock_init(&topsrv.lock);
P
Per Liden 已提交
495 496
	INIT_LIST_HEAD(&topsrv.subscriber_list);

497
	res = tipc_createport(NULL,
498 499 500 501 502 503 504 505 506 507
			      TIPC_CRITICAL_IMPORTANCE,
			      NULL,
			      NULL,
			      NULL,
			      NULL,
			      subscr_named_msg_event,
			      NULL,
			      NULL,
			      &topsrv.setup_port);
	if (res)
P
Per Liden 已提交
508 509
		goto failed;

510
	res = tipc_publish(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
511 512 513
	if (res) {
		tipc_deleteport(topsrv.setup_port);
		topsrv.setup_port = 0;
P
Per Liden 已提交
514
		goto failed;
515
	}
P
Per Liden 已提交
516 517 518 519

	return 0;

failed:
520
	pr_err("Failed to create subscription service\n");
P
Per Liden 已提交
521 522 523
	return res;
}

524
void tipc_subscr_stop(void)
P
Per Liden 已提交
525
{
526 527
	struct tipc_subscriber *subscriber;
	struct tipc_subscriber *subscriber_temp;
P
Per Liden 已提交
528 529
	spinlock_t *subscriber_lock;

530
	if (topsrv.setup_port) {
P
Per Liden 已提交
531
		tipc_deleteport(topsrv.setup_port);
532 533
		topsrv.setup_port = 0;

534
		list_for_each_entry_safe(subscriber, subscriber_temp,
P
Per Liden 已提交
535 536 537
					 &topsrv.subscriber_list,
					 subscriber_list) {
			subscriber_lock = subscriber->lock;
538
			spin_lock_bh(subscriber_lock);
P
Per Liden 已提交
539 540 541 542 543
			subscr_terminate(subscriber);
			spin_unlock_bh(subscriber_lock);
		}
	}
}