blob: 23f43d03980cfd36d4ebb95ddb7dd3b685269f13 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Allan Stephens5b06c85c2008-05-19 13:30:13 -07002 * net/tipc/subscr.c: TIPC network topology service
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Allan Stephens5b06c85c2008-05-19 13:30:13 -07005 * Copyright (c) 2005-2007, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "name_table.h"
Allan Stephensd265fef2010-11-30 12:00:53 +000039#include "user_reg.h"
Allan Stephens5b06c85c2008-05-19 13:30:13 -070040#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42/**
43 * struct subscriber - TIPC network topology subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070044 * @port_ref: object reference to server port connecting to subscriber
45 * @lock: pointer to spinlock controlling access to subscriber's server port
Per Lidenb97bf3f2006-01-02 19:04:38 +010046 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
47 * @subscription_list: list of subscription objects for this subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010048 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090049
Per Lidenb97bf3f2006-01-02 19:04:38 +010050struct subscriber {
Allan Stephens28353e72008-05-19 13:29:47 -070051 u32 port_ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052 spinlock_t *lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010053 struct list_head subscriber_list;
54 struct list_head subscription_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055};
56
57/**
58 * struct top_srv - TIPC network topology subscription service
59 * @user_ref: TIPC userid of subscription service
60 * @setup_port: reference to TIPC port that handles subscription requests
61 * @subscription_count: number of active subscriptions (not subscribers!)
62 * @subscriber_list: list of ports subscribing to service
63 * @lock: spinlock govering access to subscriber list
64 */
65
66struct top_srv {
67 u32 user_ref;
68 u32 setup_port;
69 atomic_t subscription_count;
70 struct list_head subscriber_list;
71 spinlock_t lock;
72};
73
74static struct top_srv topsrv = { 0 };
75
76/**
Neil Hormandb5a7532010-10-21 01:06:16 +000077 * htohl - convert value to endianness used by destination
78 * @in: value to convert
79 * @swap: non-zero if endianness must be reversed
80 *
81 * Returns converted value
82 */
83
84static u32 htohl(u32 in, int swap)
85{
86 return swap ? swab32(in) : in;
87}
88
89/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010090 * subscr_send_event - send a message containing a tipc_event to the subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070091 *
92 * Note: Must not hold subscriber's server port lock, since tipc_send() will
93 * try to take the lock if the message is rejected and returned!
Per Lidenb97bf3f2006-01-02 19:04:38 +010094 */
95
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090096static void subscr_send_event(struct subscription *sub,
97 u32 found_lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +010098 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090099 u32 event,
100 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100101 u32 node)
102{
103 struct iovec msg_sect;
104
105 msg_sect.iov_base = (void *)&sub->evt;
106 msg_sect.iov_len = sizeof(struct tipc_event);
107
Neil Hormandb5a7532010-10-21 01:06:16 +0000108 sub->evt.event = htohl(event, sub->swap);
109 sub->evt.found_lower = htohl(found_lower, sub->swap);
110 sub->evt.found_upper = htohl(found_upper, sub->swap);
111 sub->evt.port.ref = htohl(port_ref, sub->swap);
112 sub->evt.port.node = htohl(node, sub->swap);
Allan Stephens28353e72008-05-19 13:29:47 -0700113 tipc_send(sub->server_ref, 1, &msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114}
115
116/**
Per Liden4323add2006-01-18 00:38:21 +0100117 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118 *
119 * Returns 1 if there is overlap, otherwise 0.
120 */
121
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900122int tipc_subscr_overlap(struct subscription *sub,
123 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100124 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100125
126{
127 if (found_lower < sub->seq.lower)
128 found_lower = sub->seq.lower;
129 if (found_upper > sub->seq.upper)
130 found_upper = sub->seq.upper;
131 if (found_lower > found_upper)
132 return 0;
133 return 1;
134}
135
136/**
Per Liden4323add2006-01-18 00:38:21 +0100137 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900138 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100139 * Protected by nameseq.lock in name_table.c
140 */
141
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900142void tipc_subscr_report_overlap(struct subscription *sub,
143 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100144 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900145 u32 event,
146 u32 port_ref,
Per Liden4323add2006-01-18 00:38:21 +0100147 u32 node,
148 int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100149{
Per Liden4323add2006-01-18 00:38:21 +0100150 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700152 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700154
155 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156}
157
158/**
159 * subscr_timeout - subscription timeout has occurred
160 */
161
162static void subscr_timeout(struct subscription *sub)
163{
Allan Stephens28353e72008-05-19 13:29:47 -0700164 struct port *server_port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100165
Allan Stephens28353e72008-05-19 13:29:47 -0700166 /* Validate server port reference (in case subscriber is terminating) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100167
Allan Stephens28353e72008-05-19 13:29:47 -0700168 server_port = tipc_port_lock(sub->server_ref);
169 if (server_port == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170 return;
171
Lijun Cheneb409462006-10-16 21:59:42 -0700172 /* Validate timeout (in case subscription is being cancelled) */
173
174 if (sub->timeout == TIPC_WAIT_FOREVER) {
Allan Stephens28353e72008-05-19 13:29:47 -0700175 tipc_port_unlock(server_port);
Lijun Cheneb409462006-10-16 21:59:42 -0700176 return;
177 }
178
Per Lidenb97bf3f2006-01-02 19:04:38 +0100179 /* Unlink subscription from name table */
180
Per Liden4323add2006-01-18 00:38:21 +0100181 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182
Allan Stephens28353e72008-05-19 13:29:47 -0700183 /* Unlink subscription from subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100184
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 list_del(&sub->subscription_list);
186
Allan Stephens28353e72008-05-19 13:29:47 -0700187 /* Release subscriber's server port */
188
189 tipc_port_unlock(server_port);
190
191 /* Notify subscriber of timeout */
192
193 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
194 TIPC_SUBSCR_TIMEOUT, 0, 0);
195
Per Lidenb97bf3f2006-01-02 19:04:38 +0100196 /* Now destroy subscription */
197
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 k_term_timer(&sub->timer);
199 kfree(sub);
200 atomic_dec(&topsrv.subscription_count);
201}
202
203/**
Lijun Cheneb409462006-10-16 21:59:42 -0700204 * subscr_del - delete a subscription within a subscription list
205 *
Allan Stephens28353e72008-05-19 13:29:47 -0700206 * Called with subscriber port locked.
Lijun Cheneb409462006-10-16 21:59:42 -0700207 */
208
209static void subscr_del(struct subscription *sub)
210{
211 tipc_nametbl_unsubscribe(sub);
212 list_del(&sub->subscription_list);
213 kfree(sub);
214 atomic_dec(&topsrv.subscription_count);
215}
216
217/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100218 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900219 *
Allan Stephens28353e72008-05-19 13:29:47 -0700220 * Called with subscriber port locked. Routine must temporarily release lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900221 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 * a new object reference in the interim that uses this lock; this routine will
225 * simply wait for it to be released, then claim it.)
226 */
227
228static void subscr_terminate(struct subscriber *subscriber)
229{
Allan Stephens28353e72008-05-19 13:29:47 -0700230 u32 port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100231 struct subscription *sub;
232 struct subscription *sub_temp;
233
234 /* Invalidate subscriber reference */
235
Allan Stephens28353e72008-05-19 13:29:47 -0700236 port_ref = subscriber->port_ref;
237 subscriber->port_ref = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 spin_unlock_bh(subscriber->lock);
239
Allan Stephens28353e72008-05-19 13:29:47 -0700240 /* Sever connection to subscriber */
241
242 tipc_shutdown(port_ref);
243 tipc_deleteport(port_ref);
244
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 /* Destroy any existing subscriptions for subscriber */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900246
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
248 subscription_list) {
249 if (sub->timeout != TIPC_WAIT_FOREVER) {
250 k_cancel_timer(&sub->timer);
251 k_term_timer(&sub->timer);
252 }
Lijun Cheneb409462006-10-16 21:59:42 -0700253 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
Lijun Cheneb409462006-10-16 21:59:42 -0700255 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100256 }
257
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 /* Remove subscriber from topology server's subscriber list */
259
260 spin_lock_bh(&topsrv.lock);
261 list_del(&subscriber->subscriber_list);
262 spin_unlock_bh(&topsrv.lock);
263
Allan Stephens28353e72008-05-19 13:29:47 -0700264 /* Reclaim subscriber lock */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100265
266 spin_lock_bh(subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700267
268 /* Now destroy subscriber */
269
Per Lidenb97bf3f2006-01-02 19:04:38 +0100270 kfree(subscriber);
271}
272
273/**
Lijun Cheneb409462006-10-16 21:59:42 -0700274 * subscr_cancel - handle subscription cancellation request
275 *
Allan Stephens28353e72008-05-19 13:29:47 -0700276 * Called with subscriber port locked. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700277 * to enable the subscription timeout routine to finish without deadlocking;
278 * the lock is then reclaimed to allow caller to release it upon return.
279 *
280 * Note that fields of 's' use subscriber's endianness!
281 */
282
283static void subscr_cancel(struct tipc_subscr *s,
284 struct subscriber *subscriber)
285{
286 struct subscription *sub;
287 struct subscription *sub_temp;
288 int found = 0;
289
290 /* Find first matching subscription, exit if not found */
291
292 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
293 subscription_list) {
Neil Hormandb5a7532010-10-21 01:06:16 +0000294 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
295 found = 1;
296 break;
297 }
Lijun Cheneb409462006-10-16 21:59:42 -0700298 }
299 if (!found)
300 return;
301
302 /* Cancel subscription timer (if used), then delete subscription */
303
304 if (sub->timeout != TIPC_WAIT_FOREVER) {
305 sub->timeout = TIPC_WAIT_FOREVER;
306 spin_unlock_bh(subscriber->lock);
307 k_cancel_timer(&sub->timer);
308 k_term_timer(&sub->timer);
309 spin_lock_bh(subscriber->lock);
310 }
Neil Horman8c974432010-10-21 01:06:15 +0000311 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
Lijun Cheneb409462006-10-16 21:59:42 -0700312 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
313 subscr_del(sub);
314}
315
316/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100317 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900318 *
Allan Stephens28353e72008-05-19 13:29:47 -0700319 * Called with subscriber port locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 */
321
Allan Stephens28353e72008-05-19 13:29:47 -0700322static struct subscription *subscr_subscribe(struct tipc_subscr *s,
323 struct subscriber *subscriber)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324{
325 struct subscription *sub;
Neil Hormandb5a7532010-10-21 01:06:16 +0000326 int swap;
327
328 /* Determine subscriber's endianness */
329
330 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700331
332 /* Detect & process a subscription cancellation request */
333
Neil Hormandb5a7532010-10-21 01:06:16 +0000334 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
335 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700336 subscr_cancel(s, subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700337 return NULL;
Lijun Cheneb409462006-10-16 21:59:42 -0700338 }
339
Per Lidenb97bf3f2006-01-02 19:04:38 +0100340 /* Refuse subscription if global limit exceeded */
341
342 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700343 warn("Subscription rejected, subscription limit reached (%u)\n",
344 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100345 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700346 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100347 }
348
349 /* Allocate subscription object */
350
Allan Stephens28353e72008-05-19 13:29:47 -0700351 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700352 if (!sub) {
353 warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100354 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700355 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356 }
357
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 /* Initialize subscription object */
359
Neil Hormandb5a7532010-10-21 01:06:16 +0000360 sub->seq.type = htohl(s->seq.type, swap);
361 sub->seq.lower = htohl(s->seq.lower, swap);
362 sub->seq.upper = htohl(s->seq.upper, swap);
363 sub->timeout = htohl(s->timeout, swap);
364 sub->filter = htohl(s->filter, swap);
Neil Horman8c974432010-10-21 01:06:15 +0000365 if ((!(sub->filter & TIPC_SUB_PORTS) ==
366 !(sub->filter & TIPC_SUB_SERVICE)) ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800367 (sub->seq.lower > sub->seq.upper)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700368 warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100369 kfree(sub);
370 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700371 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 }
Allan Stephense15f8802008-05-19 13:27:31 -0700373 sub->event_cb = subscr_send_event;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100374 INIT_LIST_HEAD(&sub->nameseq_list);
375 list_add(&sub->subscription_list, &subscriber->subscription_list);
Allan Stephens28353e72008-05-19 13:29:47 -0700376 sub->server_ref = subscriber->port_ref;
Neil Hormandb5a7532010-10-21 01:06:16 +0000377 sub->swap = swap;
Allan Stephens28353e72008-05-19 13:29:47 -0700378 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379 atomic_inc(&topsrv.subscription_count);
380 if (sub->timeout != TIPC_WAIT_FOREVER) {
381 k_init_timer(&sub->timer,
382 (Handler)subscr_timeout, (unsigned long)sub);
383 k_start_timer(&sub->timer, sub->timeout);
384 }
Allan Stephens28353e72008-05-19 13:29:47 -0700385
386 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100387}
388
389/**
390 * subscr_conn_shutdown_event - handle termination request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700391 *
392 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100393 */
394
395static void subscr_conn_shutdown_event(void *usr_handle,
Allan Stephens28353e72008-05-19 13:29:47 -0700396 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 struct sk_buff **buf,
398 unsigned char const *data,
399 unsigned int size,
400 int reason)
401{
Allan Stephens28353e72008-05-19 13:29:47 -0700402 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 spinlock_t *subscriber_lock;
404
Allan Stephens28353e72008-05-19 13:29:47 -0700405 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 return;
407
408 subscriber_lock = subscriber->lock;
409 subscr_terminate(subscriber);
410 spin_unlock_bh(subscriber_lock);
411}
412
413/**
414 * subscr_conn_msg_event - handle new subscription request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700415 *
416 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417 */
418
419static void subscr_conn_msg_event(void *usr_handle,
420 u32 port_ref,
421 struct sk_buff **buf,
422 const unchar *data,
423 u32 size)
424{
Allan Stephens28353e72008-05-19 13:29:47 -0700425 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100426 spinlock_t *subscriber_lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700427 struct subscription *sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428
Allan Stephens28353e72008-05-19 13:29:47 -0700429 /*
430 * Lock subscriber's server port (& make a local copy of lock pointer,
431 * in case subscriber is deleted while processing subscription request)
432 */
433
434 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100435 return;
436
437 subscriber_lock = subscriber->lock;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900438
Allan Stephens28353e72008-05-19 13:29:47 -0700439 if (size != sizeof(struct tipc_subscr)) {
440 subscr_terminate(subscriber);
441 spin_unlock_bh(subscriber_lock);
442 } else {
443 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
444 spin_unlock_bh(subscriber_lock);
445 if (sub != NULL) {
446
447 /*
448 * We must release the server port lock before adding a
449 * subscription to the name table since TIPC needs to be
450 * able to (re)acquire the port lock if an event message
451 * issued by the subscription process is rejected and
452 * returned. The subscription cannot be deleted while
453 * it is being added to the name table because:
454 * a) the single-threading of the native API port code
455 * ensures the subscription cannot be cancelled and
456 * the subscriber connection cannot be broken, and
457 * b) the name table lock ensures the subscription
458 * timeout code cannot delete the subscription,
459 * so the subscription object is still protected.
460 */
461
462 tipc_nametbl_subscribe(sub);
463 }
464 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100465}
466
467/**
468 * subscr_named_msg_event - handle request to establish a new subscriber
469 */
470
471static void subscr_named_msg_event(void *usr_handle,
472 u32 port_ref,
473 struct sk_buff **buf,
474 const unchar *data,
475 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900476 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 struct tipc_portid const *orig,
478 struct tipc_name_seq const *dest)
479{
Allan Stephens28353e72008-05-19 13:29:47 -0700480 static struct iovec msg_sect = {NULL, 0};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481
Allan Stephens28353e72008-05-19 13:29:47 -0700482 struct subscriber *subscriber;
483 u32 server_port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100484
485 /* Create subscriber object */
486
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700487 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 if (subscriber == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700489 warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490 return;
491 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 INIT_LIST_HEAD(&subscriber->subscription_list);
493 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494
Allan Stephens28353e72008-05-19 13:29:47 -0700495 /* Create server port & establish connection to subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496
497 tipc_createport(topsrv.user_ref,
Allan Stephens28353e72008-05-19 13:29:47 -0700498 subscriber,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800500 NULL,
501 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100502 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800503 NULL,
504 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100505 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800506 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507 &subscriber->port_ref);
508 if (subscriber->port_ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700509 warn("Subscriber rejected, unable to create port\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 kfree(subscriber);
511 return;
512 }
513 tipc_connect2port(subscriber->port_ref, orig);
514
Allan Stephens28353e72008-05-19 13:29:47 -0700515 /* Lock server port (& save lock address for future use) */
516
517 subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518
519 /* Add subscriber to topology server's subscriber list */
520
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 spin_lock_bh(&topsrv.lock);
522 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
523 spin_unlock_bh(&topsrv.lock);
524
Allan Stephens28353e72008-05-19 13:29:47 -0700525 /* Unlock server port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526
Allan Stephens28353e72008-05-19 13:29:47 -0700527 server_port_ref = subscriber->port_ref;
528 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529
Allan Stephens28353e72008-05-19 13:29:47 -0700530 /* Send an ACK- to complete connection handshaking */
531
532 tipc_send(server_port_ref, 1, &msg_sect);
533
534 /* Handle optional subscription request */
535
536 if (size != 0) {
537 subscr_conn_msg_event(subscriber, server_port_ref,
538 buf, data, size);
539 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100540}
541
Per Liden4323add2006-01-18 00:38:21 +0100542int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100543{
544 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
Allan Stephens52fe7b72010-11-30 12:01:00 +0000545 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100546
547 memset(&topsrv, 0, sizeof (topsrv));
Ingo Molnar34af9462006-06-27 02:53:55 -0700548 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100549 INIT_LIST_HEAD(&topsrv.subscriber_list);
550
551 spin_lock_bh(&topsrv.lock);
Allan Stephensa5c2af92010-11-30 12:00:58 +0000552 res = tipc_attach(&topsrv.user_ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 if (res) {
554 spin_unlock_bh(&topsrv.lock);
555 return res;
556 }
557
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900558 res = tipc_createport(topsrv.user_ref,
559 NULL,
560 TIPC_CRITICAL_IMPORTANCE,
561 NULL,
562 NULL,
563 NULL,
564 NULL,
565 subscr_named_msg_event,
566 NULL,
567 NULL,
568 &topsrv.setup_port);
569 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 goto failed;
571
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
573 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 goto failed;
575
576 spin_unlock_bh(&topsrv.lock);
577 return 0;
578
579failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100580 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 tipc_detach(topsrv.user_ref);
582 topsrv.user_ref = 0;
583 spin_unlock_bh(&topsrv.lock);
584 return res;
585}
586
Per Liden4323add2006-01-18 00:38:21 +0100587void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100588{
589 struct subscriber *subscriber;
590 struct subscriber *subscriber_temp;
591 spinlock_t *subscriber_lock;
592
593 if (topsrv.user_ref) {
594 tipc_deleteport(topsrv.setup_port);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900595 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596 &topsrv.subscriber_list,
597 subscriber_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100598 subscriber_lock = subscriber->lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700599 spin_lock_bh(subscriber_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 subscr_terminate(subscriber);
601 spin_unlock_bh(subscriber_lock);
602 }
603 tipc_detach(topsrv.user_ref);
604 topsrv.user_ref = 0;
605 }
606}