blob: dde23f1e7542da377954c4d4a027364c6b6626ab [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/subscr.c: TIPC subscription service
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 1. Redistributions of source code must retain the above copyright
12 * notice, this list of conditions and the following disclaimer.
13 * 2. Redistributions in binary form must reproduce the above copyright
14 * notice, this list of conditions and the following disclaimer in the
15 * documentation and/or other materials provided with the distribution.
16 * 3. Neither the names of the copyright holders nor the names of its
17 * contributors may be used to endorse or promote products derived from
18 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * Alternatively, this software may be distributed under the terms of the
21 * GNU General Public License ("GPL") version 2 as published by the Free
22 * Software Foundation.
23 *
24 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
25 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
26 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
27 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
28 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
29 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
30 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
31 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
32 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
33 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "subscr.h"
40#include "name_table.h"
41#include "ref.h"
42
43/**
44 * struct subscriber - TIPC network topology subscriber
45 * @ref: object reference to subscriber object itself
46 * @lock: pointer to spinlock controlling access to subscriber object
47 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
48 * @subscription_list: list of subscription objects for this subscriber
49 * @port_ref: object reference to port used to communicate with subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010050 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090051
Per Lidenb97bf3f2006-01-02 19:04:38 +010052struct subscriber {
53 u32 ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090054 spinlock_t *lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055 struct list_head subscriber_list;
56 struct list_head subscription_list;
57 u32 port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +010058};
59
60/**
61 * struct top_srv - TIPC network topology subscription service
62 * @user_ref: TIPC userid of subscription service
63 * @setup_port: reference to TIPC port that handles subscription requests
64 * @subscription_count: number of active subscriptions (not subscribers!)
65 * @subscriber_list: list of ports subscribing to service
66 * @lock: spinlock govering access to subscriber list
67 */
68
69struct top_srv {
70 u32 user_ref;
71 u32 setup_port;
72 atomic_t subscription_count;
73 struct list_head subscriber_list;
74 spinlock_t lock;
75};
76
77static struct top_srv topsrv = { 0 };
78
79/**
80 * htohl - convert value to endianness used by destination
81 * @in: value to convert
82 * @swap: non-zero if endianness must be reversed
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090083 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010084 * Returns converted value
85 */
86
Sam Ravnborg05790c62006-03-20 22:37:04 -080087static u32 htohl(u32 in, int swap)
Per Lidenb97bf3f2006-01-02 19:04:38 +010088{
Allan Stephensfc5ad582008-05-19 13:29:06 -070089 return swap ? (u32)___constant_swab32(in) : in;
Per Lidenb97bf3f2006-01-02 19:04:38 +010090}
91
92/**
93 * subscr_send_event - send a message containing a tipc_event to the subscriber
94 */
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
Allan Stephens8e9501f2008-05-19 13:28:32 -0700108 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);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100113 tipc_send(sub->owner->port_ref, 1, &msg_sect);
114}
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{
150 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
151 sub->seq.upper, found_lower, found_upper);
Per Liden4323add2006-01-18 00:38:21 +0100152 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700154 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700156
157 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100158}
159
160/**
161 * subscr_timeout - subscription timeout has occurred
162 */
163
164static void subscr_timeout(struct subscription *sub)
165{
166 struct subscriber *subscriber;
167 u32 subscriber_ref;
168
169 /* Validate subscriber reference (in case subscriber is terminating) */
170
171 subscriber_ref = sub->owner->ref;
Per Liden4323add2006-01-18 00:38:21 +0100172 subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173 if (subscriber == NULL)
174 return;
175
Lijun Cheneb409462006-10-16 21:59:42 -0700176 /* Validate timeout (in case subscription is being cancelled) */
177
178 if (sub->timeout == TIPC_WAIT_FOREVER) {
179 tipc_ref_unlock(subscriber_ref);
180 return;
181 }
182
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 /* Unlink subscription from name table */
184
Per Liden4323add2006-01-18 00:38:21 +0100185 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186
187 /* Notify subscriber of timeout, then unlink subscription */
188
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900189 subscr_send_event(sub,
190 sub->evt.s.seq.lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100191 sub->evt.s.seq.upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900192 TIPC_SUBSCR_TIMEOUT,
193 0,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100194 0);
195 list_del(&sub->subscription_list);
196
197 /* Now destroy subscription */
198
Per Liden4323add2006-01-18 00:38:21 +0100199 tipc_ref_unlock(subscriber_ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 k_term_timer(&sub->timer);
201 kfree(sub);
202 atomic_dec(&topsrv.subscription_count);
203}
204
205/**
Lijun Cheneb409462006-10-16 21:59:42 -0700206 * subscr_del - delete a subscription within a subscription list
207 *
208 * Called with subscriber locked.
209 */
210
211static void subscr_del(struct subscription *sub)
212{
213 tipc_nametbl_unsubscribe(sub);
214 list_del(&sub->subscription_list);
215 kfree(sub);
216 atomic_dec(&topsrv.subscription_count);
217}
218
219/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900221 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 * Called with subscriber locked. Routine must temporarily release this lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900225 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 * a new object reference in the interim that uses this lock; this routine will
227 * simply wait for it to be released, then claim it.)
228 */
229
230static void subscr_terminate(struct subscriber *subscriber)
231{
232 struct subscription *sub;
233 struct subscription *sub_temp;
234
235 /* Invalidate subscriber reference */
236
Per Liden4323add2006-01-18 00:38:21 +0100237 tipc_ref_discard(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100238 spin_unlock_bh(subscriber->lock);
239
240 /* Destroy any existing subscriptions for subscriber */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900241
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
243 subscription_list) {
244 if (sub->timeout != TIPC_WAIT_FOREVER) {
245 k_cancel_timer(&sub->timer);
246 k_term_timer(&sub->timer);
247 }
Lijun Cheneb409462006-10-16 21:59:42 -0700248 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
Lijun Cheneb409462006-10-16 21:59:42 -0700250 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 }
252
253 /* Sever connection to subscriber */
254
255 tipc_shutdown(subscriber->port_ref);
256 tipc_deleteport(subscriber->port_ref);
257
258 /* 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
264 /* Now destroy subscriber */
265
266 spin_lock_bh(subscriber->lock);
267 kfree(subscriber);
268}
269
270/**
Lijun Cheneb409462006-10-16 21:59:42 -0700271 * subscr_cancel - handle subscription cancellation request
272 *
273 * Called with subscriber locked. Routine must temporarily release this lock
274 * to enable the subscription timeout routine to finish without deadlocking;
275 * the lock is then reclaimed to allow caller to release it upon return.
276 *
277 * Note that fields of 's' use subscriber's endianness!
278 */
279
280static void subscr_cancel(struct tipc_subscr *s,
281 struct subscriber *subscriber)
282{
283 struct subscription *sub;
284 struct subscription *sub_temp;
285 int found = 0;
286
287 /* Find first matching subscription, exit if not found */
288
289 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
290 subscription_list) {
291 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
292 found = 1;
293 break;
294 }
295 }
296 if (!found)
297 return;
298
299 /* Cancel subscription timer (if used), then delete subscription */
300
301 if (sub->timeout != TIPC_WAIT_FOREVER) {
302 sub->timeout = TIPC_WAIT_FOREVER;
303 spin_unlock_bh(subscriber->lock);
304 k_cancel_timer(&sub->timer);
305 k_term_timer(&sub->timer);
306 spin_lock_bh(subscriber->lock);
307 }
308 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
309 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
310 subscr_del(sub);
311}
312
313/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100314 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900315 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 * Called with subscriber locked
317 */
318
319static void subscr_subscribe(struct tipc_subscr *s,
320 struct subscriber *subscriber)
321{
322 struct subscription *sub;
Allan Stephens8e9501f2008-05-19 13:28:32 -0700323 int swap;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324
Allan Stephens8e9501f2008-05-19 13:28:32 -0700325 /* Determine subscriber's endianness */
Lijun Cheneb409462006-10-16 21:59:42 -0700326
Allan Stephens8e9501f2008-05-19 13:28:32 -0700327 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700328
329 /* Detect & process a subscription cancellation request */
330
Allan Stephens8e9501f2008-05-19 13:28:32 -0700331 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
332 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700333 subscr_cancel(s, subscriber);
334 return;
335 }
336
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 /* Refuse subscription if global limit exceeded */
338
339 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700340 warn("Subscription rejected, subscription limit reached (%u)\n",
341 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100342 subscr_terminate(subscriber);
343 return;
344 }
345
346 /* Allocate subscription object */
347
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -0200348 sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700349 if (!sub) {
350 warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 subscr_terminate(subscriber);
352 return;
353 }
354
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 /* Initialize subscription object */
356
Allan Stephens8e9501f2008-05-19 13:28:32 -0700357 sub->seq.type = htohl(s->seq.type, swap);
358 sub->seq.lower = htohl(s->seq.lower, swap);
359 sub->seq.upper = htohl(s->seq.upper, swap);
360 sub->timeout = htohl(s->timeout, swap);
361 sub->filter = htohl(s->filter, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700362 if ((!(sub->filter & TIPC_SUB_PORTS)
363 == !(sub->filter & TIPC_SUB_SERVICE))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 || (sub->seq.lower > sub->seq.upper)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700365 warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100366 kfree(sub);
367 subscr_terminate(subscriber);
368 return;
369 }
Allan Stephense15f8802008-05-19 13:27:31 -0700370 sub->event_cb = subscr_send_event;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
372 INIT_LIST_HEAD(&sub->subscription_list);
373 INIT_LIST_HEAD(&sub->nameseq_list);
374 list_add(&sub->subscription_list, &subscriber->subscription_list);
Allan Stephens8e9501f2008-05-19 13:28:32 -0700375 sub->swap = swap;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 atomic_inc(&topsrv.subscription_count);
377 if (sub->timeout != TIPC_WAIT_FOREVER) {
378 k_init_timer(&sub->timer,
379 (Handler)subscr_timeout, (unsigned long)sub);
380 k_start_timer(&sub->timer, sub->timeout);
381 }
382 sub->owner = subscriber;
Per Liden4323add2006-01-18 00:38:21 +0100383 tipc_nametbl_subscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100384}
385
386/**
387 * subscr_conn_shutdown_event - handle termination request from subscriber
388 */
389
390static void subscr_conn_shutdown_event(void *usr_handle,
391 u32 portref,
392 struct sk_buff **buf,
393 unsigned char const *data,
394 unsigned int size,
395 int reason)
396{
David S. Miller880b0052006-01-12 13:22:32 -0800397 struct subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100398 spinlock_t *subscriber_lock;
399
Per Liden4323add2006-01-18 00:38:21 +0100400 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 if (subscriber == NULL)
402 return;
403
404 subscriber_lock = subscriber->lock;
405 subscr_terminate(subscriber);
406 spin_unlock_bh(subscriber_lock);
407}
408
409/**
410 * subscr_conn_msg_event - handle new subscription request from subscriber
411 */
412
413static void subscr_conn_msg_event(void *usr_handle,
414 u32 port_ref,
415 struct sk_buff **buf,
416 const unchar *data,
417 u32 size)
418{
David S. Miller880b0052006-01-12 13:22:32 -0800419 struct subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100420 spinlock_t *subscriber_lock;
421
Per Liden4323add2006-01-18 00:38:21 +0100422 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423 if (subscriber == NULL)
424 return;
425
426 subscriber_lock = subscriber->lock;
427 if (size != sizeof(struct tipc_subscr))
428 subscr_terminate(subscriber);
429 else
430 subscr_subscribe((struct tipc_subscr *)data, subscriber);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900431
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432 spin_unlock_bh(subscriber_lock);
433}
434
435/**
436 * subscr_named_msg_event - handle request to establish a new subscriber
437 */
438
439static void subscr_named_msg_event(void *usr_handle,
440 u32 port_ref,
441 struct sk_buff **buf,
442 const unchar *data,
443 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900444 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100445 struct tipc_portid const *orig,
446 struct tipc_name_seq const *dest)
447{
448 struct subscriber *subscriber;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800449 struct iovec msg_sect = {NULL, 0};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 spinlock_t *subscriber_lock;
451
452 dbg("subscr_named_msg_event: orig = %x own = %x,\n",
453 orig->node, tipc_own_addr);
454 if (size && (size != sizeof(struct tipc_subscr))) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700455 warn("Subscriber rejected, invalid subscription size\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100456 return;
457 }
458
459 /* Create subscriber object */
460
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700461 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 if (subscriber == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700463 warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100464 return;
465 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 INIT_LIST_HEAD(&subscriber->subscription_list);
467 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Liden4323add2006-01-18 00:38:21 +0100468 subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 if (subscriber->ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700470 warn("Subscriber rejected, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 kfree(subscriber);
472 return;
473 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700474 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100475
476 /* Establish a connection to subscriber */
477
478 tipc_createport(topsrv.user_ref,
David S. Miller880b0052006-01-12 13:22:32 -0800479 (void *)(unsigned long)subscriber->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800481 NULL,
482 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800484 NULL,
485 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100486 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800487 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 &subscriber->port_ref);
489 if (subscriber->port_ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700490 warn("Subscriber rejected, unable to create port\n");
Per Liden4323add2006-01-18 00:38:21 +0100491 tipc_ref_discard(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 kfree(subscriber);
493 return;
494 }
495 tipc_connect2port(subscriber->port_ref, orig);
496
497
498 /* Add subscriber to topology server's subscriber list */
499
Per Liden4323add2006-01-18 00:38:21 +0100500 tipc_ref_lock(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100501 spin_lock_bh(&topsrv.lock);
502 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
503 spin_unlock_bh(&topsrv.lock);
504
505 /*
506 * Subscribe now if message contains a subscription,
507 * otherwise send an empty response to complete connection handshaking
508 */
509
510 subscriber_lock = subscriber->lock;
511 if (size)
512 subscr_subscribe((struct tipc_subscr *)data, subscriber);
513 else
514 tipc_send(subscriber->port_ref, 1, &msg_sect);
515
516 spin_unlock_bh(subscriber_lock);
517}
518
Per Liden4323add2006-01-18 00:38:21 +0100519int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100520{
521 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
522 int res = -1;
523
524 memset(&topsrv, 0, sizeof (topsrv));
Ingo Molnar34af9462006-06-27 02:53:55 -0700525 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100526 INIT_LIST_HEAD(&topsrv.subscriber_list);
527
528 spin_lock_bh(&topsrv.lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800529 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530 if (res) {
531 spin_unlock_bh(&topsrv.lock);
532 return res;
533 }
534
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900535 res = tipc_createport(topsrv.user_ref,
536 NULL,
537 TIPC_CRITICAL_IMPORTANCE,
538 NULL,
539 NULL,
540 NULL,
541 NULL,
542 subscr_named_msg_event,
543 NULL,
544 NULL,
545 &topsrv.setup_port);
546 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547 goto failed;
548
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900549 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
550 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100551 goto failed;
552
553 spin_unlock_bh(&topsrv.lock);
554 return 0;
555
556failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100557 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100558 tipc_detach(topsrv.user_ref);
559 topsrv.user_ref = 0;
560 spin_unlock_bh(&topsrv.lock);
561 return res;
562}
563
Per Liden4323add2006-01-18 00:38:21 +0100564void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100565{
566 struct subscriber *subscriber;
567 struct subscriber *subscriber_temp;
568 spinlock_t *subscriber_lock;
569
570 if (topsrv.user_ref) {
571 tipc_deleteport(topsrv.setup_port);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900572 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100573 &topsrv.subscriber_list,
574 subscriber_list) {
Per Liden4323add2006-01-18 00:38:21 +0100575 tipc_ref_lock(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100576 subscriber_lock = subscriber->lock;
577 subscr_terminate(subscriber);
578 spin_unlock_bh(subscriber_lock);
579 }
580 tipc_detach(topsrv.user_ref);
581 topsrv.user_ref = 0;
582 }
583}
584
585
586int tipc_ispublished(struct tipc_name const *name)
587{
588 u32 domain = 0;
589
Per Liden4323add2006-01-18 00:38:21 +0100590 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100591}
592