subscr.c 11.4 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-2013, 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
 * @conid: connection identifier to server connecting to subscriber
 * @lock: controll access to subscriber
P
Per Liden 已提交
46 47
 * @subscription_list: list of subscription objects for this subscriber
 */
48
struct tipc_subscriber {
49 50
	int conid;
	spinlock_t lock;
P
Per Liden 已提交
51 52 53
	struct list_head subscription_list;
};

54 55 56 57 58 59 60 61 62 63 64 65 66 67
static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
				  void *usr_data, void *buf, size_t len);
static void *subscr_named_msg_event(int conid);
static void subscr_conn_shutdown_event(int conid, void *usr_data);

static atomic_t subscription_count = ATOMIC_INIT(0);

static struct sockaddr_tipc topsrv_addr __read_mostly = {
	.family			= AF_TIPC,
	.addrtype		= TIPC_ADDR_NAMESEQ,
	.addr.nameseq.type	= TIPC_TOP_SRV,
	.addr.nameseq.lower	= TIPC_TOP_SRV,
	.addr.nameseq.upper	= TIPC_TOP_SRV,
	.scope			= TIPC_NODE_SCOPE
P
Per Liden 已提交
68 69
};

70 71 72 73 74 75 76 77 78 79
static struct tipc_server topsrv __read_mostly = {
	.saddr			= &topsrv_addr,
	.imp			= TIPC_CRITICAL_IMPORTANCE,
	.type			= SOCK_SEQPACKET,
	.max_rcvbuf_size	= sizeof(struct tipc_subscr),
	.name			= "topology_server",
	.tipc_conn_recvmsg	= subscr_conn_msg_event,
	.tipc_conn_new		= subscr_named_msg_event,
	.tipc_conn_shutdown	= subscr_conn_shutdown_event,
};
P
Per Liden 已提交
80

81 82 83 84 85 86 87 88 89 90 91 92
/**
 * 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;
}

93 94
static void subscr_send_event(struct tipc_subscription *sub, u32 found_lower,
			      u32 found_upper, u32 event, u32 port_ref,
P
Per Liden 已提交
95 96
			      u32 node)
{
97 98 99
	struct tipc_subscriber *subscriber = sub->subscriber;
	struct kvec msg_sect;
	int ret;
P
Per Liden 已提交
100 101 102 103

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

104 105 106 107 108
	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);
109 110 111 112
	ret = tipc_conn_sendmsg(&topsrv, subscriber->conid, NULL,
				msg_sect.iov_base, msg_sect.iov_len);
	if (ret < 0)
		pr_err("Sending subscription event failed, no memory\n");
P
Per Liden 已提交
113 114 115
}

/**
116
 * tipc_subscr_overlap - test for subscription overlap with the given values
P
Per Liden 已提交
117 118 119
 *
 * Returns 1 if there is overlap, otherwise 0.
 */
120
int tipc_subscr_overlap(struct tipc_subscription *sub, u32 found_lower,
121
			u32 found_upper)
P
Per Liden 已提交
122 123 124 125 126 127 128 129 130 131 132
{
	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;
}

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

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

149
static void subscr_timeout(struct tipc_subscription *sub)
P
Per Liden 已提交
150
{
151
	struct tipc_subscriber *subscriber = sub->subscriber;
P
Per Liden 已提交
152

153 154 155 156 157 158 159 160
	/* The spin lock per subscriber is used to protect its members */
	spin_lock_bh(&subscriber->lock);

	/* Validate if the connection related to the subscriber is
	 * closed (in case subscriber is terminating)
	 */
	if (subscriber->conid == 0) {
		spin_unlock_bh(&subscriber->lock);
P
Per Liden 已提交
161
		return;
162
	}
P
Per Liden 已提交
163

164 165
	/* Validate timeout (in case subscription is being cancelled) */
	if (sub->timeout == TIPC_WAIT_FOREVER) {
166
		spin_unlock_bh(&subscriber->lock);
167 168 169
		return;
	}

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

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

176
	spin_unlock_bh(&subscriber->lock);
177 178 179 180 181

	/* 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 已提交
182 183 184
	/* Now destroy subscription */
	k_term_timer(&sub->timer);
	kfree(sub);
185
	atomic_dec(&subscription_count);
P
Per Liden 已提交
186 187
}

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

P
Per Liden 已提交
201 202
/**
 * subscr_terminate - terminate communication with a subscriber
203
 *
204
 * Note: Must call it in process context since it might sleep.
P
Per Liden 已提交
205
 */
206
static void subscr_terminate(struct tipc_subscriber *subscriber)
P
Per Liden 已提交
207
{
208 209 210 211 212
	tipc_conn_terminate(&topsrv, subscriber->conid);
}

static void subscr_release(struct tipc_subscriber *subscriber)
{
213 214
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
P
Per Liden 已提交
215

216
	spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
217

218 219
	/* Invalidate subscriber reference */
	subscriber->conid = 0;
220

P
Per Liden 已提交
221 222 223 224
	/* 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) {
225
			spin_unlock_bh(&subscriber->lock);
P
Per Liden 已提交
226 227
			k_cancel_timer(&sub->timer);
			k_term_timer(&sub->timer);
228
			spin_lock_bh(&subscriber->lock);
P
Per Liden 已提交
229
		}
230
		subscr_del(sub);
P
Per Liden 已提交
231
	}
232
	spin_unlock_bh(&subscriber->lock);
233 234

	/* Now destroy subscriber */
P
Per Liden 已提交
235 236 237
	kfree(subscriber);
}

