]> git.karo-electronics.de Git - mv-sheeva.git/blob - net/tipc/subscr.c
Merge branch 'master' of master.kernel.org:/pub/scm/linux/kernel/git/davem/net-2.6
[mv-sheeva.git] / net / tipc / subscr.c
1 /*
2  * net/tipc/subscr.c: TIPC network topology service
3  *
4  * Copyright (c) 2000-2006, Ericsson AB
5  * Copyright (c) 2005-2007, 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 "name_table.h"
39 #include "user_reg.h"
40 #include "subscr.h"
41
42 /**
43  * struct subscriber - TIPC network topology subscriber
44  * @port_ref: object reference to server port connecting to subscriber
45  * @lock: pointer to spinlock controlling access to subscriber's server port
46  * @subscriber_list: adjacent subscribers in top. server's list of subscribers
47  * @subscription_list: list of subscription objects for this subscriber
48  */
49
50 struct subscriber {
51         u32 port_ref;
52         spinlock_t *lock;
53         struct list_head subscriber_list;
54         struct list_head subscription_list;
55 };
56
57 /**
58  * struct top_srv - TIPC network topology subscription service
59  * @user_ref: TIPC userid of subscription service
60  * @setup_port: reference to TIPC port that handles subscription requests
61  * @subscription_count: number of active subscriptions (not subscribers!)
62  * @subscriber_list: list of ports subscribing to service
63  * @lock: spinlock govering access to subscriber list
64  */
65
66 struct top_srv {
67         u32 user_ref;
68         u32 setup_port;
69         atomic_t subscription_count;
70         struct list_head subscriber_list;
71         spinlock_t lock;
72 };
73
74 static struct top_srv topsrv = { 0 };
75
76 /**
77  * htohl - convert value to endianness used by destination
78  * @in: value to convert
79  * @swap: non-zero if endianness must be reversed
80  *
81  * Returns converted value
82  */
83
84 static u32 htohl(u32 in, int swap)
85 {
86         return swap ? swab32(in) : in;
87 }
88
89 /**
90  * subscr_send_event - send a message containing a tipc_event to the subscriber
91  *
92  * Note: Must not hold subscriber's server port lock, since tipc_send() will
93  *       try to take the lock if the message is rejected and returned!
94  */
95
96 static void subscr_send_event(struct subscription *sub,
97                               u32 found_lower,
98                               u32 found_upper,
99                               u32 event,
100                               u32 port_ref,
101                               u32 node)
102 {
103         struct iovec msg_sect;
104
105         msg_sect.iov_base = (void *)&sub->evt;
106         msg_sect.iov_len = sizeof(struct tipc_event);
107
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->server_ref, 1, &msg_sect);
114 }
115
116 /**
117  * tipc_subscr_overlap - test for subscription overlap with the given values
118  *
119  * Returns 1 if there is overlap, otherwise 0.
120  */
121
122 int tipc_subscr_overlap(struct subscription *sub,
123                         u32 found_lower,
124                         u32 found_upper)
125
126 {
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)
132                 return 0;
133         return 1;
134 }
135
136 /**
137  * tipc_subscr_report_overlap - issue event if there is subscription overlap
138  *
139  * Protected by nameseq.lock in name_table.c
140  */
141
142 void tipc_subscr_report_overlap(struct subscription *sub,
143                                 u32 found_lower,
144                                 u32 found_upper,
145                                 u32 event,
146                                 u32 port_ref,
147                                 u32 node,
148                                 int must)
149 {
150         if (!tipc_subscr_overlap(sub, found_lower, found_upper))
151                 return;
152         if (!must && !(sub->filter & TIPC_SUB_PORTS))
153                 return;
154
155         sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
156 }
157
158 /**
159  * subscr_timeout - subscription timeout has occurred
160  */
161
162 static void subscr_timeout(struct subscription *sub)
163 {
164         struct port *server_port;
165
166         /* Validate server port reference (in case subscriber is terminating) */
167
168         server_port = tipc_port_lock(sub->server_ref);
169         if (server_port == NULL)
170                 return;
171
172         /* Validate timeout (in case subscription is being cancelled) */
173
174         if (sub->timeout == TIPC_WAIT_FOREVER) {
175                 tipc_port_unlock(server_port);
176                 return;
177         }
178
179         /* Unlink subscription from name table */
180
181         tipc_nametbl_unsubscribe(sub);
182
183         /* Unlink subscription from subscriber */
184
185         list_del(&sub->subscription_list);
186
187         /* Release subscriber's server port */
188
189         tipc_port_unlock(server_port);
190
191         /* Notify subscriber of timeout */
192
193         subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
194                           TIPC_SUBSCR_TIMEOUT, 0, 0);
195
196         /* Now destroy subscription */
197
198         k_term_timer(&sub->timer);
199         kfree(sub);
200         atomic_dec(&topsrv.subscription_count);
201 }
202
203 /**
204  * subscr_del - delete a subscription within a subscription list
205  *
206  * Called with subscriber port locked.
207  */
208
209 static void subscr_del(struct subscription *sub)
210 {
211         tipc_nametbl_unsubscribe(sub);
212         list_del(&sub->subscription_list);
213         kfree(sub);
214         atomic_dec(&topsrv.subscription_count);
215 }
216
217 /**
218  * subscr_terminate - terminate communication with a subscriber
219  *
220  * Called with subscriber port locked.  Routine must temporarily release lock
221  * to enable subscription timeout routine(s) to finish without deadlocking;
222  * the lock is then reclaimed to allow caller to release it upon return.
223  * (This should work even in the unlikely event some other thread creates
224  * a new object reference in the interim that uses this lock; this routine will
225  * simply wait for it to be released, then claim it.)
226  */
227
228 static void subscr_terminate(struct subscriber *subscriber)
229 {
230         u32 port_ref;
231         struct subscription *sub;
232         struct subscription *sub_temp;
233
234         /* Invalidate subscriber reference */
235
236         port_ref = subscriber->port_ref;
237         subscriber->port_ref = 0;
238         spin_unlock_bh(subscriber->lock);
239
240         /* Sever connection to subscriber */
241
242         tipc_shutdown(port_ref);
243         tipc_deleteport(port_ref);
244
245         /* Destroy any existing subscriptions for subscriber */
246
247         list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
248                                  subscription_list) {
249                 if (sub->timeout != TIPC_WAIT_FOREVER) {
250                         k_cancel_timer(&sub->timer);
251                         k_term_timer(&sub->timer);
252                 }
253                 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
254                     sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
255                 subscr_del(sub);
256         }
257
258         /* Remove subscriber from topology server's subscriber list */
259
260         spin_lock_bh(&topsrv.lock);
261         list_del(&subscriber->subscriber_list);
262         spin_unlock_bh(&topsrv.lock);
263
264         /* Reclaim subscriber lock */
265
266         spin_lock_bh(subscriber->lock);
267
268         /* Now destroy subscriber */
269
270         kfree(subscriber);
271 }
272
273 /**
274  * subscr_cancel - handle subscription cancellation request
275  *
276  * Called with subscriber port locked.  Routine must temporarily release lock
277  * to enable the subscription timeout routine to finish without deadlocking;
278  * the lock is then reclaimed to allow caller to release it upon return.
279  *
280  * Note that fields of 's' use subscriber's endianness!
281  */
282
283 static void subscr_cancel(struct tipc_subscr *s,
284                           struct subscriber *subscriber)
285 {
286         struct subscription *sub;
287         struct subscription *sub_temp;
288         int found = 0;
289
290         /* Find first matching subscription, exit if not found */
291
292         list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
293                                  subscription_list) {
294                 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
295                         found = 1;
296                         break;
297                 }
298         }
299         if (!found)
300                 return;
301
302         /* Cancel subscription timer (if used), then delete subscription */
303
304         if (sub->timeout != TIPC_WAIT_FOREVER) {
305                 sub->timeout = TIPC_WAIT_FOREVER;
306                 spin_unlock_bh(subscriber->lock);
307                 k_cancel_timer(&sub->timer);
308                 k_term_timer(&sub->timer);
309                 spin_lock_bh(subscriber->lock);
310         }
311         dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
312             sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
313         subscr_del(sub);
314 }
315
316 /**
317  * subscr_subscribe - create subscription for subscriber
318  *
319  * Called with subscriber port locked.
320  */
321
322 static struct subscription *subscr_subscribe(struct tipc_subscr *s,
323                                              struct subscriber *subscriber)
324 {
325         struct subscription *sub;
326         int swap;
327
328         /* Determine subscriber's endianness */
329
330         swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
331
332         /* Detect & process a subscription cancellation request */
333
334         if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
335                 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
336                 subscr_cancel(s, subscriber);
337                 return NULL;
338         }
339
340         /* Refuse subscription if global limit exceeded */
341
342         if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
343                 warn("Subscription rejected, subscription limit reached (%u)\n",
344                      tipc_max_subscriptions);
345                 subscr_terminate(subscriber);
346                 return NULL;
347         }
348
349         /* Allocate subscription object */
350
351         sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
352         if (!sub) {
353                 warn("Subscription rejected, no memory\n");
354                 subscr_terminate(subscriber);
355                 return NULL;
356         }
357
358         /* Initialize subscription object */
359
360         sub->seq.type = htohl(s->seq.type, swap);
361         sub->seq.lower = htohl(s->seq.lower, swap);
362         sub->seq.upper = htohl(s->seq.upper, swap);
363         sub->timeout = htohl(s->timeout, swap);
364         sub->filter = htohl(s->filter, swap);
365         if ((!(sub->filter & TIPC_SUB_PORTS) ==
366              !(sub->filter & TIPC_SUB_SERVICE)) ||
367             (sub->seq.lower > sub->seq.upper)) {
368                 warn("Subscription rejected, illegal request\n");
369                 kfree(sub);
370                 subscr_terminate(subscriber);
371                 return NULL;
372         }
373         sub->event_cb = subscr_send_event;
374         INIT_LIST_HEAD(&sub->nameseq_list);
375         list_add(&sub->subscription_list, &subscriber->subscription_list);
376         sub->server_ref = subscriber->port_ref;
377         sub->swap = swap;
378         memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
379         atomic_inc(&topsrv.subscription_count);
380         if (sub->timeout != TIPC_WAIT_FOREVER) {
381                 k_init_timer(&sub->timer,
382                              (Handler)subscr_timeout, (unsigned long)sub);
383                 k_start_timer(&sub->timer, sub->timeout);
384         }
385
386         return sub;
387 }
388
389 /**
390  * subscr_conn_shutdown_event - handle termination request from subscriber
391  *
392  * Called with subscriber's server port unlocked.
393  */
394
395 static void subscr_conn_shutdown_event(void *usr_handle,
396                                        u32 port_ref,
397                                        struct sk_buff **buf,
398                                        unsigned char const *data,
399                                        unsigned int size,
400                                        int reason)
401 {
402         struct subscriber *subscriber = usr_handle;
403         spinlock_t *subscriber_lock;
404
405         if (tipc_port_lock(port_ref) == NULL)
406                 return;
407
408         subscriber_lock = subscriber->lock;
409         subscr_terminate(subscriber);
410         spin_unlock_bh(subscriber_lock);
411 }
412
413 /**
414  * subscr_conn_msg_event - handle new subscription request from subscriber
415  *
416  * Called with subscriber's server port unlocked.
417  */
418
419 static void subscr_conn_msg_event(void *usr_handle,
420                                   u32 port_ref,
421                                   struct sk_buff **buf,
422                                   const unchar *data,
423                                   u32 size)
424 {
425         struct subscriber *subscriber = usr_handle;
426         spinlock_t *subscriber_lock;
427         struct subscription *sub;
428
429         /*
430          * Lock subscriber's server port (& make a local copy of lock pointer,
431          * in case subscriber is deleted while processing subscription request)
432          */
433
434         if (tipc_port_lock(port_ref) == NULL)
435                 return;
436
437         subscriber_lock = subscriber->lock;
438
439         if (size != sizeof(struct tipc_subscr)) {
440                 subscr_terminate(subscriber);
441                 spin_unlock_bh(subscriber_lock);
442         } else {
443                 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
444                 spin_unlock_bh(subscriber_lock);
445                 if (sub != NULL) {
446
447                         /*
448                          * We must release the server port lock before adding a
449                          * subscription to the name table since TIPC needs to be
450                          * able to (re)acquire the port lock if an event message
451                          * issued by the subscription process is rejected and
452                          * returned.  The subscription cannot be deleted while
453                          * it is being added to the name table because:
454                          * a) the single-threading of the native API port code
455                          *    ensures the subscription cannot be cancelled and
456                          *    the subscriber connection cannot be broken, and
457                          * b) the name table lock ensures the subscription
458                          *    timeout code cannot delete the subscription,
459                          * so the subscription object is still protected.
460                          */
461
462                         tipc_nametbl_subscribe(sub);
463                 }
464         }
465 }
466
467 /**
468  * subscr_named_msg_event - handle request to establish a new subscriber
469  */
470
471 static void subscr_named_msg_event(void *usr_handle,
472                                    u32 port_ref,
473                                    struct sk_buff **buf,
474                                    const unchar *data,
475                                    u32 size,
476                                    u32 importance,
477                                    struct tipc_portid const *orig,
478                                    struct tipc_name_seq const *dest)
479 {
480         static struct iovec msg_sect = {NULL, 0};
481
482         struct subscriber *subscriber;
483         u32 server_port_ref;
484
485         /* Create subscriber object */
486
487         subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
488         if (subscriber == NULL) {
489                 warn("Subscriber rejected, no memory\n");
490                 return;
491         }
492         INIT_LIST_HEAD(&subscriber->subscription_list);
493         INIT_LIST_HEAD(&subscriber->subscriber_list);
494
495         /* Create server port & establish connection to subscriber */
496
497         tipc_createport(topsrv.user_ref,
498                         subscriber,
499                         importance,
500                         NULL,
501                         NULL,
502                         subscr_conn_shutdown_event,
503                         NULL,
504                         NULL,
505                         subscr_conn_msg_event,
506                         NULL,
507                         &subscriber->port_ref);
508         if (subscriber->port_ref == 0) {
509                 warn("Subscriber rejected, unable to create port\n");
510                 kfree(subscriber);
511                 return;
512         }
513         tipc_connect2port(subscriber->port_ref, orig);
514
515         /* Lock server port (& save lock address for future use) */
516
517         subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
518
519         /* Add subscriber to topology server's subscriber list */
520
521         spin_lock_bh(&topsrv.lock);
522         list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
523         spin_unlock_bh(&topsrv.lock);
524
525         /* Unlock server port */
526
527         server_port_ref = subscriber->port_ref;
528         spin_unlock_bh(subscriber->lock);
529
530         /* Send an ACK- to complete connection handshaking */
531
532         tipc_send(server_port_ref, 1, &msg_sect);
533
534         /* Handle optional subscription request */
535
536         if (size != 0) {
537                 subscr_conn_msg_event(subscriber, server_port_ref,
538                                       buf, data, size);
539         }
540 }
541
542 int tipc_subscr_start(void)
543 {
544         struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
545         int res;
546
547         memset(&topsrv, 0, sizeof (topsrv));
548         spin_lock_init(&topsrv.lock);
549         INIT_LIST_HEAD(&topsrv.subscriber_list);
550
551         spin_lock_bh(&topsrv.lock);
552         res = tipc_attach(&topsrv.user_ref);
553         if (res) {
554                 spin_unlock_bh(&topsrv.lock);
555                 return res;
556         }
557
558         res = tipc_createport(topsrv.user_ref,
559                               NULL,
560                               TIPC_CRITICAL_IMPORTANCE,
561                               NULL,
562                               NULL,
563                               NULL,
564                               NULL,
565                               subscr_named_msg_event,
566                               NULL,
567                               NULL,
568                               &topsrv.setup_port);
569         if (res)
570                 goto failed;
571
572         res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
573         if (res)
574                 goto failed;
575
576         spin_unlock_bh(&topsrv.lock);
577         return 0;
578
579 failed:
580         err("Failed to create subscription service\n");
581         tipc_detach(topsrv.user_ref);
582         topsrv.user_ref = 0;
583         spin_unlock_bh(&topsrv.lock);
584         return res;
585 }
586
587 void tipc_subscr_stop(void)
588 {
589         struct subscriber *subscriber;
590         struct subscriber *subscriber_temp;
591         spinlock_t *subscriber_lock;
592
593         if (topsrv.user_ref) {
594                 tipc_deleteport(topsrv.setup_port);
595                 list_for_each_entry_safe(subscriber, subscriber_temp,
596                                          &topsrv.subscriber_list,
597                                          subscriber_list) {
598                         subscriber_lock = subscriber->lock;
599                         spin_lock_bh(subscriber_lock);
600                         subscr_terminate(subscriber);
601                         spin_unlock_bh(subscriber_lock);
602                 }
603                 tipc_detach(topsrv.user_ref);
604                 topsrv.user_ref = 0;
605         }
606 }