tipc: Consolidate subscriber & subscriber port references
[safe/jmp/linux-2.6] / net / tipc / subscr.c
1 /*
2  * net/tipc/subscr.c: TIPC subscription service
3  *
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2005, Wind River Systems
6  * All rights reserved.
7  *
8  * Redistribution and use in source and binary forms, with or without
9  * modification, are permitted provided that the following conditions are met:
10  *
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.
19  *
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.
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
34  * 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 "port.h"
42 #include "ref.h"
43
44 /**
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
50  */
51
52 struct subscriber {
53         u32 port_ref;
54         spinlock_t *lock;
55         struct list_head subscriber_list;
56         struct list_head subscription_list;
57 };
58
59 /**
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
66  */
67
68 struct top_srv {
69         u32 user_ref;
70         u32 setup_port;
71         atomic_t subscription_count;
72         struct list_head subscriber_list;
73         spinlock_t lock;
74 };
75
76 static struct top_srv topsrv = { 0 };
77
78 /**
79  * htohl - convert value to endianness used by destination
80  * @in: value to convert
81  * @swap: non-zero if endianness must be reversed
82  *
83  * Returns converted value
84  */
85
86 static u32 htohl(u32 in, int swap)
87 {
88         return swap ? (u32)___constant_swab32(in) : in;
89 }
90
91 /**
92  * subscr_send_event - send a message containing a tipc_event to the subscriber
93  *
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!
96  */
97
98 static void subscr_send_event(struct subscription *sub,
99                               u32 found_lower,
100                               u32 found_upper,
101                               u32 event,
102                               u32 port_ref,
103                               u32 node)
104 {
105         struct iovec msg_sect;
106
107         msg_sect.iov_base = (void *)&sub->evt;
108         msg_sect.iov_len = sizeof(struct tipc_event);
109
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);
116 }
117
118 /**
119  * tipc_subscr_overlap - test for subscription overlap with the given values
120  *
121  * Returns 1 if there is overlap, otherwise 0.
122  */
123
124 int tipc_subscr_overlap(struct subscription *sub,
125                         u32 found_lower,
126                         u32 found_upper)
127
128 {
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)
134                 return 0;
135         return 1;
136 }
137
138 /**
139  * tipc_subscr_report_overlap - issue event if there is subscription overlap
140  *
141  * Protected by nameseq.lock in name_table.c
142  */
143
144 void tipc_subscr_report_overlap(struct subscription *sub,
145                                 u32 found_lower,
146                                 u32 found_upper,
147                                 u32 event,
148                                 u32 port_ref,
149                                 u32 node,
150                                 int must)
151 {
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))
155                 return;
156         if (!must && !(sub->filter & TIPC_SUB_PORTS))
157                 return;
158
159         sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
160 }
161
162 /**
163  * subscr_timeout - subscription timeout has occurred
164  */
165
166 static void subscr_timeout(struct subscription *sub)
167 {
168         struct port *server_port;
169
170         /* Validate server port reference (in case subscriber is terminating) */
171
172         server_port = tipc_port_lock(sub->server_ref);
173         if (server_port == NULL)
174                 return;
175
176         /* Validate timeout (in case subscription is being cancelled) */
177
178         if (sub->timeout == TIPC_WAIT_FOREVER) {
179                 tipc_port_unlock(server_port);
180                 return;
181         }
182
183         /* Unlink subscription from name table */
184
185         tipc_nametbl_unsubscribe(sub);
186
187         /* Unlink subscription from subscriber */
188
189         list_del(&sub->subscription_list);
190
191         /* Release subscriber's server port */
192
193         tipc_port_unlock(server_port);
194
195         /* Notify subscriber of timeout */
196
197         subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
198                           TIPC_SUBSCR_TIMEOUT, 0, 0);
199
200         /* Now destroy subscription */
201
202         k_term_timer(&sub->timer);
203         kfree(sub);
204         atomic_dec(&topsrv.subscription_count);
205 }
206
207 /**
208  * subscr_del - delete a subscription within a subscription list
209  *
210  * Called with subscriber port locked.
211  */
212
213 static void subscr_del(struct subscription *sub)
214 {
215         tipc_nametbl_unsubscribe(sub);
216         list_del(&sub->subscription_list);
217         kfree(sub);
218         atomic_dec(&topsrv.subscription_count);
219 }
220
221 /**
222  * subscr_terminate - terminate communication with a subscriber
223  *
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.)
230  */
231
232 static void subscr_terminate(struct subscriber *subscriber)
233 {
234         u32 port_ref;
235         struct subscription *sub;
236         struct subscription *sub_temp;
237
238         /* Invalidate subscriber reference */
239
240         port_ref = subscriber->port_ref;
241         subscriber->port_ref = 0;
242         spin_unlock_bh(subscriber->lock);
243
244         /* Sever connection to subscriber */
245
246         tipc_shutdown(port_ref);
247         tipc_deleteport(port_ref);
248
249         /* Destroy any existing subscriptions for subscriber */
250
251         list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
252                                  subscription_list) {
253                 if (sub->timeout != TIPC_WAIT_FOREVER) {
254                         k_cancel_timer(&sub->timer);
255                         k_term_timer(&sub->timer);
256                 }
257                 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
258                     sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
259                 subscr_del(sub);
260         }
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         /* Reclaim subscriber lock */
269
270         spin_lock_bh(subscriber->lock);
271
272         /* Now destroy subscriber */
273
274         kfree(subscriber);
275 }
276
277 /**
278  * subscr_cancel - handle subscription cancellation request
279  *
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.
283  *
284  * Note that fields of 's' use subscriber's endianness!
285  */
286
287 static void subscr_cancel(struct tipc_subscr *s,
288                           struct subscriber *subscriber)
289 {
290         struct subscription *sub;
291         struct subscription *sub_temp;
292         int found = 0;
293
294         /* Find first matching subscription, exit if not found */
295
296         list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
297                                  subscription_list) {
298                 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
299                         found = 1;
300                         break;
301                 }
302         }
303         if (!found)
304                 return;
305
306         /* Cancel subscription timer (if used), then delete subscription */
307
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);
314         }
315         dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
316             sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
317         subscr_del(sub);
318 }
319
320 /**
321  * subscr_subscribe - create subscription for subscriber
322  *
323  * Called with subscriber port locked.
324  */
325
326 static struct subscription *subscr_subscribe(struct tipc_subscr *s,
327                                              struct subscriber *subscriber)
328 {
329         struct subscription *sub;
330         int swap;
331
332         /* Determine subscriber's endianness */
333
334         swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
335
336         /* Detect & process a subscription cancellation request */
337
338         if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
339                 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
340                 subscr_cancel(s, subscriber);
341                 return NULL;
342         }
343
344         /* Refuse subscription if global limit exceeded */
345
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);
350                 return NULL;
351         }
352
353         /* Allocate subscription object */
354
355         sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
356         if (!sub) {
357                 warn("Subscription rejected, no memory\n");
358                 subscr_terminate(subscriber);
359                 return NULL;
360         }
361
362         /* Initialize subscription object */
363
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");
373                 kfree(sub);
374                 subscr_terminate(subscriber);
375                 return NULL;
376         }
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;
381         sub->swap = swap;
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);
388         }
389
390         return sub;
391 }
392
393 /**
394  * subscr_conn_shutdown_event - handle termination request from subscriber
395  *
396  * Called with subscriber's server port unlocked.
397  */
398
399 static void subscr_conn_shutdown_event(void *usr_handle,
400                                        u32 port_ref,
401                                        struct sk_buff **buf,
402                                        unsigned char const *data,
403                                        unsigned int size,
404                                        int reason)
405 {
406         struct subscriber *subscriber = usr_handle;
407         spinlock_t *subscriber_lock;
408
409         if (tipc_port_lock(port_ref) == NULL)
410                 return;
411
412         subscriber_lock = subscriber->lock;
413         subscr_terminate(subscriber);
414         spin_unlock_bh(subscriber_lock);
415 }
416
417 /**
418  * subscr_conn_msg_event - handle new subscription request from subscriber
419  *
420  * Called with subscriber's server port unlocked.
421  */
422
423 static void subscr_conn_msg_event(void *usr_handle,
424                                   u32 port_ref,
425                                   struct sk_buff **buf,
426                                   const unchar *data,
427                                   u32 size)
428 {
429         struct subscriber *subscriber = usr_handle;
430         spinlock_t *subscriber_lock;
431         struct subscription *sub;
432
433         /*
434          * Lock subscriber's server port (& make a local copy of lock pointer,
435          * in case subscriber is deleted while processing subscription request)
436          */
437
438         if (tipc_port_lock(port_ref) == NULL)
439                 return;
440
441         subscriber_lock = subscriber->lock;
442
443         if (size != sizeof(struct tipc_subscr)) {
444                 subscr_terminate(subscriber);
445                 spin_unlock_bh(subscriber_lock);
446         } else {
447                 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
448                 spin_unlock_bh(subscriber_lock);
449                 if (sub != NULL) {
450
451                         /*
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.
464                          */
465
466                         tipc_nametbl_subscribe(sub);
467                 }
468         }
469 }
470
471 /**
472  * subscr_named_msg_event - handle request to establish a new subscriber
473  */
474
475 static void subscr_named_msg_event(void *usr_handle,
476                                    u32 port_ref,
477                                    struct sk_buff **buf,
478                                    const unchar *data,
479                                    u32 size,
480                                    u32 importance,
481                                    struct tipc_portid const *orig,
482                                    struct tipc_name_seq const *dest)
483 {
484         static struct iovec msg_sect = {NULL, 0};
485
486         struct subscriber *subscriber;
487         u32 server_port_ref;
488
489         /* Create subscriber object */
490
491         subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
492         if (subscriber == NULL) {
493                 warn("Subscriber rejected, no memory\n");
494                 return;
495         }
496         INIT_LIST_HEAD(&subscriber->subscription_list);
497         INIT_LIST_HEAD(&subscriber->subscriber_list);
498
499         /* Create server port & establish connection to subscriber */
500
501         tipc_createport(topsrv.user_ref,
502                         subscriber,
503                         importance,
504                         NULL,
505                         NULL,
506                         subscr_conn_shutdown_event,
507                         NULL,
508                         NULL,
509                         subscr_conn_msg_event,
510                         NULL,
511                         &subscriber->port_ref);
512         if (subscriber->port_ref == 0) {
513                 warn("Subscriber rejected, unable to create port\n");
514                 kfree(subscriber);
515                 return;
516         }
517         tipc_connect2port(subscriber->port_ref, orig);
518
519         /* Lock server port (& save lock address for future use) */
520
521         subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
522
523         /* Add subscriber to topology server's subscriber list */
524
525         spin_lock_bh(&topsrv.lock);
526         list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
527         spin_unlock_bh(&topsrv.lock);
528
529         /* Unlock server port */
530
531         server_port_ref = subscriber->port_ref;
532         spin_unlock_bh(subscriber->lock);
533
534         /* Send an ACK- to complete connection handshaking */
535
536         tipc_send(server_port_ref, 1, &msg_sect);
537
538         /* Handle optional subscription request */
539
540         if (size != 0) {
541                 subscr_conn_msg_event(subscriber, server_port_ref,
542                                       buf, data, size);
543         }
544 }
545
546 int tipc_subscr_start(void)
547 {
548         struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
549         int res = -1;
550
551         memset(&topsrv, 0, sizeof (topsrv));
552         spin_lock_init(&topsrv.lock);
553         INIT_LIST_HEAD(&topsrv.subscriber_list);
554
555         spin_lock_bh(&topsrv.lock);
556         res = tipc_attach(&topsrv.user_ref, NULL, NULL);
557         if (res) {
558                 spin_unlock_bh(&topsrv.lock);
559                 return res;
560         }
561
562         res = tipc_createport(topsrv.user_ref,
563                               NULL,
564                               TIPC_CRITICAL_IMPORTANCE,
565                               NULL,
566                               NULL,
567                               NULL,
568                               NULL,
569                               subscr_named_msg_event,
570                               NULL,
571                               NULL,
572                               &topsrv.setup_port);
573         if (res)
574                 goto failed;
575
576         res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
577         if (res)
578                 goto failed;
579
580         spin_unlock_bh(&topsrv.lock);
581         return 0;
582
583 failed:
584         err("Failed to create subscription service\n");
585         tipc_detach(topsrv.user_ref);
586         topsrv.user_ref = 0;
587         spin_unlock_bh(&topsrv.lock);
588         return res;
589 }
590
591 void tipc_subscr_stop(void)
592 {
593         struct subscriber *subscriber;
594         struct subscriber *subscriber_temp;
595         spinlock_t *subscriber_lock;
596
597         if (topsrv.user_ref) {
598                 tipc_deleteport(topsrv.setup_port);
599                 list_for_each_entry_safe(subscriber, subscriber_temp,
600                                          &topsrv.subscriber_list,
601                                          subscriber_list) {
602                         subscriber_lock = subscriber->lock;
603                         spin_lock_bh(subscriber_lock);
604                         subscr_terminate(subscriber);
605                         spin_unlock_bh(subscriber_lock);
606                 }
607                 tipc_detach(topsrv.user_ref);
608                 topsrv.user_ref = 0;
609         }
610 }
611
612
613 int tipc_ispublished(struct tipc_name const *name)
614 {
615         u32 domain = 0;
616
617         return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
618 }
619