238 239 240
/**
 * subscr_cancel - handle subscription cancellation request
 *
241
 * Called with subscriber lock held. Routine must temporarily release lock
242 243 244 245 246 247
 * 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,
248
			  struct tipc_subscriber *subscriber)
249
{
250 251
	struct tipc_subscription *sub;
	struct tipc_subscription *sub_temp;
252 253 254 255 256
	int found = 0;

	/* Find first matching subscription, exit if not found */
	list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
				 subscription_list) {
257 258 259 260
		if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
			found = 1;
			break;
		}
261 262 263 264 265 266 267
	}
	if (!found)
		return;

	/* Cancel subscription timer (if used), then delete subscription */
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		sub->timeout = TIPC_WAIT_FOREVER;
268
		spin_unlock_bh(&subscriber->lock);
269 270
		k_cancel_timer(&sub->timer);
		k_term_timer(&sub->timer);
271
		spin_lock_bh(&subscriber->lock);
272 273 274 275
	}
	subscr_del(sub);
}

P
Per Liden 已提交
276 277
/**
 * subscr_subscribe - create subscription for subscriber
278
 *
279
 * Called with subscriber lock held.
P
Per Liden 已提交
280
 */
281
static struct tipc_subscription *subscr_subscribe(struct tipc_subscr *s,
282
					     struct tipc_subscriber *subscriber)
P
Per Liden 已提交
283
{
284
	struct tipc_subscription *sub;
285 286 287 288
	int swap;

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

	/* Detect & process a subscription cancellation request */
291 292
	if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
		s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
293
		subscr_cancel(s, subscriber);
294
		return NULL;
295 296
	}

P
Per Liden 已提交
297
	/* Refuse subscription if global limit exceeded */
298
	if (atomic_read(&subscription_count) >= TIPC_MAX_SUBSCRIPTIONS) {
299
		pr_warn("Subscription rejected, limit reached (%u)\n",
300
			TIPC_MAX_SUBSCRIPTIONS);
P
Per Liden 已提交
301
		subscr_terminate(subscriber);
302
		return NULL;
P
Per Liden 已提交
303 304 305
	}

	/* Allocate subscription object */
306
	sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
307
	if (!sub) {
308
		pr_warn("Subscription rejected, no memory\n");
P
Per Liden 已提交
309
		subscr_terminate(subscriber);
310
		return NULL;
P
Per Liden 已提交
311 312 313
	}

	/* Initialize subscription object */
314 315 316 317 318
	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);
319 320
	if ((!(sub->filter & TIPC_SUB_PORTS) ==
	     !(sub->filter & TIPC_SUB_SERVICE)) ||
321
	    (sub->seq.lower > sub->seq.upper)) {
322
		pr_warn("Subscription rejected, illegal request\n");
P
Per Liden 已提交
323 324
		kfree(sub);
		subscr_terminate(subscriber);
325
		return NULL;
P
Per Liden 已提交
326 327 328
	}
	INIT_LIST_HEAD(&sub->nameseq_list);
	list_add(&sub->subscription_list, &subscriber->subscription_list);
329
	sub->subscriber = subscriber;
330
	sub->swap = swap;
331
	memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
332
	atomic_inc(&subscription_count);
P
Per Liden 已提交
333 334 335 336 337
	if (sub->timeout != TIPC_WAIT_FOREVER) {
		k_init_timer(&sub->timer,
			     (Handler)subscr_timeout, (unsigned long)sub);
		k_start_timer(&sub->timer, sub->timeout);
	}
338 339

	return sub;
P
Per Liden 已提交
340 341
}

342 343
/* Handle one termination request for the subscriber */
static void subscr_conn_shutdown_event(int conid, void *usr_data)
P
Per Liden 已提交
344
{
345
	subscr_release((struct tipc_subscriber *)usr_data);
P
Per Liden 已提交
346 347
}

348 349 350
/* Handle one request to create a new subscription for the subscriber */
static void subscr_conn_msg_event(int conid, struct sockaddr_tipc *addr,
				  void *usr_data, void *buf, size_t len)
P
Per Liden 已提交
351
{
352
	struct tipc_subscriber *subscriber = usr_data;
353
	struct tipc_subscription *sub;
354

355 356 357 358 359
	spin_lock_bh(&subscriber->lock);
	sub = subscr_subscribe((struct tipc_subscr *)buf, subscriber);
	if (sub)
		tipc_nametbl_subscribe(sub);
	spin_unlock_bh(&subscriber->lock);
P
Per Liden 已提交
360 361
}

362 363 364

/* Handle one request to establish a new subscriber */
static void *subscr_named_msg_event(int conid)
P
Per Liden 已提交
365
{
366
	struct tipc_subscriber *subscriber;
P
Per Liden 已提交
367 368

	/* Create subscriber object */
369
	subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
P
Per Liden 已提交
370
	if (subscriber == NULL) {
371
		pr_warn("Subscriber rejected, no memory\n");
372
		return NULL;
P
Per Liden 已提交
373 374
	}
	INIT_LIST_HEAD(&subscriber->subscription_list);
375 376
	subscriber->conid = conid;
	spin_lock_init(&subscriber->lock);
377

378
	return (void *)subscriber;
P
Per Liden 已提交
379 380
}

381
int tipc_subscr_start(void)
P
Per Liden 已提交
382
{
383
	return tipc_server_start(&topsrv);
P
Per Liden 已提交
384 385
}

386
void tipc_subscr_stop(void)
P
Per Liden 已提交
387
{
388
	tipc_server_stop(&topsrv);
P
Per Liden 已提交
389
}