2 * net/tipc/subscr.c: TIPC network topology service
4 * Copyright (c) 2000-2006, Ericsson AB
5 * Copyright (c) 2005-2007, 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.
39 #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 * subscr_send_event - send a message containing a tipc_event to the subscriber
81 * Note: Must not hold subscriber's server port lock, since tipc_send() will
82 * try to take the lock if the message is rejected and returned!
85 static void subscr_send_event(struct subscription *sub,
92 struct iovec msg_sect;
94 msg_sect.iov_base = (void *)&sub->evt;
95 msg_sect.iov_len = sizeof(struct tipc_event);
97 sub->evt.event = htonl(event);
98 sub->evt.found_lower = htonl(found_lower);
99 sub->evt.found_upper = htonl(found_upper);
100 sub->evt.port.ref = htonl(port_ref);
101 sub->evt.port.node = htonl(node);
102 tipc_send(sub->server_ref, 1, &msg_sect);
106 * tipc_subscr_overlap - test for subscription overlap with the given values
108 * Returns 1 if there is overlap, otherwise 0.
111 int tipc_subscr_overlap(struct subscription *sub,
116 if (found_lower < sub->seq.lower)
117 found_lower = sub->seq.lower;
118 if (found_upper > sub->seq.upper)
119 found_upper = sub->seq.upper;
120 if (found_lower > found_upper)
126 * tipc_subscr_report_overlap - issue event if there is subscription overlap
128 * Protected by nameseq.lock in name_table.c
131 void tipc_subscr_report_overlap(struct subscription *sub,
139 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
141 if (!must && !(sub->filter & TIPC_SUB_PORTS))
144 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
148 * subscr_timeout - subscription timeout has occurred
151 static void subscr_timeout(struct subscription *sub)
153 struct port *server_port;
155 /* Validate server port reference (in case subscriber is terminating) */
157 server_port = tipc_port_lock(sub->server_ref);
158 if (server_port == NULL)
161 /* Validate timeout (in case subscription is being cancelled) */
163 if (sub->timeout == TIPC_WAIT_FOREVER) {
164 tipc_port_unlock(server_port);
168 /* Unlink subscription from name table */
170 tipc_nametbl_unsubscribe(sub);
172 /* Unlink subscription from subscriber */
174 list_del(&sub->subscription_list);
176 /* Release subscriber's server port */
178 tipc_port_unlock(server_port);
180 /* Notify subscriber of timeout */
182 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
183 TIPC_SUBSCR_TIMEOUT, 0, 0);
185 /* Now destroy subscription */
187 k_term_timer(&sub->timer);
189 atomic_dec(&topsrv.subscription_count);
193 * subscr_del - delete a subscription within a subscription list
195 * Called with subscriber port locked.
198 static void subscr_del(struct subscription *sub)
200 tipc_nametbl_unsubscribe(sub);
201 list_del(&sub->subscription_list);
203 atomic_dec(&topsrv.subscription_count);
207 * subscr_terminate - terminate communication with a subscriber
209 * Called with subscriber port locked. Routine must temporarily release lock
210 * to enable subscription timeout routine(s) to finish without deadlocking;
211 * the lock is then reclaimed to allow caller to release it upon return.
212 * (This should work even in the unlikely event some other thread creates
213 * a new object reference in the interim that uses this lock; this routine will
214 * simply wait for it to be released, then claim it.)
217 static void subscr_terminate(struct subscriber *subscriber)
220 struct subscription *sub;
221 struct subscription *sub_temp;
223 /* Invalidate subscriber reference */
225 port_ref = subscriber->port_ref;
226 subscriber->port_ref = 0;
227 spin_unlock_bh(subscriber->lock);
229 /* Sever connection to subscriber */
231 tipc_shutdown(port_ref);
232 tipc_deleteport(port_ref);
234 /* Destroy any existing subscriptions for subscriber */
236 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
238 if (sub->timeout != TIPC_WAIT_FOREVER) {
239 k_cancel_timer(&sub->timer);
240 k_term_timer(&sub->timer);
242 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
243 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
247 /* Remove subscriber from topology server's subscriber list */
249 spin_lock_bh(&topsrv.lock);
250 list_del(&subscriber->subscriber_list);
251 spin_unlock_bh(&topsrv.lock);
253 /* Reclaim subscriber lock */
255 spin_lock_bh(subscriber->lock);
257 /* Now destroy subscriber */
263 * subscr_cancel - handle subscription cancellation request
265 * Called with subscriber port locked. Routine must temporarily release lock
266 * to enable the subscription timeout routine to finish without deadlocking;
267 * the lock is then reclaimed to allow caller to release it upon return.
269 * Note that fields of 's' use subscriber's endianness!
272 static void subscr_cancel(struct tipc_subscr *s,
273 struct subscriber *subscriber)
275 struct subscription *sub;
276 struct subscription *sub_temp;
277 __u32 type, lower, upper, timeout, filter;
280 /* Find first matching subscription, exit if not found */
282 type = ntohl(s->seq.type);
283 lower = ntohl(s->seq.lower);
284 upper = ntohl(s->seq.upper);
285 timeout = ntohl(s->timeout);
286 filter = ntohl(s->filter) & ~TIPC_SUB_CANCEL;
288 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
290 if ((type == sub->seq.type) &&
291 (lower == sub->seq.lower) &&
292 (upper == sub->seq.upper) &&
293 (timeout == sub->timeout) &&
294 (filter == sub->filter) &&
295 !memcmp(s->usr_handle,sub->evt.s.usr_handle,
296 sizeof(s->usr_handle)) ){
304 /* Cancel subscription timer (if used), then delete subscription */
306 if (sub->timeout != TIPC_WAIT_FOREVER) {
307 sub->timeout = TIPC_WAIT_FOREVER;
308 spin_unlock_bh(subscriber->lock);
309 k_cancel_timer(&sub->timer);
310 k_term_timer(&sub->timer);
311 spin_lock_bh(subscriber->lock);
313 dbg("Cancel: removing sub %u,%u,%u from subscriber %p list\n",
314 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
319 * subscr_subscribe - create subscription for subscriber
321 * Called with subscriber port locked.
324 static struct subscription *subscr_subscribe(struct tipc_subscr *s,
325 struct subscriber *subscriber)
327 struct subscription *sub;
329 /* Detect & process a subscription cancellation request */
331 if (ntohl(s->filter) & TIPC_SUB_CANCEL) {
332 subscr_cancel(s, subscriber);
336 /* Refuse subscription if global limit exceeded */
338 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
339 warn("Subscription rejected, subscription limit reached (%u)\n",
340 tipc_max_subscriptions);
341 subscr_terminate(subscriber);
345 /* Allocate subscription object */
347 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
349 warn("Subscription rejected, no memory\n");
350 subscr_terminate(subscriber);
354 /* Initialize subscription object */
356 sub->seq.type = ntohl(s->seq.type);
357 sub->seq.lower = ntohl(s->seq.lower);
358 sub->seq.upper = ntohl(s->seq.upper);
359 sub->timeout = ntohl(s->timeout);
360 sub->filter = ntohl(s->filter);
361 if ((sub->filter && (sub->filter != TIPC_SUB_PORTS)) ||
362 (sub->seq.lower > sub->seq.upper)) {
363 warn("Subscription rejected, illegal request\n");
365 subscr_terminate(subscriber);
368 sub->event_cb = subscr_send_event;
369 INIT_LIST_HEAD(&sub->nameseq_list);
370 list_add(&sub->subscription_list, &subscriber->subscription_list);
371 sub->server_ref = subscriber->port_ref;
372 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
373 atomic_inc(&topsrv.subscription_count);
374 if (sub->timeout != TIPC_WAIT_FOREVER) {
375 k_init_timer(&sub->timer,
376 (Handler)subscr_timeout, (unsigned long)sub);
377 k_start_timer(&sub->timer, sub->timeout);
384 * subscr_conn_shutdown_event - handle termination request from subscriber
386 * Called with subscriber's server port unlocked.
389 static void subscr_conn_shutdown_event(void *usr_handle,
391 struct sk_buff **buf,
392 unsigned char const *data,
396 struct subscriber *subscriber = usr_handle;
397 spinlock_t *subscriber_lock;
399 if (tipc_port_lock(port_ref) == NULL)
402 subscriber_lock = subscriber->lock;
403 subscr_terminate(subscriber);
404 spin_unlock_bh(subscriber_lock);
408 * subscr_conn_msg_event - handle new subscription request from subscriber
410 * Called with subscriber's server port unlocked.
413 static void subscr_conn_msg_event(void *usr_handle,
415 struct sk_buff **buf,
419 struct subscriber *subscriber = usr_handle;
420 spinlock_t *subscriber_lock;
421 struct subscription *sub;
424 * Lock subscriber's server port (& make a local copy of lock pointer,
425 * in case subscriber is deleted while processing subscription request)
428 if (tipc_port_lock(port_ref) == NULL)
431 subscriber_lock = subscriber->lock;
433 if (size != sizeof(struct tipc_subscr)) {
434 subscr_terminate(subscriber);
435 spin_unlock_bh(subscriber_lock);
437 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
438 spin_unlock_bh(subscriber_lock);
442 * We must release the server port lock before adding a
443 * subscription to the name table since TIPC needs to be
444 * able to (re)acquire the port lock if an event message
445 * issued by the subscription process is rejected and
446 * returned. The subscription cannot be deleted while
447 * it is being added to the name table because:
448 * a) the single-threading of the native API port code
449 * ensures the subscription cannot be cancelled and
450 * the subscriber connection cannot be broken, and
451 * b) the name table lock ensures the subscription
452 * timeout code cannot delete the subscription,
453 * so the subscription object is still protected.
456 tipc_nametbl_subscribe(sub);
462 * subscr_named_msg_event - handle request to establish a new subscriber
465 static void subscr_named_msg_event(void *usr_handle,
467 struct sk_buff **buf,
471 struct tipc_portid const *orig,
472 struct tipc_name_seq const *dest)
474 static struct iovec msg_sect = {NULL, 0};
476 struct subscriber *subscriber;
479 /* Create subscriber object */
481 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
482 if (subscriber == NULL) {
483 warn("Subscriber rejected, no memory\n");
486 INIT_LIST_HEAD(&subscriber->subscription_list);
487 INIT_LIST_HEAD(&subscriber->subscriber_list);
489 /* Create server port & establish connection to subscriber */
491 tipc_createport(topsrv.user_ref,
496 subscr_conn_shutdown_event,
499 subscr_conn_msg_event,
501 &subscriber->port_ref);
502 if (subscriber->port_ref == 0) {
503 warn("Subscriber rejected, unable to create port\n");
507 tipc_connect2port(subscriber->port_ref, orig);
509 /* Lock server port (& save lock address for future use) */
511 subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
513 /* Add subscriber to topology server's subscriber list */
515 spin_lock_bh(&topsrv.lock);
516 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
517 spin_unlock_bh(&topsrv.lock);
519 /* Unlock server port */
521 server_port_ref = subscriber->port_ref;
522 spin_unlock_bh(subscriber->lock);
524 /* Send an ACK- to complete connection handshaking */
526 tipc_send(server_port_ref, 1, &msg_sect);
528 /* Handle optional subscription request */
531 subscr_conn_msg_event(subscriber, server_port_ref,
536 int tipc_subscr_start(void)
538 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
541 memset(&topsrv, 0, sizeof (topsrv));
542 spin_lock_init(&topsrv.lock);
543 INIT_LIST_HEAD(&topsrv.subscriber_list);
545 spin_lock_bh(&topsrv.lock);
546 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
548 spin_unlock_bh(&topsrv.lock);
552 res = tipc_createport(topsrv.user_ref,
554 TIPC_CRITICAL_IMPORTANCE,
559 subscr_named_msg_event,
566 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
570 spin_unlock_bh(&topsrv.lock);
574 err("Failed to create subscription service\n");
575 tipc_detach(topsrv.user_ref);
577 spin_unlock_bh(&topsrv.lock);
581 void tipc_subscr_stop(void)
583 struct subscriber *subscriber;
584 struct subscriber *subscriber_temp;
585 spinlock_t *subscriber_lock;
587 if (topsrv.user_ref) {
588 tipc_deleteport(topsrv.setup_port);
589 list_for_each_entry_safe(subscriber, subscriber_temp,
590 &topsrv.subscriber_list,
592 subscriber_lock = subscriber->lock;
593 spin_lock_bh(subscriber_lock);
594 subscr_terminate(subscriber);
595 spin_unlock_bh(subscriber_lock);
597 tipc_detach(topsrv.user_ref);
603 int tipc_ispublished(struct tipc_name const *name)
607 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);