blob: 81e2bd5f2413819da3d2b0c84cb5627e87ceadc3 [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
50 * @swap: indicates if subscriber uses opposite endianness in its messages
51 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090052
Per Lidenb97bf3f2006-01-02 19:04:38 +010053struct subscriber {
54 u32 ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090055 spinlock_t *lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010056 struct list_head subscriber_list;
57 struct list_head subscription_list;
58 u32 port_ref;
59 int swap;
60};
61
62/**
63 * struct top_srv - TIPC network topology subscription service
64 * @user_ref: TIPC userid of subscription service
65 * @setup_port: reference to TIPC port that handles subscription requests
66 * @subscription_count: number of active subscriptions (not subscribers!)
67 * @subscriber_list: list of ports subscribing to service
68 * @lock: spinlock govering access to subscriber list
69 */
70
71struct top_srv {
72 u32 user_ref;
73 u32 setup_port;
74 atomic_t subscription_count;
75 struct list_head subscriber_list;
76 spinlock_t lock;
77};
78
79static struct top_srv topsrv = { 0 };
80
81/**
82 * htohl - convert value to endianness used by destination
83 * @in: value to convert
84 * @swap: non-zero if endianness must be reversed
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010086 * Returns converted value
87 */
88
Sam Ravnborg05790c62006-03-20 22:37:04 -080089static u32 htohl(u32 in, int swap)
Per Lidenb97bf3f2006-01-02 19:04:38 +010090{
91 char *c = (char *)∈
92
93 return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in;
94}
95
96/**
97 * subscr_send_event - send a message containing a tipc_event to the subscriber
98 */
99
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900100static void subscr_send_event(struct subscription *sub,
101 u32 found_lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100102 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900103 u32 event,
104 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100105 u32 node)
106{
107 struct iovec msg_sect;
108
109 msg_sect.iov_base = (void *)&sub->evt;
110 msg_sect.iov_len = sizeof(struct tipc_event);
111
112 sub->evt.event = htohl(event, sub->owner->swap);
113 sub->evt.found_lower = htohl(found_lower, sub->owner->swap);
114 sub->evt.found_upper = htohl(found_upper, sub->owner->swap);
115 sub->evt.port.ref = htohl(port_ref, sub->owner->swap);
116 sub->evt.port.node = htohl(node, sub->owner->swap);
117 tipc_send(sub->owner->port_ref, 1, &msg_sect);
118}
119
120/**
Per Liden4323add2006-01-18 00:38:21 +0100121 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +0100122 *
123 * Returns 1 if there is overlap, otherwise 0.
124 */
125
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900126int tipc_subscr_overlap(struct subscription *sub,
127 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100128 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100129
130{
131 if (found_lower < sub->seq.lower)
132 found_lower = sub->seq.lower;
133 if (found_upper > sub->seq.upper)
134 found_upper = sub->seq.upper;
135 if (found_lower > found_upper)
136 return 0;
137 return 1;
138}
139
140/**
Per Liden4323add2006-01-18 00:38:21 +0100141 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900142 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 * Protected by nameseq.lock in name_table.c
144 */
145
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900146void tipc_subscr_report_overlap(struct subscription *sub,
147 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100148 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900149 u32 event,
150 u32 port_ref,
Per Liden4323add2006-01-18 00:38:21 +0100151 u32 node,
152 int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100153{
154 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
155 sub->seq.upper, found_lower, found_upper);
Per Liden4323add2006-01-18 00:38:21 +0100156 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700158 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700160
161 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100162}
163
164/**
165 * subscr_timeout - subscription timeout has occurred
166 */
167
168static void subscr_timeout(struct subscription *sub)
169{
170 struct subscriber *subscriber;
171 u32 subscriber_ref;
172
173 /* Validate subscriber reference (in case subscriber is terminating) */
174
175 subscriber_ref = sub->owner->ref;
Per Liden4323add2006-01-18 00:38:21 +0100176 subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100177 if (subscriber == NULL)
178 return;
179
Lijun Cheneb409462006-10-16 21:59:42 -0700180 /* Validate timeout (in case subscription is being cancelled) */
181
182 if (sub->timeout == TIPC_WAIT_FOREVER) {
183 tipc_ref_unlock(subscriber_ref);
184 return;
185 }
186
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 /* Unlink subscription from name table */
188
Per Liden4323add2006-01-18 00:38:21 +0100189 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100190
191 /* Notify subscriber of timeout, then unlink subscription */
192
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900193 subscr_send_event(sub,
194 sub->evt.s.seq.lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100195 sub->evt.s.seq.upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900196 TIPC_SUBSCR_TIMEOUT,
197 0,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100198 0);
199 list_del(&sub->subscription_list);
200
201 /* Now destroy subscription */
202
Per Liden4323add2006-01-18 00:38:21 +0100203 tipc_ref_unlock(subscriber_ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100204 k_term_timer(&sub->timer);
205 kfree(sub);
206 atomic_dec(&topsrv.subscription_count);
207}
208
209/**
Lijun Cheneb409462006-10-16 21:59:42 -0700210 * subscr_del - delete a subscription within a subscription list
211 *
212 * Called with subscriber locked.
213 */
214
215static void subscr_del(struct subscription *sub)
216{
217 tipc_nametbl_unsubscribe(sub);
218 list_del(&sub->subscription_list);
219 kfree(sub);
220 atomic_dec(&topsrv.subscription_count);
221}
222
223/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100224 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900225 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 * Called with subscriber locked. Routine must temporarily release this lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900227 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900229 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100230 * a new object reference in the interim that uses this lock; this routine will
231 * simply wait for it to be released, then claim it.)
232 */
233
234static void subscr_terminate(struct subscriber *subscriber)
235{
236 struct subscription *sub;
237 struct subscription *sub_temp;
238
239 /* Invalidate subscriber reference */
240
Per Liden4323add2006-01-18 00:38:21 +0100241 tipc_ref_discard(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 spin_unlock_bh(subscriber->lock);
243
244 /* Destroy any existing subscriptions for subscriber */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900245
Per Lidenb97bf3f2006-01-02 19:04:38 +0100246 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
247 subscription_list) {
248 if (sub->timeout != TIPC_WAIT_FOREVER) {
249 k_cancel_timer(&sub->timer);
250 k_term_timer(&sub->timer);
251 }
Lijun Cheneb409462006-10-16 21:59:42 -0700252 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100253 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
Lijun Cheneb409462006-10-16 21:59:42 -0700254 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100255 }
256
257 /* Sever connection to subscriber */
258
259 tipc_shutdown(subscriber->port_ref);
260 tipc_deleteport(subscriber->port_ref);
261
262 /* Remove subscriber from topology server's subscriber list */
263
264 spin_lock_bh(&topsrv.lock);
265 list_del(&subscriber->subscriber_list);
266 spin_unlock_bh(&topsrv.lock);
267
268 /* Now destroy subscriber */
269
270 spin_lock_bh(subscriber->lock);
271 kfree(subscriber);
272}
273
274/**
Lijun Cheneb409462006-10-16 21:59:42 -0700275 * subscr_cancel - handle subscription cancellation request
276 *
277 * Called with subscriber locked. Routine must temporarily release this lock
278 * to enable the subscription timeout routine to finish without deadlocking;
279 * the lock is then reclaimed to allow caller to release it upon return.
280 *
281 * Note that fields of 's' use subscriber's endianness!
282 */
283
284static void subscr_cancel(struct tipc_subscr *s,
285 struct subscriber *subscriber)
286{
287 struct subscription *sub;
288 struct subscription *sub_temp;
289 int found = 0;
290
291 /* Find first matching subscription, exit if not found */
292
293 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
294 subscription_list) {
295 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
296 found = 1;
297 break;
298 }
299 }
300 if (!found)
301 return;
302
303 /* Cancel subscription timer (if used), then delete subscription */
304
305 if (sub->timeout != TIPC_WAIT_FOREVER) {
306 sub->timeout = TIPC_WAIT_FOREVER;
307 spin_unlock_bh(subscriber->lock);
308 k_cancel_timer(&sub->timer);
309 k_term_timer(&sub->timer);
310 spin_lock_bh(subscriber->lock);
311 }
312 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
313 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
314 subscr_del(sub);
315}
316
317/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900319 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 * Called with subscriber locked
321 */
322
323static void subscr_subscribe(struct tipc_subscr *s,
324 struct subscriber *subscriber)
325{
326 struct subscription *sub;
327
Lijun Cheneb409462006-10-16 21:59:42 -0700328 /* Determine/update subscriber's endianness */
329
330 if (s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE))
331 subscriber->swap = 0;
332 else
333 subscriber->swap = 1;
334
335 /* Detect & process a subscription cancellation request */
336
337 if (s->filter & htohl(TIPC_SUB_CANCEL, subscriber->swap)) {
338 s->filter &= ~htohl(TIPC_SUB_CANCEL, subscriber->swap);
339 subscr_cancel(s, subscriber);
340 return;
341 }
342
Per Lidenb97bf3f2006-01-02 19:04:38 +0100343 /* Refuse subscription if global limit exceeded */
344
345 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700346 warn("Subscription rejected, subscription limit reached (%u)\n",
347 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 subscr_terminate(subscriber);
349 return;
350 }
351
352 /* Allocate subscription object */
353
Arnaldo Carvalho de Melo2710b572006-11-21 01:22:12 -0200354 sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700355 if (!sub) {
356 warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100357 subscr_terminate(subscriber);
358 return;
359 }
360
Per Lidenb97bf3f2006-01-02 19:04:38 +0100361 /* Initialize subscription object */
362
Per Lidenb97bf3f2006-01-02 19:04:38 +0100363 sub->seq.type = htohl(s->seq.type, subscriber->swap);
364 sub->seq.lower = htohl(s->seq.lower, subscriber->swap);
365 sub->seq.upper = htohl(s->seq.upper, subscriber->swap);
366 sub->timeout = htohl(s->timeout, subscriber->swap);
367 sub->filter = htohl(s->filter, subscriber->swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700368 if ((!(sub->filter & TIPC_SUB_PORTS)
369 == !(sub->filter & TIPC_SUB_SERVICE))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100370 || (sub->seq.lower > sub->seq.upper)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700371 warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100372 kfree(sub);
373 subscr_terminate(subscriber);
374 return;
375 }
Allan Stephense15f8802008-05-19 13:27:31 -0700376 sub->event_cb = subscr_send_event;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100377 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
378 INIT_LIST_HEAD(&sub->subscription_list);
379 INIT_LIST_HEAD(&sub->nameseq_list);
380 list_add(&sub->subscription_list, &subscriber->subscription_list);
381 atomic_inc(&topsrv.subscription_count);
382 if (sub->timeout != TIPC_WAIT_FOREVER) {
383 k_init_timer(&sub->timer,
384 (Handler)subscr_timeout, (unsigned long)sub);
385 k_start_timer(&sub->timer, sub->timeout);
386 }
387 sub->owner = subscriber;
Per Liden4323add2006-01-18 00:38:21 +0100388 tipc_nametbl_subscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100389}
390
391/**
392 * subscr_conn_shutdown_event - handle termination request from subscriber
393 */
394
395static void subscr_conn_shutdown_event(void *usr_handle,
396 u32 portref,
397 struct sk_buff **buf,
398 unsigned char const *data,
399 unsigned int size,
400 int reason)
401{
David S. Miller880b0052006-01-12 13:22:32 -0800402 struct subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100403 spinlock_t *subscriber_lock;
404
Per Liden4323add2006-01-18 00:38:21 +0100405 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 if (subscriber == NULL)
407 return;
408
409 subscriber_lock = subscriber->lock;
410 subscr_terminate(subscriber);
411 spin_unlock_bh(subscriber_lock);
412}
413
414/**
415 * subscr_conn_msg_event - handle new subscription request from subscriber
416 */
417
418static void subscr_conn_msg_event(void *usr_handle,
419 u32 port_ref,
420 struct sk_buff **buf,
421 const unchar *data,
422 u32 size)
423{
David S. Miller880b0052006-01-12 13:22:32 -0800424 struct subscriber *subscriber;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100425 spinlock_t *subscriber_lock;
426
Per Liden4323add2006-01-18 00:38:21 +0100427 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428 if (subscriber == NULL)
429 return;
430
431 subscriber_lock = subscriber->lock;
432 if (size != sizeof(struct tipc_subscr))
433 subscr_terminate(subscriber);
434 else
435 subscr_subscribe((struct tipc_subscr *)data, subscriber);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900436
Per Lidenb97bf3f2006-01-02 19:04:38 +0100437 spin_unlock_bh(subscriber_lock);
438}
439
440/**
441 * subscr_named_msg_event - handle request to establish a new subscriber
442 */
443
444static void subscr_named_msg_event(void *usr_handle,
445 u32 port_ref,
446 struct sk_buff **buf,
447 const unchar *data,
448 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900449 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100450 struct tipc_portid const *orig,
451 struct tipc_name_seq const *dest)
452{
453 struct subscriber *subscriber;
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800454 struct iovec msg_sect = {NULL, 0};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100455 spinlock_t *subscriber_lock;
456
457 dbg("subscr_named_msg_event: orig = %x own = %x,\n",
458 orig->node, tipc_own_addr);
459 if (size && (size != sizeof(struct tipc_subscr))) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700460 warn("Subscriber rejected, invalid subscription size\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100461 return;
462 }
463
464 /* Create subscriber object */
465
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700466 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100467 if (subscriber == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700468 warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469 return;
470 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 INIT_LIST_HEAD(&subscriber->subscription_list);
472 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Liden4323add2006-01-18 00:38:21 +0100473 subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100474 if (subscriber->ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700475 warn("Subscriber rejected, reference table exhausted\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100476 kfree(subscriber);
477 return;
478 }
Allan Stephens7ef43eb2008-05-12 15:42:28 -0700479 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100480
481 /* Establish a connection to subscriber */
482
483 tipc_createport(topsrv.user_ref,
David S. Miller880b0052006-01-12 13:22:32 -0800484 (void *)(unsigned long)subscriber->ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800486 NULL,
487 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800489 NULL,
490 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800492 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493 &subscriber->port_ref);
494 if (subscriber->port_ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700495 warn("Subscriber rejected, unable to create port\n");
Per Liden4323add2006-01-18 00:38:21 +0100496 tipc_ref_discard(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100497 kfree(subscriber);
498 return;
499 }
500 tipc_connect2port(subscriber->port_ref, orig);
501
502
503 /* Add subscriber to topology server's subscriber list */
504
Per Liden4323add2006-01-18 00:38:21 +0100505 tipc_ref_lock(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 spin_lock_bh(&topsrv.lock);
507 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
508 spin_unlock_bh(&topsrv.lock);
509
510 /*
511 * Subscribe now if message contains a subscription,
512 * otherwise send an empty response to complete connection handshaking
513 */
514
515 subscriber_lock = subscriber->lock;
516 if (size)
517 subscr_subscribe((struct tipc_subscr *)data, subscriber);
518 else
519 tipc_send(subscriber->port_ref, 1, &msg_sect);
520
521 spin_unlock_bh(subscriber_lock);
522}
523
Per Liden4323add2006-01-18 00:38:21 +0100524int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525{
526 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
527 int res = -1;
528
529 memset(&topsrv, 0, sizeof (topsrv));
Ingo Molnar34af9462006-06-27 02:53:55 -0700530 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 INIT_LIST_HEAD(&topsrv.subscriber_list);
532
533 spin_lock_bh(&topsrv.lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800534 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 if (res) {
536 spin_unlock_bh(&topsrv.lock);
537 return res;
538 }
539
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900540 res = tipc_createport(topsrv.user_ref,
541 NULL,
542 TIPC_CRITICAL_IMPORTANCE,
543 NULL,
544 NULL,
545 NULL,
546 NULL,
547 subscr_named_msg_event,
548 NULL,
549 NULL,
550 &topsrv.setup_port);
551 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100552 goto failed;
553
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900554 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
555 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100556 goto failed;
557
558 spin_unlock_bh(&topsrv.lock);
559 return 0;
560
561failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100562 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 tipc_detach(topsrv.user_ref);
564 topsrv.user_ref = 0;
565 spin_unlock_bh(&topsrv.lock);
566 return res;
567}
568
Per Liden4323add2006-01-18 00:38:21 +0100569void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570{
571 struct subscriber *subscriber;
572 struct subscriber *subscriber_temp;
573 spinlock_t *subscriber_lock;
574
575 if (topsrv.user_ref) {
576 tipc_deleteport(topsrv.setup_port);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900577 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 &topsrv.subscriber_list,
579 subscriber_list) {
Per Liden4323add2006-01-18 00:38:21 +0100580 tipc_ref_lock(subscriber->ref);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100581 subscriber_lock = subscriber->lock;
582 subscr_terminate(subscriber);
583 spin_unlock_bh(subscriber_lock);
584 }
585 tipc_detach(topsrv.user_ref);
586 topsrv.user_ref = 0;
587 }
588}
589
590
591int tipc_ispublished(struct tipc_name const *name)
592{
593 u32 domain = 0;
594
Per Liden4323add2006-01-18 00:38:21 +0100595 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100596}
597