2 * net/tipc/subscr.c: TIPC subscription service
4 * Copyright (c) 2003-2005, Ericsson Research Canada
5 * Copyright (c) 2005, Wind River Systems
6 * Copyright (c) 2005-2006, Ericsson AB
9 * Redistribution and use in source and binary forms, with or without
10 * modification, are permitted provided that the following conditions are met:
12 * Redistributions of source code must retain the above copyright notice, this
13 * list of conditions and the following disclaimer.
14 * Redistributions in binary form must reproduce the above copyright notice,
15 * this list of conditions and the following disclaimer in the documentation
16 * and/or other materials provided with the distribution.
17 * Neither the names of the copyright holders nor the names of its
18 * contributors may be used to endorse or promote products derived from this
19 * software without specific prior written permission.
21 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
22 * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
23 * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
24 * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
25 * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
26 * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
27 * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
28 * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
29 * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
30 * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
31 * POSSIBILITY OF SUCH DAMAGE.
37 #include "name_table.h"
41 * struct subscriber - TIPC network topology subscriber
42 * @ref: object reference to subscriber object itself
43 * @lock: pointer to spinlock controlling access to subscriber object
44 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
45 * @subscription_list: list of subscription objects for this subscriber
46 * @port_ref: object reference to port used to communicate with subscriber
47 * @swap: indicates if subscriber uses opposite endianness in its messages
53 struct list_head subscriber_list
;
54 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 inline u32
htohl(u32 in
, int swap
)
88 char *c
= (char *)&in
;
90 return swap
? ((c
[3] << 3) + (c
[2] << 2) + (c
[1] << 1) + c
[0]) : in
;
94 * subscr_send_event - send a message containing a tipc_event to the subscriber
97 static void subscr_send_event(struct subscription
*sub
,
104 struct iovec msg_sect
;
106 msg_sect
.iov_base
= (void *)&sub
->evt
;
107 msg_sect
.iov_len
= sizeof(struct tipc_event
);
109 sub
->evt
.event
= htohl(event
, sub
->owner
->swap
);
110 sub
->evt
.found_lower
= htohl(found_lower
, sub
->owner
->swap
);
111 sub
->evt
.found_upper
= htohl(found_upper
, sub
->owner
->swap
);
112 sub
->evt
.port
.ref
= htohl(port_ref
, sub
->owner
->swap
);
113 sub
->evt
.port
.node
= htohl(node
, sub
->owner
->swap
);
114 tipc_send(sub
->owner
->port_ref
, 1, &msg_sect
);
118 * subscr_overlap - test for subscription overlap with the given values
120 * Returns 1 if there is overlap, otherwise 0.
123 int subscr_overlap(struct subscription
*sub
,
128 if (found_lower
< sub
->seq
.lower
)
129 found_lower
= sub
->seq
.lower
;
130 if (found_upper
> sub
->seq
.upper
)
131 found_upper
= sub
->seq
.upper
;
132 if (found_lower
> found_upper
)
138 * subscr_report_overlap - issue event if there is subscription overlap
140 * Protected by nameseq.lock in name_table.c
143 void subscr_report_overlap(struct subscription
*sub
,
151 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub
->seq
.type
, sub
->seq
.lower
,
152 sub
->seq
.upper
, found_lower
, found_upper
);
153 if (!subscr_overlap(sub
, found_lower
, found_upper
))
155 if (!must
&& (sub
->filter
!= TIPC_SUB_PORTS
))
157 subscr_send_event(sub
, found_lower
, found_upper
, event
, port_ref
, node
);
161 * subscr_timeout - subscription timeout has occurred
164 static void subscr_timeout(struct subscription
*sub
)
166 struct subscriber
*subscriber
;
169 /* Validate subscriber reference (in case subscriber is terminating) */
171 subscriber_ref
= sub
->owner
->ref
;
172 subscriber
= (struct subscriber
*)ref_lock(subscriber_ref
);
173 if (subscriber
== NULL
)
176 /* Unlink subscription from name table */
178 nametbl_unsubscribe(sub
);
180 /* Notify subscriber of timeout, then unlink subscription */
182 subscr_send_event(sub
,
183 sub
->evt
.s
.seq
.lower
,
184 sub
->evt
.s
.seq
.upper
,
188 list_del(&sub
->subscription_list
);
190 /* Now destroy subscription */
192 ref_unlock(subscriber_ref
);
193 k_term_timer(&sub
->timer
);
195 atomic_dec(&topsrv
.subscription_count
);
199 * subscr_terminate - terminate communication with a subscriber
201 * Called with subscriber locked. Routine must temporarily release this lock
202 * to enable subscription timeout routine(s) to finish without deadlocking;
203 * the lock is then reclaimed to allow caller to release it upon return.
204 * (This should work even in the unlikely event some other thread creates
205 * a new object reference in the interim that uses this lock; this routine will
206 * simply wait for it to be released, then claim it.)
209 static void subscr_terminate(struct subscriber
*subscriber
)
211 struct subscription
*sub
;
212 struct subscription
*sub_temp
;
214 /* Invalidate subscriber reference */
216 ref_discard(subscriber
->ref
);
217 spin_unlock_bh(subscriber
->lock
);
219 /* Destroy any existing subscriptions for subscriber */
221 list_for_each_entry_safe(sub
, sub_temp
, &subscriber
->subscription_list
,
223 if (sub
->timeout
!= TIPC_WAIT_FOREVER
) {
224 k_cancel_timer(&sub
->timer
);
225 k_term_timer(&sub
->timer
);
227 nametbl_unsubscribe(sub
);
228 list_del(&sub
->subscription_list
);
229 dbg("Term: Removed sub %u,%u,%u from subscriber %x list\n",
230 sub
->seq
.type
, sub
->seq
.lower
, sub
->seq
.upper
, subscriber
);
232 atomic_dec(&topsrv
.subscription_count
);
235 /* Sever connection to subscriber */
237 tipc_shutdown(subscriber
->port_ref
);
238 tipc_deleteport(subscriber
->port_ref
);
240 /* Remove subscriber from topology server's subscriber list */
242 spin_lock_bh(&topsrv
.lock
);
243 list_del(&subscriber
->subscriber_list
);
244 spin_unlock_bh(&topsrv
.lock
);
246 /* Now destroy subscriber */
248 spin_lock_bh(subscriber
->lock
);
253 * subscr_subscribe - create subscription for subscriber
255 * Called with subscriber locked
258 static void subscr_subscribe(struct tipc_subscr
*s
,
259 struct subscriber
*subscriber
)
261 struct subscription
*sub
;
263 /* Refuse subscription if global limit exceeded */
265 if (atomic_read(&topsrv
.subscription_count
) >= tipc_max_subscriptions
) {
266 warn("Failed: max %u subscriptions\n", tipc_max_subscriptions
);
267 subscr_terminate(subscriber
);
271 /* Allocate subscription object */
273 sub
= kmalloc(sizeof(*sub
), GFP_ATOMIC
);
275 warn("Memory squeeze; ignoring subscription\n");
276 subscr_terminate(subscriber
);
280 /* Determine/update subscriber's endianness */
282 if ((s
->filter
== TIPC_SUB_PORTS
) || (s
->filter
== TIPC_SUB_SERVICE
))
283 subscriber
->swap
= 0;
285 subscriber
->swap
= 1;
287 /* Initialize subscription object */
289 memset(sub
, 0, sizeof(*sub
));
290 sub
->seq
.type
= htohl(s
->seq
.type
, subscriber
->swap
);
291 sub
->seq
.lower
= htohl(s
->seq
.lower
, subscriber
->swap
);
292 sub
->seq
.upper
= htohl(s
->seq
.upper
, subscriber
->swap
);
293 sub
->timeout
= htohl(s
->timeout
, subscriber
->swap
);
294 sub
->filter
= htohl(s
->filter
, subscriber
->swap
);
295 if ((((sub
->filter
!= TIPC_SUB_PORTS
)
296 && (sub
->filter
!= TIPC_SUB_SERVICE
)))
297 || (sub
->seq
.lower
> sub
->seq
.upper
)) {
298 warn("Rejecting illegal subscription %u,%u,%u\n",
299 sub
->seq
.type
, sub
->seq
.lower
, sub
->seq
.upper
);
301 subscr_terminate(subscriber
);
304 memcpy(&sub
->evt
.s
, s
, sizeof(struct tipc_subscr
));
305 INIT_LIST_HEAD(&sub
->subscription_list
);
306 INIT_LIST_HEAD(&sub
->nameseq_list
);
307 list_add(&sub
->subscription_list
, &subscriber
->subscription_list
);
308 atomic_inc(&topsrv
.subscription_count
);
309 if (sub
->timeout
!= TIPC_WAIT_FOREVER
) {
310 k_init_timer(&sub
->timer
,
311 (Handler
)subscr_timeout
, (unsigned long)sub
);
312 k_start_timer(&sub
->timer
, sub
->timeout
);
314 sub
->owner
= subscriber
;
315 nametbl_subscribe(sub
);
319 * subscr_conn_shutdown_event - handle termination request from subscriber
322 static void subscr_conn_shutdown_event(void *usr_handle
,
324 struct sk_buff
**buf
,
325 unsigned char const *data
,
329 struct subscriber
*subscriber
= ref_lock((u32
)usr_handle
);
330 spinlock_t
*subscriber_lock
;
332 if (subscriber
== NULL
)
335 subscriber_lock
= subscriber
->lock
;
336 subscr_terminate(subscriber
);
337 spin_unlock_bh(subscriber_lock
);
341 * subscr_conn_msg_event - handle new subscription request from subscriber
344 static void subscr_conn_msg_event(void *usr_handle
,
346 struct sk_buff
**buf
,
350 struct subscriber
*subscriber
= ref_lock((u32
)usr_handle
);
351 spinlock_t
*subscriber_lock
;
353 if (subscriber
== NULL
)
356 subscriber_lock
= subscriber
->lock
;
357 if (size
!= sizeof(struct tipc_subscr
))
358 subscr_terminate(subscriber
);
360 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
362 spin_unlock_bh(subscriber_lock
);
366 * subscr_named_msg_event - handle request to establish a new subscriber
369 static void subscr_named_msg_event(void *usr_handle
,
371 struct sk_buff
**buf
,
375 struct tipc_portid
const *orig
,
376 struct tipc_name_seq
const *dest
)
378 struct subscriber
*subscriber
;
379 struct iovec msg_sect
= {0, 0};
380 spinlock_t
*subscriber_lock
;
382 dbg("subscr_named_msg_event: orig = %x own = %x,\n",
383 orig
->node
, tipc_own_addr
);
384 if (size
&& (size
!= sizeof(struct tipc_subscr
))) {
385 warn("Received tipc_subscr of invalid size\n");
389 /* Create subscriber object */
391 subscriber
= kmalloc(sizeof(struct subscriber
), GFP_ATOMIC
);
392 if (subscriber
== NULL
) {
393 warn("Memory squeeze; ignoring subscriber setup\n");
396 memset(subscriber
, 0, sizeof(struct subscriber
));
397 INIT_LIST_HEAD(&subscriber
->subscription_list
);
398 INIT_LIST_HEAD(&subscriber
->subscriber_list
);
399 subscriber
->ref
= ref_acquire(subscriber
, &subscriber
->lock
);
400 if (subscriber
->ref
== 0) {
401 warn("Failed to acquire subscriber reference\n");
406 /* Establish a connection to subscriber */
408 tipc_createport(topsrv
.user_ref
,
409 (void *)subscriber
->ref
,
413 subscr_conn_shutdown_event
,
416 subscr_conn_msg_event
,
418 &subscriber
->port_ref
);
419 if (subscriber
->port_ref
== 0) {
420 warn("Memory squeeze; failed to create subscription port\n");
421 ref_discard(subscriber
->ref
);
425 tipc_connect2port(subscriber
->port_ref
, orig
);
428 /* Add subscriber to topology server's subscriber list */
430 ref_lock(subscriber
->ref
);
431 spin_lock_bh(&topsrv
.lock
);
432 list_add(&subscriber
->subscriber_list
, &topsrv
.subscriber_list
);
433 spin_unlock_bh(&topsrv
.lock
);
436 * Subscribe now if message contains a subscription,
437 * otherwise send an empty response to complete connection handshaking
440 subscriber_lock
= subscriber
->lock
;
442 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
444 tipc_send(subscriber
->port_ref
, 1, &msg_sect
);
446 spin_unlock_bh(subscriber_lock
);
449 int subscr_start(void)
451 struct tipc_name_seq seq
= {TIPC_TOP_SRV
, TIPC_TOP_SRV
, TIPC_TOP_SRV
};
454 memset(&topsrv
, 0, sizeof (topsrv
));
455 topsrv
.lock
= SPIN_LOCK_UNLOCKED
;
456 INIT_LIST_HEAD(&topsrv
.subscriber_list
);
458 spin_lock_bh(&topsrv
.lock
);
459 res
= tipc_attach(&topsrv
.user_ref
, 0, 0);
461 spin_unlock_bh(&topsrv
.lock
);
465 res
= tipc_createport(topsrv
.user_ref
,
467 TIPC_CRITICAL_IMPORTANCE
,
472 subscr_named_msg_event
,
479 res
= nametbl_publish_rsv(topsrv
.setup_port
, TIPC_NODE_SCOPE
, &seq
);
483 spin_unlock_bh(&topsrv
.lock
);
487 err("Unable to create subscription service\n");
488 tipc_detach(topsrv
.user_ref
);
490 spin_unlock_bh(&topsrv
.lock
);
494 void subscr_stop(void)
496 struct subscriber
*subscriber
;
497 struct subscriber
*subscriber_temp
;
498 spinlock_t
*subscriber_lock
;
500 if (topsrv
.user_ref
) {
501 tipc_deleteport(topsrv
.setup_port
);
502 list_for_each_entry_safe(subscriber
, subscriber_temp
,
503 &topsrv
.subscriber_list
,
505 ref_lock(subscriber
->ref
);
506 subscriber_lock
= subscriber
->lock
;
507 subscr_terminate(subscriber
);
508 spin_unlock_bh(subscriber_lock
);
510 tipc_detach(topsrv
.user_ref
);
516 int tipc_ispublished(struct tipc_name
const *name
)
520 return(nametbl_translate(name
->type
, name
->instance
,&domain
) != 0);