2 * net/tipc/subscr.c: TIPC subscription service
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005, Wind River Systems
8 * Redistribution and use in source and binary forms, with or without
9 * modification, are permitted provided that the following conditions are met:
11 * 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.
20 * 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.
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
34 * POSSIBILITY OF SUCH DAMAGE.
40 #include "name_table.h"
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
55 struct list_head subscriber_list
;
56 struct list_head subscription_list
;
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
72 atomic_t subscription_count
;
73 struct list_head subscriber_list
;
77 static struct top_srv topsrv
= { 0 };
80 * htohl - convert value to endianness used by destination
81 * @in: value to convert
82 * @swap: non-zero if endianness must be reversed
84 * Returns converted value
87 static u32
htohl(u32 in
, int swap
)
89 return swap
? (u32
)___constant_swab32(in
) : in
;
93 * subscr_send_event - send a message containing a tipc_event to the subscriber
96 static void subscr_send_event(struct subscription
*sub
,
103 struct iovec msg_sect
;
105 msg_sect
.iov_base
= (void *)&sub
->evt
;
106 msg_sect
.iov_len
= sizeof(struct tipc_event
);
108 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
);
113 tipc_send(sub
->owner
->port_ref
, 1, &msg_sect
);
117 * tipc_subscr_overlap - test for subscription overlap with the given values
119 * Returns 1 if there is overlap, otherwise 0.
122 int tipc_subscr_overlap(struct subscription
*sub
,
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
)
137 * tipc_subscr_report_overlap - issue event if there is subscription overlap
139 * Protected by nameseq.lock in name_table.c
142 void tipc_subscr_report_overlap(struct subscription
*sub
,
150 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub
->seq
.type
, sub
->seq
.lower
,
151 sub
->seq
.upper
, found_lower
, found_upper
);
152 if (!tipc_subscr_overlap(sub
, found_lower
, found_upper
))
154 if (!must
&& !(sub
->filter
& TIPC_SUB_PORTS
))
157 sub
->event_cb(sub
, found_lower
, found_upper
, event
, port_ref
, node
);
161 * subscr_timeout - subscription timeout has occurred
164 static void subscr_timeout(struct subscription
*sub
)
166 struct subscriber
*subscriber
;
169 /* Validate subscriber reference (in case subscriber is terminating) */
171 subscriber_ref
= sub
->owner
->ref
;
172 subscriber
= (struct subscriber
*)tipc_ref_lock(subscriber_ref
);
173 if (subscriber
== NULL
)
176 /* Validate timeout (in case subscription is being cancelled) */
178 if (sub
->timeout
== TIPC_WAIT_FOREVER
) {
179 tipc_ref_unlock(subscriber_ref
);
183 /* Unlink subscription from name table */
185 tipc_nametbl_unsubscribe(sub
);
187 /* Notify subscriber of timeout, then unlink subscription */
189 subscr_send_event(sub
,
190 sub
->evt
.s
.seq
.lower
,
191 sub
->evt
.s
.seq
.upper
,
195 list_del(&sub
->subscription_list
);
197 /* Now destroy subscription */
199 tipc_ref_unlock(subscriber_ref
);
200 k_term_timer(&sub
->timer
);
202 atomic_dec(&topsrv
.subscription_count
);
206 * subscr_del - delete a subscription within a subscription list
208 * Called with subscriber locked.
211 static void subscr_del(struct subscription
*sub
)
213 tipc_nametbl_unsubscribe(sub
);
214 list_del(&sub
->subscription_list
);
216 atomic_dec(&topsrv
.subscription_count
);
220 * subscr_terminate - terminate communication with a subscriber
222 * Called with subscriber locked. Routine must temporarily release this lock
223 * to enable subscription timeout routine(s) to finish without deadlocking;
224 * the lock is then reclaimed to allow caller to release it upon return.
225 * (This should work even in the unlikely event some other thread creates
226 * 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.)
230 static void subscr_terminate(struct subscriber
*subscriber
)
232 struct subscription
*sub
;
233 struct subscription
*sub_temp
;
235 /* Invalidate subscriber reference */
237 tipc_ref_discard(subscriber
->ref
);
238 spin_unlock_bh(subscriber
->lock
);
240 /* Destroy any existing subscriptions for subscriber */
242 list_for_each_entry_safe(sub
, sub_temp
, &subscriber
->subscription_list
,
244 if (sub
->timeout
!= TIPC_WAIT_FOREVER
) {
245 k_cancel_timer(&sub
->timer
);
246 k_term_timer(&sub
->timer
);
248 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
249 sub
->seq
.type
, sub
->seq
.lower
, sub
->seq
.upper
, subscriber
);
253 /* Sever connection to subscriber */
255 tipc_shutdown(subscriber
->port_ref
);
256 tipc_deleteport(subscriber
->port_ref
);
258 /* Remove subscriber from topology server's subscriber list */
260 spin_lock_bh(&topsrv
.lock
);
261 list_del(&subscriber
->subscriber_list
);
262 spin_unlock_bh(&topsrv
.lock
);
264 /* Now destroy subscriber */
266 spin_lock_bh(subscriber
->lock
);
271 * subscr_cancel - handle subscription cancellation request
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.
277 * Note that fields of 's' use subscriber's endianness!
280 static void subscr_cancel(struct tipc_subscr
*s
,
281 struct subscriber
*subscriber
)
283 struct subscription
*sub
;
284 struct subscription
*sub_temp
;
287 /* Find first matching subscription, exit if not found */
289 list_for_each_entry_safe(sub
, sub_temp
, &subscriber
->subscription_list
,
291 if (!memcmp(s
, &sub
->evt
.s
, sizeof(struct tipc_subscr
))) {
299 /* Cancel subscription timer (if used), then delete subscription */
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
);
308 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
309 sub
->seq
.type
, sub
->seq
.lower
, sub
->seq
.upper
, subscriber
);
314 * subscr_subscribe - create subscription for subscriber
316 * Called with subscriber locked
319 static void subscr_subscribe(struct tipc_subscr
*s
,
320 struct subscriber
*subscriber
)
322 struct subscription
*sub
;
325 /* Determine subscriber's endianness */
327 swap
= !(s
->filter
& (TIPC_SUB_PORTS
| TIPC_SUB_SERVICE
));
329 /* Detect & process a subscription cancellation request */
331 if (s
->filter
& htohl(TIPC_SUB_CANCEL
, swap
)) {
332 s
->filter
&= ~htohl(TIPC_SUB_CANCEL
, swap
);
333 subscr_cancel(s
, subscriber
);
337 /* Refuse subscription if global limit exceeded */
339 if (atomic_read(&topsrv
.subscription_count
) >= tipc_max_subscriptions
) {
340 warn("Subscription rejected, subscription limit reached (%u)\n",
341 tipc_max_subscriptions
);
342 subscr_terminate(subscriber
);
346 /* Allocate subscription object */
348 sub
= kzalloc(sizeof(*sub
), GFP_ATOMIC
);
350 warn("Subscription rejected, no memory\n");
351 subscr_terminate(subscriber
);
355 /* Initialize subscription object */
357 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
);
362 if ((!(sub
->filter
& TIPC_SUB_PORTS
)
363 == !(sub
->filter
& TIPC_SUB_SERVICE
))
364 || (sub
->seq
.lower
> sub
->seq
.upper
)) {
365 warn("Subscription rejected, illegal request\n");
367 subscr_terminate(subscriber
);
370 sub
->event_cb
= subscr_send_event
;
371 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
);
376 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
);
382 sub
->owner
= subscriber
;
383 tipc_nametbl_subscribe(sub
);
387 * subscr_conn_shutdown_event - handle termination request from subscriber
390 static void subscr_conn_shutdown_event(void *usr_handle
,
392 struct sk_buff
**buf
,
393 unsigned char const *data
,
397 struct subscriber
*subscriber
;
398 spinlock_t
*subscriber_lock
;
400 subscriber
= tipc_ref_lock((u32
)(unsigned long)usr_handle
);
401 if (subscriber
== NULL
)
404 subscriber_lock
= subscriber
->lock
;
405 subscr_terminate(subscriber
);
406 spin_unlock_bh(subscriber_lock
);
410 * subscr_conn_msg_event - handle new subscription request from subscriber
413 static void subscr_conn_msg_event(void *usr_handle
,
415 struct sk_buff
**buf
,
419 struct subscriber
*subscriber
;
420 spinlock_t
*subscriber_lock
;
422 subscriber
= tipc_ref_lock((u32
)(unsigned long)usr_handle
);
423 if (subscriber
== NULL
)
426 subscriber_lock
= subscriber
->lock
;
427 if (size
!= sizeof(struct tipc_subscr
))
428 subscr_terminate(subscriber
);
430 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
432 spin_unlock_bh(subscriber_lock
);
436 * subscr_named_msg_event - handle request to establish a new subscriber
439 static void subscr_named_msg_event(void *usr_handle
,
441 struct sk_buff
**buf
,
445 struct tipc_portid
const *orig
,
446 struct tipc_name_seq
const *dest
)
448 struct subscriber
*subscriber
;
449 struct iovec msg_sect
= {NULL
, 0};
450 spinlock_t
*subscriber_lock
;
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
))) {
455 warn("Subscriber rejected, invalid subscription size\n");
459 /* Create subscriber object */
461 subscriber
= kzalloc(sizeof(struct subscriber
), GFP_ATOMIC
);
462 if (subscriber
== NULL
) {
463 warn("Subscriber rejected, no memory\n");
466 INIT_LIST_HEAD(&subscriber
->subscription_list
);
467 INIT_LIST_HEAD(&subscriber
->subscriber_list
);
468 subscriber
->ref
= tipc_ref_acquire(subscriber
, &subscriber
->lock
);
469 if (subscriber
->ref
== 0) {
470 warn("Subscriber rejected, reference table exhausted\n");
474 spin_unlock_bh(subscriber
->lock
);
476 /* Establish a connection to subscriber */
478 tipc_createport(topsrv
.user_ref
,
479 (void *)(unsigned long)subscriber
->ref
,
483 subscr_conn_shutdown_event
,
486 subscr_conn_msg_event
,
488 &subscriber
->port_ref
);
489 if (subscriber
->port_ref
== 0) {
490 warn("Subscriber rejected, unable to create port\n");
491 tipc_ref_discard(subscriber
->ref
);
495 tipc_connect2port(subscriber
->port_ref
, orig
);
498 /* Add subscriber to topology server's subscriber list */
500 tipc_ref_lock(subscriber
->ref
);
501 spin_lock_bh(&topsrv
.lock
);
502 list_add(&subscriber
->subscriber_list
, &topsrv
.subscriber_list
);
503 spin_unlock_bh(&topsrv
.lock
);
506 * Subscribe now if message contains a subscription,
507 * otherwise send an empty response to complete connection handshaking
510 subscriber_lock
= subscriber
->lock
;
512 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
514 tipc_send(subscriber
->port_ref
, 1, &msg_sect
);
516 spin_unlock_bh(subscriber_lock
);
519 int tipc_subscr_start(void)
521 struct tipc_name_seq seq
= {TIPC_TOP_SRV
, TIPC_TOP_SRV
, TIPC_TOP_SRV
};
524 memset(&topsrv
, 0, sizeof (topsrv
));
525 spin_lock_init(&topsrv
.lock
);
526 INIT_LIST_HEAD(&topsrv
.subscriber_list
);
528 spin_lock_bh(&topsrv
.lock
);
529 res
= tipc_attach(&topsrv
.user_ref
, NULL
, NULL
);
531 spin_unlock_bh(&topsrv
.lock
);
535 res
= tipc_createport(topsrv
.user_ref
,
537 TIPC_CRITICAL_IMPORTANCE
,
542 subscr_named_msg_event
,
549 res
= tipc_nametbl_publish_rsv(topsrv
.setup_port
, TIPC_NODE_SCOPE
, &seq
);
553 spin_unlock_bh(&topsrv
.lock
);
557 err("Failed to create subscription service\n");
558 tipc_detach(topsrv
.user_ref
);
560 spin_unlock_bh(&topsrv
.lock
);
564 void tipc_subscr_stop(void)
566 struct subscriber
*subscriber
;
567 struct subscriber
*subscriber_temp
;
568 spinlock_t
*subscriber_lock
;
570 if (topsrv
.user_ref
) {
571 tipc_deleteport(topsrv
.setup_port
);
572 list_for_each_entry_safe(subscriber
, subscriber_temp
,
573 &topsrv
.subscriber_list
,
575 tipc_ref_lock(subscriber
->ref
);
576 subscriber_lock
= subscriber
->lock
;
577 subscr_terminate(subscriber
);
578 spin_unlock_bh(subscriber_lock
);
580 tipc_detach(topsrv
.user_ref
);
586 int tipc_ispublished(struct tipc_name
const *name
)
590 return(tipc_nametbl_translate(name
->type
, name
->instance
,&domain
) != 0);