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"
45 * struct subscriber - TIPC network topology subscriber
46 * @port_ref: object reference to server port connecting to subscriber
47 * @lock: pointer to spinlock controlling access to subscriber's server port
48 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
49 * @subscription_list: list of subscription objects for this subscriber
55 struct list_head subscriber_list;
56 struct list_head subscription_list;
60 * struct top_srv - TIPC network topology subscription service
61 * @user_ref: TIPC userid of subscription service
62 * @setup_port: reference to TIPC port that handles subscription requests
63 * @subscription_count: number of active subscriptions (not subscribers!)
64 * @subscriber_list: list of ports subscribing to service
65 * @lock: spinlock govering access to subscriber list
71 atomic_t subscription_count;
72 struct list_head subscriber_list;
76 static struct top_srv topsrv = { 0 };
79 * htohl - convert value to endianness used by destination
80 * @in: value to convert
81 * @swap: non-zero if endianness must be reversed
83 * Returns converted value
86 static u32 htohl(u32 in, int swap)
88 return swap ? (u32)___constant_swab32(in) : in;
92 * subscr_send_event - send a message containing a tipc_event to the subscriber
94 * Note: Must not hold subscriber's server port lock, since tipc_send() will
95 * try to take the lock if the message is rejected and returned!
98 static void subscr_send_event(struct subscription *sub,
105 struct iovec msg_sect;
107 msg_sect.iov_base = (void *)&sub->evt;
108 msg_sect.iov_len = sizeof(struct tipc_event);
110 sub->evt.event = htohl(event, sub->swap);
111 sub->evt.found_lower = htohl(found_lower, sub->swap);
112 sub->evt.found_upper = htohl(found_upper, sub->swap);
113 sub->evt.port.ref = htohl(port_ref, sub->swap);
114 sub->evt.port.node = htohl(node, sub->swap);
115 tipc_send(sub->server_ref, 1, &msg_sect);
119 * tipc_subscr_overlap - test for subscription overlap with the given values
121 * Returns 1 if there is overlap, otherwise 0.
124 int tipc_subscr_overlap(struct subscription *sub,
129 if (found_lower < sub->seq.lower)
130 found_lower = sub->seq.lower;
131 if (found_upper > sub->seq.upper)
132 found_upper = sub->seq.upper;
133 if (found_lower > found_upper)
139 * tipc_subscr_report_overlap - issue event if there is subscription overlap
141 * Protected by nameseq.lock in name_table.c
144 void tipc_subscr_report_overlap(struct subscription *sub,
152 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
153 sub->seq.upper, found_lower, found_upper);
154 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
156 if (!must && !(sub->filter & TIPC_SUB_PORTS))
159 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
163 * subscr_timeout - subscription timeout has occurred
166 static void subscr_timeout(struct subscription *sub)
168 struct port *server_port;
170 /* Validate server port reference (in case subscriber is terminating) */
172 server_port = tipc_port_lock(sub->server_ref);
173 if (server_port == NULL)
176 /* Validate timeout (in case subscription is being cancelled) */
178 if (sub->timeout == TIPC_WAIT_FOREVER) {
179 tipc_port_unlock(server_port);
183 /* Unlink subscription from name table */
185 tipc_nametbl_unsubscribe(sub);
187 /* Unlink subscription from subscriber */
189 list_del(&sub->subscription_list);
191 /* Release subscriber's server port */
193 tipc_port_unlock(server_port);
195 /* Notify subscriber of timeout */
197 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
198 TIPC_SUBSCR_TIMEOUT, 0, 0);
200 /* Now destroy subscription */
202 k_term_timer(&sub->timer);
204 atomic_dec(&topsrv.subscription_count);
208 * subscr_del - delete a subscription within a subscription list
210 * Called with subscriber port locked.
213 static void subscr_del(struct subscription *sub)
215 tipc_nametbl_unsubscribe(sub);
216 list_del(&sub->subscription_list);
218 atomic_dec(&topsrv.subscription_count);
222 * subscr_terminate - terminate communication with a subscriber
224 * Called with subscriber port locked. Routine must temporarily release lock
225 * to enable subscription timeout routine(s) to finish without deadlocking;
226 * the lock is then reclaimed to allow caller to release it upon return.
227 * (This should work even in the unlikely event some other thread creates
228 * a new object reference in the interim that uses this lock; this routine will
229 * simply wait for it to be released, then claim it.)
232 static void subscr_terminate(struct subscriber *subscriber)
235 struct subscription *sub;
236 struct subscription *sub_temp;
238 /* Invalidate subscriber reference */
240 port_ref = subscriber->port_ref;
241 subscriber->port_ref = 0;
242 spin_unlock_bh(subscriber->lock);
244 /* Sever connection to subscriber */
246 tipc_shutdown(port_ref);
247 tipc_deleteport(port_ref);
249 /* Destroy any existing subscriptions for subscriber */
251 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
253 if (sub->timeout != TIPC_WAIT_FOREVER) {
254 k_cancel_timer(&sub->timer);
255 k_term_timer(&sub->timer);
257 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
258 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
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 /* Reclaim subscriber lock */
270 spin_lock_bh(subscriber->lock);
272 /* Now destroy subscriber */
278 * subscr_cancel - handle subscription cancellation request
280 * Called with subscriber port locked. Routine must temporarily release lock
281 * to enable the subscription timeout routine to finish without deadlocking;
282 * the lock is then reclaimed to allow caller to release it upon return.
284 * Note that fields of 's' use subscriber's endianness!
287 static void subscr_cancel(struct tipc_subscr *s,
288 struct subscriber *subscriber)
290 struct subscription *sub;
291 struct subscription *sub_temp;
294 /* Find first matching subscription, exit if not found */
296 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
298 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
306 /* Cancel subscription timer (if used), then delete subscription */
308 if (sub->timeout != TIPC_WAIT_FOREVER) {
309 sub->timeout = TIPC_WAIT_FOREVER;
310 spin_unlock_bh(subscriber->lock);
311 k_cancel_timer(&sub->timer);
312 k_term_timer(&sub->timer);
313 spin_lock_bh(subscriber->lock);
315 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
316 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
321 * subscr_subscribe - create subscription for subscriber
323 * Called with subscriber port locked.
326 static struct subscription *subscr_subscribe(struct tipc_subscr *s,
327 struct subscriber *subscriber)
329 struct subscription *sub;
332 /* Determine subscriber's endianness */
334 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
336 /* Detect & process a subscription cancellation request */
338 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
339 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
340 subscr_cancel(s, subscriber);
344 /* Refuse subscription if global limit exceeded */
346 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
347 warn("Subscription rejected, subscription limit reached (%u)\n",
348 tipc_max_subscriptions);
349 subscr_terminate(subscriber);
353 /* Allocate subscription object */
355 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
357 warn("Subscription rejected, no memory\n");
358 subscr_terminate(subscriber);
362 /* Initialize subscription object */
364 sub->seq.type = htohl(s->seq.type, swap);
365 sub->seq.lower = htohl(s->seq.lower, swap);
366 sub->seq.upper = htohl(s->seq.upper, swap);
367 sub->timeout = htohl(s->timeout, swap);
368 sub->filter = htohl(s->filter, swap);
369 if ((!(sub->filter & TIPC_SUB_PORTS)
370 == !(sub->filter & TIPC_SUB_SERVICE))
371 || (sub->seq.lower > sub->seq.upper)) {
372 warn("Subscription rejected, illegal request\n");
374 subscr_terminate(subscriber);
377 sub->event_cb = subscr_send_event;
378 INIT_LIST_HEAD(&sub->nameseq_list);
379 list_add(&sub->subscription_list, &subscriber->subscription_list);
380 sub->server_ref = subscriber->port_ref;
382 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
383 atomic_inc(&topsrv.subscription_count);
384 if (sub->timeout != TIPC_WAIT_FOREVER) {
385 k_init_timer(&sub->timer,
386 (Handler)subscr_timeout, (unsigned long)sub);
387 k_start_timer(&sub->timer, sub->timeout);
394 * subscr_conn_shutdown_event - handle termination request from subscriber
396 * Called with subscriber's server port unlocked.
399 static void subscr_conn_shutdown_event(void *usr_handle,
401 struct sk_buff **buf,
402 unsigned char const *data,
406 struct subscriber *subscriber = usr_handle;
407 spinlock_t *subscriber_lock;
409 if (tipc_port_lock(port_ref) == NULL)
412 subscriber_lock = subscriber->lock;
413 subscr_terminate(subscriber);
414 spin_unlock_bh(subscriber_lock);
418 * subscr_conn_msg_event - handle new subscription request from subscriber
420 * Called with subscriber's server port unlocked.
423 static void subscr_conn_msg_event(void *usr_handle,
425 struct sk_buff **buf,
429 struct subscriber *subscriber = usr_handle;
430 spinlock_t *subscriber_lock;
431 struct subscription *sub;
434 * Lock subscriber's server port (& make a local copy of lock pointer,
435 * in case subscriber is deleted while processing subscription request)
438 if (tipc_port_lock(port_ref) == NULL)
441 subscriber_lock = subscriber->lock;
443 if (size != sizeof(struct tipc_subscr)) {
444 subscr_terminate(subscriber);
445 spin_unlock_bh(subscriber_lock);
447 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
448 spin_unlock_bh(subscriber_lock);
452 * We must release the server port lock before adding a
453 * subscription to the name table since TIPC needs to be
454 * able to (re)acquire the port lock if an event message
455 * issued by the subscription process is rejected and
456 * returned. The subscription cannot be deleted while
457 * it is being added to the name table because:
458 * a) the single-threading of the native API port code
459 * ensures the subscription cannot be cancelled and
460 * the subscriber connection cannot be broken, and
461 * b) the name table lock ensures the subscription
462 * timeout code cannot delete the subscription,
463 * so the subscription object is still protected.
466 tipc_nametbl_subscribe(sub);
472 * subscr_named_msg_event - handle request to establish a new subscriber
475 static void subscr_named_msg_event(void *usr_handle,
477 struct sk_buff **buf,
481 struct tipc_portid const *orig,
482 struct tipc_name_seq const *dest)
484 static struct iovec msg_sect = {NULL, 0};
486 struct subscriber *subscriber;
489 /* Create subscriber object */
491 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
492 if (subscriber == NULL) {
493 warn("Subscriber rejected, no memory\n");
496 INIT_LIST_HEAD(&subscriber->subscription_list);
497 INIT_LIST_HEAD(&subscriber->subscriber_list);
499 /* Create server port & establish connection to subscriber */
501 tipc_createport(topsrv.user_ref,
506 subscr_conn_shutdown_event,
509 subscr_conn_msg_event,
511 &subscriber->port_ref);
512 if (subscriber->port_ref == 0) {
513 warn("Subscriber rejected, unable to create port\n");
517 tipc_connect2port(subscriber->port_ref, orig);
519 /* Lock server port (& save lock address for future use) */
521 subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
523 /* Add subscriber to topology server's subscriber list */
525 spin_lock_bh(&topsrv.lock);
526 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
527 spin_unlock_bh(&topsrv.lock);
529 /* Unlock server port */
531 server_port_ref = subscriber->port_ref;
532 spin_unlock_bh(subscriber->lock);
534 /* Send an ACK- to complete connection handshaking */
536 tipc_send(server_port_ref, 1, &msg_sect);
538 /* Handle optional subscription request */
541 subscr_conn_msg_event(subscriber, server_port_ref,
546 int tipc_subscr_start(void)
548 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
551 memset(&topsrv, 0, sizeof (topsrv));
552 spin_lock_init(&topsrv.lock);
553 INIT_LIST_HEAD(&topsrv.subscriber_list);
555 spin_lock_bh(&topsrv.lock);
556 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
558 spin_unlock_bh(&topsrv.lock);
562 res = tipc_createport(topsrv.user_ref,
564 TIPC_CRITICAL_IMPORTANCE,
569 subscr_named_msg_event,
576 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
580 spin_unlock_bh(&topsrv.lock);
584 err("Failed to create subscription service\n");
585 tipc_detach(topsrv.user_ref);
587 spin_unlock_bh(&topsrv.lock);
591 void tipc_subscr_stop(void)
593 struct subscriber *subscriber;
594 struct subscriber *subscriber_temp;
595 spinlock_t *subscriber_lock;
597 if (topsrv.user_ref) {
598 tipc_deleteport(topsrv.setup_port);
599 list_for_each_entry_safe(subscriber, subscriber_temp,
600 &topsrv.subscriber_list,
602 subscriber_lock = subscriber->lock;
603 spin_lock_bh(subscriber_lock);
604 subscr_terminate(subscriber);
605 spin_unlock_bh(subscriber_lock);
607 tipc_detach(topsrv.user_ref);
613 int tipc_ispublished(struct tipc_name const *name)
617 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);