blob: b52959fca5e631ca39383e44d9ccba1017b83d13 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/subscr.c: TIPC subscription service
3 *
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
7 * All rights reserved.
8 *
Per Liden9ea1fd32006-01-11 13:30:43 +01009 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +010010 * modification, are permitted provided that the following conditions are met:
11 *
Per Liden9ea1fd32006-01-11 13:30:43 +010012 * 1. Redistributions of source code must retain the above copyright
13 * notice, this list of conditions and the following disclaimer.
14 * 2. Redistributions in binary form must reproduce the above copyright
15 * notice, this list of conditions and the following disclaimer in the
16 * documentation and/or other materials provided with the distribution.
17 * 3. Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from
19 * this software without specific prior written permission.
Per Lidenb97bf3f2006-01-02 19:04:38 +010020 *
Per Liden9ea1fd32006-01-11 13:30:43 +010021 * Alternatively, this software may be distributed under the terms of the
22 * GNU General Public License ("GPL") version 2 as published by the Free
23 * Software Foundation.
24 *
25 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
26 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
27 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
28 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
29 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
30 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
31 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
32 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
33 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
34 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
Per Lidenb97bf3f2006-01-02 19:04:38 +010035 * POSSIBILITY OF SUCH DAMAGE.
36 */
37
38#include "core.h"
39#include "dbg.h"
40#include "subscr.h"
41#include "name_table.h"
42#include "ref.h"
43
44/**
45 * struct subscriber - TIPC network topology subscriber
46 * @ref: object reference to subscriber object itself
47 * @lock: pointer to spinlock controlling access to subscriber object
48 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
49 * @subscription_list: list of subscription objects for this subscriber
50 * @port_ref: object reference to port used to communicate with subscriber
51 * @swap: indicates if subscriber uses opposite endianness in its messages
52 */
53
54struct subscriber {
55 u32 ref;
56 spinlock_t *lock;
57 struct list_head subscriber_list;
58 struct list_head subscription_list;
59 u32 port_ref;
60 int swap;
61};
62
63/**
64 * struct top_srv - TIPC network topology subscription service
65 * @user_ref: TIPC userid of subscription service
66 * @setup_port: reference to TIPC port that handles subscription requests
67 * @subscription_count: number of active subscriptions (not subscribers!)
68 * @subscriber_list: list of ports subscribing to service
69 * @lock: spinlock govering access to subscriber list
70 */
71
72struct top_srv {
73 u32 user_ref;
74 u32 setup_port;
75 atomic_t subscription_count;
76 struct list_head subscriber_list;
77 spinlock_t lock;
78};
79
80static struct top_srv topsrv = { 0 };
81
82/**
83 * htohl - convert value to endianness used by destination
84 * @in: value to convert
85 * @swap: non-zero if endianness must be reversed
86 *
87 * Returns converted value
88 */
89
90static inline u32 htohl(u32 in, int swap)
91{
92 char *c = (char *)∈
93
94 return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in;
95}
96
97/**
98 * subscr_send_event - send a message containing a tipc_event to the subscriber
99 */
100
101static void subscr_send_event(struct subscription *sub,
102 u32 found_lower,
103 u32 found_upper,
104 u32 event,
105 u32 port_ref,
106 u32 node)
107{
108 struct iovec msg_sect;
109
110 msg_sect.iov_base = (void *)&sub->evt;
111 msg_sect.iov_len = sizeof(struct tipc_event);
112
113 sub->evt.event = htohl(event, sub->owner->swap);
114 sub->evt.found_lower = htohl(found_lower, sub->owner->swap);
115 sub->evt.found_upper = htohl(found_upper, sub->owner->swap);
116 sub->evt.port.ref = htohl(port_ref, sub->owner->swap);
117 sub->evt.port.node = htohl(node, sub->owner->swap);
118 tipc_send(sub->owner->port_ref, 1, &msg_sect);
119}
120
121/**
122 * subscr_overlap - test for subscription overlap with the given values
123 *
124 * Returns 1 if there is overlap, otherwise 0.
125 */
126
127int subscr_overlap(struct subscription *sub,
128 u32 found_lower,
129 u32 found_upper)
130
131{
132 if (found_lower < sub->seq.lower)
133 found_lower = sub->seq.lower;
134 if (found_upper > sub->seq.upper)
135 found_upper = sub->seq.upper;
136 if (found_lower > found_upper)
137 return 0;
138 return 1;
139}
140
141/**
142 * subscr_report_overlap - issue event if there is subscription overlap
143 *
144 * Protected by nameseq.lock in name_table.c
145 */
146
147void subscr_report_overlap(struct subscription *sub,
148 u32 found_lower,
149 u32 found_upper,
150 u32 event,
151 u32 port_ref,
152 u32 node,
153 int must)
154{
155 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
156 sub->seq.upper, found_lower, found_upper);
157 if (!subscr_overlap(sub, found_lower, found_upper))
158 return;
159 if (!must && (sub->filter != TIPC_SUB_PORTS))
160 return;
161 subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
162}
163
164/**
165 * subscr_timeout - subscription timeout has occurred
166 */
167
168static void subscr_timeout(struct subscription *sub)
169{
170 struct subscriber *subscriber;
171 u32 subscriber_ref;
172
173 /* Validate subscriber reference (in case subscriber is terminating) */
174
175 subscriber_ref = sub->owner->ref;
176 subscriber = (struct subscriber *)ref_lock(subscriber_ref);
177 if (subscriber == NULL)
178 return;
179
180 /* Unlink subscription from name table */
181
182 nametbl_unsubscribe(sub);
183
184 /* Notify subscriber of timeout, then unlink subscription */
185
186 subscr_send_event(sub,
187 sub->evt.s.seq.lower,
188 sub->evt.s.seq.upper,
189 TIPC_SUBSCR_TIMEOUT,
190 0,
191 0);
192 list_del(&sub->subscription_list);
193
194 /* Now destroy subscription */
195
196 ref_unlock(subscriber_ref);
197 k_term_timer(&sub->timer);
198 kfree(sub);
199 atomic_dec(&topsrv.subscription_count);
200}
201
202/**
203 * subscr_terminate - terminate communication with a subscriber
204 *
205 * Called with subscriber locked. Routine must temporarily release this lock
206 * to enable subscription timeout routine(s) to finish without deadlocking;
207 * the lock is then reclaimed to allow caller to release it upon return.
208 * (This should work even in the unlikely event some other thread creates
209 * a new object reference in the interim that uses this lock; this routine will
210 * simply wait for it to be released, then claim it.)
211 */
212
213static void subscr_terminate(struct subscriber *subscriber)
214{
215 struct subscription *sub;
216 struct subscription *sub_temp;
217
218 /* Invalidate subscriber reference */
219
220 ref_discard(subscriber->ref);
221 spin_unlock_bh(subscriber->lock);
222
223 /* Destroy any existing subscriptions for subscriber */
224
225 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
226 subscription_list) {
227 if (sub->timeout != TIPC_WAIT_FOREVER) {
228 k_cancel_timer(&sub->timer);
229 k_term_timer(&sub->timer);
230 }
231 nametbl_unsubscribe(sub);
232 list_del(&sub->subscription_list);
233 dbg("Term: Removed sub %u,%u,%u from subscriber %x list\n",
234 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
235 kfree(sub);
236 atomic_dec(&topsrv.subscription_count);
237 }
238
239 /* Sever connection to subscriber */
240
241 tipc_shutdown(subscriber->port_ref);
242 tipc_deleteport(subscriber->port_ref);
243
244 /* Remove subscriber from topology server's subscriber list */
245
246 spin_lock_bh(&topsrv.lock);
247 list_del(&subscriber->subscriber_list);
248 spin_unlock_bh(&topsrv.lock);
249
250 /* Now destroy subscriber */
251
252 spin_lock_bh(subscriber->lock);
253 kfree(subscriber);
254}
255
256/**
257 * subscr_subscribe - create subscription for subscriber
258 *
259 * Called with subscriber locked
260 */
261
262static void subscr_subscribe(struct tipc_subscr *s,
263 struct subscriber *subscriber)
264{
265 struct subscription *sub;
266
267 /* Refuse subscription if global limit exceeded */
268
269 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
270 warn("Failed: max %u subscriptions\n", tipc_max_subscriptions);
271 subscr_terminate(subscriber);
272 return;
273 }
274
275 /* Allocate subscription object */
276
277 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
278 if (sub == NULL) {
279 warn("Memory squeeze; ignoring subscription\n");
280 subscr_terminate(subscriber);
281 return;
282 }
283
284 /* Determine/update subscriber's endianness */
285
286 if ((s->filter == TIPC_SUB_PORTS) || (s->filter == TIPC_SUB_SERVICE))
287 subscriber->swap = 0;
288 else
289 subscriber->swap = 1;
290
291 /* Initialize subscription object */
292
293 memset(sub, 0, sizeof(*sub));
294 sub->seq.type = htohl(s->seq.type, subscriber->swap);
295 sub->seq.lower = htohl(s->seq.lower, subscriber->swap);
296 sub->seq.upper = htohl(s->seq.upper, subscriber->swap);
297 sub->timeout = htohl(s->timeout, subscriber->swap);
298 sub->filter = htohl(s->filter, subscriber->swap);
299 if ((((sub->filter != TIPC_SUB_PORTS)
300 && (sub->filter != TIPC_SUB_SERVICE)))
301 || (sub->seq.lower > sub->seq.upper)) {
302 warn("Rejecting illegal subscription %u,%u,%u\n",
303 sub->seq.type, sub->seq.lower, sub->seq.upper);
304 kfree(sub);
305 subscr_terminate(subscriber);
306 return;
307 }
308 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
309 INIT_LIST_HEAD(&sub->subscription_list);
310 INIT_LIST_HEAD(&sub->nameseq_list);
311 list_add(&sub->subscription_list, &subscriber->subscription_list);
312 atomic_inc(&topsrv.subscription_count);
313 if (sub->timeout != TIPC_WAIT_FOREVER) {
314 k_init_timer(&sub->timer,
315 (Handler)subscr_timeout, (unsigned long)sub);
316 k_start_timer(&sub->timer, sub->timeout);
317 }
318 sub->owner = subscriber;
319 nametbl_subscribe(sub);
320}
321
322/**
323 * subscr_conn_shutdown_event - handle termination request from subscriber
324 */
325
326static void subscr_conn_shutdown_event(void *usr_handle,
327 u32 portref,
328 struct sk_buff **buf,
329 unsigned char const *data,
330 unsigned int size,
331 int reason)
332{
333 struct subscriber *subscriber = ref_lock((u32)usr_handle);
334 spinlock_t *subscriber_lock;
335
336 if (subscriber == NULL)
337 return;
338
339 subscriber_lock = subscriber->lock;
340 subscr_terminate(subscriber);
341 spin_unlock_bh(subscriber_lock);
342}
343
344/**
345 * subscr_conn_msg_event - handle new subscription request from subscriber
346 */
347
348static void subscr_conn_msg_event(void *usr_handle,
349 u32 port_ref,
350 struct sk_buff **buf,
351 const unchar *data,
352 u32 size)
353{
354 struct subscriber *subscriber = ref_lock((u32)usr_handle);
355 spinlock_t *subscriber_lock;
356
357 if (subscriber == NULL)
358 return;
359
360 subscriber_lock = subscriber->lock;
361 if (size != sizeof(struct tipc_subscr))
362 subscr_terminate(subscriber);
363 else
364 subscr_subscribe((struct tipc_subscr *)data, subscriber);
365
366 spin_unlock_bh(subscriber_lock);
367}
368
369/**
370 * subscr_named_msg_event - handle request to establish a new subscriber
371 */
372
373static void subscr_named_msg_event(void *usr_handle,
374 u32 port_ref,
375 struct sk_buff **buf,
376 const unchar *data,
377 u32 size,
378 u32 importance,
379 struct tipc_portid const *orig,
380 struct tipc_name_seq const *dest)
381{
382 struct subscriber *subscriber;
383 struct iovec msg_sect = {0, 0};
384 spinlock_t *subscriber_lock;
385
386 dbg("subscr_named_msg_event: orig = %x own = %x,\n",
387 orig->node, tipc_own_addr);
388 if (size && (size != sizeof(struct tipc_subscr))) {
389 warn("Received tipc_subscr of invalid size\n");
390 return;
391 }
392
393 /* Create subscriber object */
394
395 subscriber = kmalloc(sizeof(struct subscriber), GFP_ATOMIC);
396 if (subscriber == NULL) {
397 warn("Memory squeeze; ignoring subscriber setup\n");
398 return;
399 }
400 memset(subscriber, 0, sizeof(struct subscriber));
401 INIT_LIST_HEAD(&subscriber->subscription_list);
402 INIT_LIST_HEAD(&subscriber->subscriber_list);
403 subscriber->ref = ref_acquire(subscriber, &subscriber->lock);
404 if (subscriber->ref == 0) {
405 warn("Failed to acquire subscriber reference\n");
406 kfree(subscriber);
407 return;
408 }
409
410 /* Establish a connection to subscriber */
411
412 tipc_createport(topsrv.user_ref,
413 (void *)subscriber->ref,
414 importance,
415 0,
416 0,
417 subscr_conn_shutdown_event,
418 0,
419 0,
420 subscr_conn_msg_event,
421 0,
422 &subscriber->port_ref);
423 if (subscriber->port_ref == 0) {
424 warn("Memory squeeze; failed to create subscription port\n");
425 ref_discard(subscriber->ref);
426 kfree(subscriber);
427 return;
428 }
429 tipc_connect2port(subscriber->port_ref, orig);
430
431
432 /* Add subscriber to topology server's subscriber list */
433
434 ref_lock(subscriber->ref);
435 spin_lock_bh(&topsrv.lock);
436 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
437 spin_unlock_bh(&topsrv.lock);
438
439 /*
440 * Subscribe now if message contains a subscription,
441 * otherwise send an empty response to complete connection handshaking
442 */
443
444 subscriber_lock = subscriber->lock;
445 if (size)
446 subscr_subscribe((struct tipc_subscr *)data, subscriber);
447 else
448 tipc_send(subscriber->port_ref, 1, &msg_sect);
449
450 spin_unlock_bh(subscriber_lock);
451}
452
453int subscr_start(void)
454{
455 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
456 int res = -1;
457
458 memset(&topsrv, 0, sizeof (topsrv));
459 topsrv.lock = SPIN_LOCK_UNLOCKED;
460 INIT_LIST_HEAD(&topsrv.subscriber_list);
461
462 spin_lock_bh(&topsrv.lock);
463 res = tipc_attach(&topsrv.user_ref, 0, 0);
464 if (res) {
465 spin_unlock_bh(&topsrv.lock);
466 return res;
467 }
468
469 res = tipc_createport(topsrv.user_ref,
470 0,
471 TIPC_CRITICAL_IMPORTANCE,
472 0,
473 0,
474 0,
475 0,
476 subscr_named_msg_event,
477 0,
478 0,
479 &topsrv.setup_port);
480 if (res)
481 goto failed;
482
483 res = nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
484 if (res)
485 goto failed;
486
487 spin_unlock_bh(&topsrv.lock);
488 return 0;
489
490failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100491 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 tipc_detach(topsrv.user_ref);
493 topsrv.user_ref = 0;
494 spin_unlock_bh(&topsrv.lock);
495 return res;
496}
497
498void subscr_stop(void)
499{
500 struct subscriber *subscriber;
501 struct subscriber *subscriber_temp;
502 spinlock_t *subscriber_lock;
503
504 if (topsrv.user_ref) {
505 tipc_deleteport(topsrv.setup_port);
506 list_for_each_entry_safe(subscriber, subscriber_temp,
507 &topsrv.subscriber_list,
508 subscriber_list) {
509 ref_lock(subscriber->ref);
510 subscriber_lock = subscriber->lock;
511 subscr_terminate(subscriber);
512 spin_unlock_bh(subscriber_lock);
513 }
514 tipc_detach(topsrv.user_ref);
515 topsrv.user_ref = 0;
516 }
517}
518
519
520int tipc_ispublished(struct tipc_name const *name)
521{
522 u32 domain = 0;
523
524 return(nametbl_translate(name->type, name->instance,&domain) != 0);
525}
526