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
50 * @swap: indicates if subscriber uses opposite endianness in its messages
56 struct list_head subscriber_list;
57 struct list_head subscription_list;
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
74 atomic_t subscription_count;
75 struct list_head subscriber_list;
79 static struct top_srv topsrv = { 0 };
82 * htohl - convert value to endianness used by destination
83 * @in: value to convert
84 * @swap: non-zero if endianness must be reversed
86 * Returns converted value
89 static u32 htohl(u32 in, int swap)
91 char *c = (char *)∈
93 return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in;
97 * subscr_send_event - send a message containing a tipc_event to the subscriber
100 static void subscr_send_event(struct subscription *sub,
107 struct iovec msg_sect;
109 msg_sect.iov_base = (void *)&sub->evt;
110 msg_sect.iov_len = sizeof(struct tipc_event);
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);
121 * tipc_subscr_overlap - test for subscription overlap with the given values
123 * Returns 1 if there is overlap, otherwise 0.
126 int tipc_subscr_overlap(struct subscription *sub,
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)
141 * tipc_subscr_report_overlap - issue event if there is subscription overlap
143 * Protected by nameseq.lock in name_table.c
146 void tipc_subscr_report_overlap(struct subscription *sub,
154 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
155 sub->seq.upper, found_lower, found_upper);
156 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
158 if (!must && !(sub->filter & TIPC_SUB_PORTS))
161 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
165 * subscr_timeout - subscription timeout has occurred
168 static void subscr_timeout(struct subscription *sub)
170 struct subscriber *subscriber;
173 /* Validate subscriber reference (in case subscriber is terminating) */
175 subscriber_ref = sub->owner->ref;
176 subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
177 if (subscriber == NULL)
180 /* Validate timeout (in case subscription is being cancelled) */
182 if (sub->timeout == TIPC_WAIT_FOREVER) {
183 tipc_ref_unlock(subscriber_ref);
187 /* Unlink subscription from name table */
189 tipc_nametbl_unsubscribe(sub);
191 /* Notify subscriber of timeout, then unlink subscription */
193 subscr_send_event(sub,
194 sub->evt.s.seq.lower,
195 sub->evt.s.seq.upper,
199 list_del(&sub->subscription_list);
201 /* Now destroy subscription */
203 tipc_ref_unlock(subscriber_ref);
204 k_term_timer(&sub->timer);
206 atomic_dec(&topsrv.subscription_count);
210 * subscr_del - delete a subscription within a subscription list
212 * Called with subscriber locked.
215 static void subscr_del(struct subscription *sub)
217 tipc_nametbl_unsubscribe(sub);
218 list_del(&sub->subscription_list);
220 atomic_dec(&topsrv.subscription_count);
224 * subscr_terminate - terminate communication with a subscriber
226 * Called with subscriber locked. Routine must temporarily release this lock
227 * to enable subscription timeout routine(s) to finish without deadlocking;
228 * the lock is then reclaimed to allow caller to release it upon return.
229 * (This should work even in the unlikely event some other thread creates
230 * 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.)
234 static void subscr_terminate(struct subscriber *subscriber)
236 struct subscription *sub;
237 struct subscription *sub_temp;
239 /* Invalidate subscriber reference */
241 tipc_ref_discard(subscriber->ref);
242 spin_unlock_bh(subscriber->lock);
244 /* Destroy any existing subscriptions for subscriber */
246 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
248 if (sub->timeout != TIPC_WAIT_FOREVER) {
249 k_cancel_timer(&sub->timer);
250 k_term_timer(&sub->timer);
252 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
253 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
257 /* Sever connection to subscriber */
259 tipc_shutdown(subscriber->port_ref);
260 tipc_deleteport(subscriber->port_ref);
262 /* Remove subscriber from topology server's subscriber list */
264 spin_lock_bh(&topsrv.lock);
265 list_del(&subscriber->subscriber_list);
266 spin_unlock_bh(&topsrv.lock);
268 /* Now destroy subscriber */
270 spin_lock_bh(subscriber->lock);
275 * subscr_cancel - handle subscription cancellation request
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.
281 * Note that fields of 's' use subscriber's endianness!
284 static void subscr_cancel(struct tipc_subscr *s,
285 struct subscriber *subscriber)
287 struct subscription *sub;
288 struct subscription *sub_temp;
291 /* Find first matching subscription, exit if not found */
293 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
295 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
303 /* Cancel subscription timer (if used), then delete subscription */
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);
312 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
313 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
318 * subscr_subscribe - create subscription for subscriber
320 * Called with subscriber locked
323 static void subscr_subscribe(struct tipc_subscr *s,
324 struct subscriber *subscriber)
326 struct subscription *sub;
328 /* Determine/update subscriber's endianness */
330 if (s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE))
331 subscriber->swap = 0;
333 subscriber->swap = 1;
335 /* Detect & process a subscription cancellation request */
337 if (s->filter & htohl(TIPC_SUB_CANCEL, subscriber->swap)) {
338 s->filter &= ~htohl(TIPC_SUB_CANCEL, subscriber->swap);
339 subscr_cancel(s, subscriber);
343 /* Refuse subscription if global limit exceeded */
345 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
346 warn("Subscription rejected, subscription limit reached (%u)\n",
347 tipc_max_subscriptions);
348 subscr_terminate(subscriber);
352 /* Allocate subscription object */
354 sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
356 warn("Subscription rejected, no memory\n");
357 subscr_terminate(subscriber);
361 /* Initialize subscription object */
363 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);
368 if ((!(sub->filter & TIPC_SUB_PORTS)
369 == !(sub->filter & TIPC_SUB_SERVICE))
370 || (sub->seq.lower > sub->seq.upper)) {
371 warn("Subscription rejected, illegal request\n");
373 subscr_terminate(subscriber);
376 sub->event_cb = subscr_send_event;
377 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);
387 sub->owner = subscriber;
388 tipc_nametbl_subscribe(sub);
392 * subscr_conn_shutdown_event - handle termination request from subscriber
395 static void subscr_conn_shutdown_event(void *usr_handle,
397 struct sk_buff **buf,
398 unsigned char const *data,
402 struct subscriber *subscriber;
403 spinlock_t *subscriber_lock;
405 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
406 if (subscriber == NULL)
409 subscriber_lock = subscriber->lock;
410 subscr_terminate(subscriber);
411 spin_unlock_bh(subscriber_lock);
415 * subscr_conn_msg_event - handle new subscription request from subscriber
418 static void subscr_conn_msg_event(void *usr_handle,
420 struct sk_buff **buf,
424 struct subscriber *subscriber;
425 spinlock_t *subscriber_lock;
427 subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
428 if (subscriber == NULL)
431 subscriber_lock = subscriber->lock;
432 if (size != sizeof(struct tipc_subscr))
433 subscr_terminate(subscriber);
435 subscr_subscribe((struct tipc_subscr *)data, subscriber);
437 spin_unlock_bh(subscriber_lock);
441 * subscr_named_msg_event - handle request to establish a new subscriber
444 static void subscr_named_msg_event(void *usr_handle,
446 struct sk_buff **buf,
450 struct tipc_portid const *orig,
451 struct tipc_name_seq const *dest)
453 struct subscriber *subscriber;
454 struct iovec msg_sect = {NULL, 0};
455 spinlock_t *subscriber_lock;
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))) {
460 warn("Subscriber rejected, invalid subscription size\n");
464 /* Create subscriber object */
466 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
467 if (subscriber == NULL) {
468 warn("Subscriber rejected, no memory\n");
471 INIT_LIST_HEAD(&subscriber->subscription_list);
472 INIT_LIST_HEAD(&subscriber->subscriber_list);
473 subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
474 if (subscriber->ref == 0) {
475 warn("Subscriber rejected, reference table exhausted\n");
479 spin_unlock_bh(subscriber->lock);
481 /* Establish a connection to subscriber */
483 tipc_createport(topsrv.user_ref,
484 (void *)(unsigned long)subscriber->ref,
488 subscr_conn_shutdown_event,
491 subscr_conn_msg_event,
493 &subscriber->port_ref);
494 if (subscriber->port_ref == 0) {
495 warn("Subscriber rejected, unable to create port\n");
496 tipc_ref_discard(subscriber->ref);
500 tipc_connect2port(subscriber->port_ref, orig);
503 /* Add subscriber to topology server's subscriber list */
505 tipc_ref_lock(subscriber->ref);
506 spin_lock_bh(&topsrv.lock);
507 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
508 spin_unlock_bh(&topsrv.lock);
511 * Subscribe now if message contains a subscription,
512 * otherwise send an empty response to complete connection handshaking
515 subscriber_lock = subscriber->lock;
517 subscr_subscribe((struct tipc_subscr *)data, subscriber);
519 tipc_send(subscriber->port_ref, 1, &msg_sect);
521 spin_unlock_bh(subscriber_lock);
524 int tipc_subscr_start(void)
526 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
529 memset(&topsrv, 0, sizeof (topsrv));
530 spin_lock_init(&topsrv.lock);
531 INIT_LIST_HEAD(&topsrv.subscriber_list);
533 spin_lock_bh(&topsrv.lock);
534 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
536 spin_unlock_bh(&topsrv.lock);
540 res = tipc_createport(topsrv.user_ref,
542 TIPC_CRITICAL_IMPORTANCE,
547 subscr_named_msg_event,
554 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
558 spin_unlock_bh(&topsrv.lock);
562 err("Failed to create subscription service\n");
563 tipc_detach(topsrv.user_ref);
565 spin_unlock_bh(&topsrv.lock);
569 void tipc_subscr_stop(void)
571 struct subscriber *subscriber;
572 struct subscriber *subscriber_temp;
573 spinlock_t *subscriber_lock;
575 if (topsrv.user_ref) {
576 tipc_deleteport(topsrv.setup_port);
577 list_for_each_entry_safe(subscriber, subscriber_temp,
578 &topsrv.subscriber_list,
580 tipc_ref_lock(subscriber->ref);
581 subscriber_lock = subscriber->lock;
582 subscr_terminate(subscriber);
583 spin_unlock_bh(subscriber_lock);
585 tipc_detach(topsrv.user_ref);
591 int tipc_ispublished(struct tipc_name const *name)
595 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);