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 * 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.
21 * 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.
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
35 * POSSIBILITY OF SUCH DAMAGE.
41 #include "name_table.h"
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
57 struct list_head subscriber_list
;
58 struct list_head subscription_list
;
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
75 atomic_t subscription_count
;
76 struct list_head subscriber_list
;
80 static struct top_srv topsrv
= { 0 };
83 * htohl - convert value to endianness used by destination
84 * @in: value to convert
85 * @swap: non-zero if endianness must be reversed
87 * Returns converted value
90 static inline u32
htohl(u32 in
, int swap
)
92 char *c
= (char *)&in
;
94 return swap
? ((c
[3] << 3) + (c
[2] << 2) + (c
[1] << 1) + c
[0]) : in
;
98 * subscr_send_event - send a message containing a tipc_event to the subscriber
101 static void subscr_send_event(struct subscription
*sub
,
108 struct iovec msg_sect
;
110 msg_sect
.iov_base
= (void *)&sub
->evt
;
111 msg_sect
.iov_len
= sizeof(struct tipc_event
);
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
);
122 * subscr_overlap - test for subscription overlap with the given values
124 * Returns 1 if there is overlap, otherwise 0.
127 int subscr_overlap(struct subscription
*sub
,
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
)
142 * subscr_report_overlap - issue event if there is subscription overlap
144 * Protected by nameseq.lock in name_table.c
147 void subscr_report_overlap(struct subscription
*sub
,
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
))
159 if (!must
&& (sub
->filter
!= TIPC_SUB_PORTS
))
161 subscr_send_event(sub
, found_lower
, found_upper
, event
, port_ref
, node
);
165 * subscr_timeout - subscription timeout has occurred
168 static void subscr_timeout(struct subscription
*sub
)
170 struct subscriber
*subscriber
;
173 /* Validate subscriber reference (in case subscriber is terminating) */
175 subscriber_ref
= sub
->owner
->ref
;
176 subscriber
= (struct subscriber
*)ref_lock(subscriber_ref
);
177 if (subscriber
== NULL
)
180 /* Unlink subscription from name table */
182 nametbl_unsubscribe(sub
);
184 /* Notify subscriber of timeout, then unlink subscription */
186 subscr_send_event(sub
,
187 sub
->evt
.s
.seq
.lower
,
188 sub
->evt
.s
.seq
.upper
,
192 list_del(&sub
->subscription_list
);
194 /* Now destroy subscription */
196 ref_unlock(subscriber_ref
);
197 k_term_timer(&sub
->timer
);
199 atomic_dec(&topsrv
.subscription_count
);
203 * subscr_terminate - terminate communication with a subscriber
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.)
213 static void subscr_terminate(struct subscriber
*subscriber
)
215 struct subscription
*sub
;
216 struct subscription
*sub_temp
;
218 /* Invalidate subscriber reference */
220 ref_discard(subscriber
->ref
);
221 spin_unlock_bh(subscriber
->lock
);
223 /* Destroy any existing subscriptions for subscriber */
225 list_for_each_entry_safe(sub
, sub_temp
, &subscriber
->subscription_list
,
227 if (sub
->timeout
!= TIPC_WAIT_FOREVER
) {
228 k_cancel_timer(&sub
->timer
);
229 k_term_timer(&sub
->timer
);
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
);
236 atomic_dec(&topsrv
.subscription_count
);
239 /* Sever connection to subscriber */
241 tipc_shutdown(subscriber
->port_ref
);
242 tipc_deleteport(subscriber
->port_ref
);
244 /* Remove subscriber from topology server's subscriber list */
246 spin_lock_bh(&topsrv
.lock
);
247 list_del(&subscriber
->subscriber_list
);
248 spin_unlock_bh(&topsrv
.lock
);
250 /* Now destroy subscriber */
252 spin_lock_bh(subscriber
->lock
);
257 * subscr_subscribe - create subscription for subscriber
259 * Called with subscriber locked
262 static void subscr_subscribe(struct tipc_subscr
*s
,
263 struct subscriber
*subscriber
)
265 struct subscription
*sub
;
267 /* Refuse subscription if global limit exceeded */
269 if (atomic_read(&topsrv
.subscription_count
) >= tipc_max_subscriptions
) {
270 warn("Failed: max %u subscriptions\n", tipc_max_subscriptions
);
271 subscr_terminate(subscriber
);
275 /* Allocate subscription object */
277 sub
= kmalloc(sizeof(*sub
), GFP_ATOMIC
);
279 warn("Memory squeeze; ignoring subscription\n");
280 subscr_terminate(subscriber
);
284 /* Determine/update subscriber's endianness */
286 if ((s
->filter
== TIPC_SUB_PORTS
) || (s
->filter
== TIPC_SUB_SERVICE
))
287 subscriber
->swap
= 0;
289 subscriber
->swap
= 1;
291 /* Initialize subscription object */
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
);
305 subscr_terminate(subscriber
);
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
);
318 sub
->owner
= subscriber
;
319 nametbl_subscribe(sub
);
323 * subscr_conn_shutdown_event - handle termination request from subscriber
326 static void subscr_conn_shutdown_event(void *usr_handle
,
328 struct sk_buff
**buf
,
329 unsigned char const *data
,
333 struct subscriber
*subscriber
= ref_lock((u32
)usr_handle
);
334 spinlock_t
*subscriber_lock
;
336 if (subscriber
== NULL
)
339 subscriber_lock
= subscriber
->lock
;
340 subscr_terminate(subscriber
);
341 spin_unlock_bh(subscriber_lock
);
345 * subscr_conn_msg_event - handle new subscription request from subscriber
348 static void subscr_conn_msg_event(void *usr_handle
,
350 struct sk_buff
**buf
,
354 struct subscriber
*subscriber
= ref_lock((u32
)usr_handle
);
355 spinlock_t
*subscriber_lock
;
357 if (subscriber
== NULL
)
360 subscriber_lock
= subscriber
->lock
;
361 if (size
!= sizeof(struct tipc_subscr
))
362 subscr_terminate(subscriber
);
364 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
366 spin_unlock_bh(subscriber_lock
);
370 * subscr_named_msg_event - handle request to establish a new subscriber
373 static void subscr_named_msg_event(void *usr_handle
,
375 struct sk_buff
**buf
,
379 struct tipc_portid
const *orig
,
380 struct tipc_name_seq
const *dest
)
382 struct subscriber
*subscriber
;
383 struct iovec msg_sect
= {0, 0};
384 spinlock_t
*subscriber_lock
;
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");
393 /* Create subscriber object */
395 subscriber
= kmalloc(sizeof(struct subscriber
), GFP_ATOMIC
);
396 if (subscriber
== NULL
) {
397 warn("Memory squeeze; ignoring subscriber setup\n");
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");
410 /* Establish a connection to subscriber */
412 tipc_createport(topsrv
.user_ref
,
413 (void *)subscriber
->ref
,
417 subscr_conn_shutdown_event
,
420 subscr_conn_msg_event
,
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
);
429 tipc_connect2port(subscriber
->port_ref
, orig
);
432 /* Add subscriber to topology server's subscriber list */
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
);
440 * Subscribe now if message contains a subscription,
441 * otherwise send an empty response to complete connection handshaking
444 subscriber_lock
= subscriber
->lock
;
446 subscr_subscribe((struct tipc_subscr
*)data
, subscriber
);
448 tipc_send(subscriber
->port_ref
, 1, &msg_sect
);
450 spin_unlock_bh(subscriber_lock
);
453 int subscr_start(void)
455 struct tipc_name_seq seq
= {TIPC_TOP_SRV
, TIPC_TOP_SRV
, TIPC_TOP_SRV
};
458 memset(&topsrv
, 0, sizeof (topsrv
));
459 topsrv
.lock
= SPIN_LOCK_UNLOCKED
;
460 INIT_LIST_HEAD(&topsrv
.subscriber_list
);
462 spin_lock_bh(&topsrv
.lock
);
463 res
= tipc_attach(&topsrv
.user_ref
, 0, 0);
465 spin_unlock_bh(&topsrv
.lock
);
469 res
= tipc_createport(topsrv
.user_ref
,
471 TIPC_CRITICAL_IMPORTANCE
,
476 subscr_named_msg_event
,
483 res
= nametbl_publish_rsv(topsrv
.setup_port
, TIPC_NODE_SCOPE
, &seq
);
487 spin_unlock_bh(&topsrv
.lock
);
491 err("Unable to create subscription service\n");
492 tipc_detach(topsrv
.user_ref
);
494 spin_unlock_bh(&topsrv
.lock
);
498 void subscr_stop(void)
500 struct subscriber
*subscriber
;
501 struct subscriber
*subscriber_temp
;
502 spinlock_t
*subscriber_lock
;
504 if (topsrv
.user_ref
) {
505 tipc_deleteport(topsrv
.setup_port
);
506 list_for_each_entry_safe(subscriber
, subscriber_temp
,
507 &topsrv
.subscriber_list
,
509 ref_lock(subscriber
->ref
);
510 subscriber_lock
= subscriber
->lock
;
511 subscr_terminate(subscriber
);
512 spin_unlock_bh(subscriber_lock
);
514 tipc_detach(topsrv
.user_ref
);
520 int tipc_ispublished(struct tipc_name
const *name
)
524 return(nametbl_translate(name
->type
, name
->instance
,&domain
) != 0);