blob: 7c62791eb0cc54de01a5a36edf71fb8a256b28af [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
2 * net/tipc/subscr.c: TIPC subscription service
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Per Lidenb97bf3f2006-01-02 19:04:38 +01005 * Copyright (c) 2005, Wind River Systems
Per Lidenb97bf3f2006-01-02 19:04:38 +01006 * All rights reserved.
7 *
Per Liden9ea1fd32006-01-11 13:30:43 +01008 * Redistribution and use in source and binary forms, with or without
Per Lidenb97bf3f2006-01-02 19:04:38 +01009 * modification, are permitted provided that the following conditions are met:
10 *
Per Liden9ea1fd32006-01-11 13:30:43 +010011 * 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.
Per Lidenb97bf3f2006-01-02 19:04:38 +010019 *
Per Liden9ea1fd32006-01-11 13:30:43 +010020 * 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
Per Lidenb97bf3f2006-01-02 19:04:38 +010034 * POSSIBILITY OF SUCH DAMAGE.
35 */
36
37#include "core.h"
38#include "dbg.h"
39#include "subscr.h"
40#include "name_table.h"
Allan Stephens28353e72008-05-19 13:29:47 -070041#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010042#include "ref.h"
43
44/**
45 * struct subscriber - TIPC network topology subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070046 * @port_ref: object reference to server port connecting to subscriber
47 * @lock: pointer to spinlock controlling access to subscriber's server port
Per Lidenb97bf3f2006-01-02 19:04:38 +010048 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
49 * @subscription_list: list of subscription objects for this subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010050 */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090051
Per Lidenb97bf3f2006-01-02 19:04:38 +010052struct subscriber {
Allan Stephens28353e72008-05-19 13:29:47 -070053 u32 port_ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090054 spinlock_t *lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010055 struct list_head subscriber_list;
56 struct list_head subscription_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +010057};
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
68struct 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
76static 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
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090082 *
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 * Returns converted value
84 */
85
Sam Ravnborg05790c62006-03-20 22:37:04 -080086static u32 htohl(u32 in, int swap)
Per Lidenb97bf3f2006-01-02 19:04:38 +010087{
Allan Stephensfc5ad582008-05-19 13:29:06 -070088 return swap ? (u32)___constant_swab32(in) : in;
Per Lidenb97bf3f2006-01-02 19:04:38 +010089}
90
91/**
92 * subscr_send_event - send a message containing a tipc_event to the subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070093 *
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!
Per Lidenb97bf3f2006-01-02 19:04:38 +010096 */
97
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090098static void subscr_send_event(struct subscription *sub,
99 u32 found_lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100100 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900101 u32 event,
102 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103 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
Allan Stephens8e9501f2008-05-19 13:28:32 -0700110 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);
Allan Stephens28353e72008-05-19 13:29:47 -0700115 tipc_send(sub->server_ref, 1, &msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100116}
117
118/**
Per Liden4323add2006-01-18 00:38:21 +0100119 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +0100120 *
121 * Returns 1 if there is overlap, otherwise 0.
122 */
123
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900124int tipc_subscr_overlap(struct subscription *sub,
125 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100126 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100127
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/**
Per Liden4323add2006-01-18 00:38:21 +0100139 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900140 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141 * Protected by nameseq.lock in name_table.c
142 */
143
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900144void tipc_subscr_report_overlap(struct subscription *sub,
145 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100146 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900147 u32 event,
148 u32 port_ref,
Per Liden4323add2006-01-18 00:38:21 +0100149 u32 node,
150 int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100151{
152 dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
153 sub->seq.upper, found_lower, found_upper);
Per Liden4323add2006-01-18 00:38:21 +0100154 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100155 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700156 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100157 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700158
159 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160}
161
162/**
163 * subscr_timeout - subscription timeout has occurred
164 */
165
166static void subscr_timeout(struct subscription *sub)
167{
Allan Stephens28353e72008-05-19 13:29:47 -0700168 struct port *server_port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100169
Allan Stephens28353e72008-05-19 13:29:47 -0700170 /* Validate server port reference (in case subscriber is terminating) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171
Allan Stephens28353e72008-05-19 13:29:47 -0700172 server_port = tipc_port_lock(sub->server_ref);
173 if (server_port == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 return;
175
Lijun Cheneb409462006-10-16 21:59:42 -0700176 /* Validate timeout (in case subscription is being cancelled) */
177
178 if (sub->timeout == TIPC_WAIT_FOREVER) {
Allan Stephens28353e72008-05-19 13:29:47 -0700179 tipc_port_unlock(server_port);
Lijun Cheneb409462006-10-16 21:59:42 -0700180 return;
181 }
182
Per Lidenb97bf3f2006-01-02 19:04:38 +0100183 /* Unlink subscription from name table */
184
Per Liden4323add2006-01-18 00:38:21 +0100185 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100186
Allan Stephens28353e72008-05-19 13:29:47 -0700187 /* Unlink subscription from subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100188
Per Lidenb97bf3f2006-01-02 19:04:38 +0100189 list_del(&sub->subscription_list);
190
Allan Stephens28353e72008-05-19 13:29:47 -0700191 /* 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
Per Lidenb97bf3f2006-01-02 19:04:38 +0100200 /* Now destroy subscription */
201
Per Lidenb97bf3f2006-01-02 19:04:38 +0100202 k_term_timer(&sub->timer);
203 kfree(sub);
204 atomic_dec(&topsrv.subscription_count);
205}
206
207/**
Lijun Cheneb409462006-10-16 21:59:42 -0700208 * subscr_del - delete a subscription within a subscription list
209 *
Allan Stephens28353e72008-05-19 13:29:47 -0700210 * Called with subscriber port locked.
Lijun Cheneb409462006-10-16 21:59:42 -0700211 */
212
213static 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/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100222 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900223 *
Allan Stephens28353e72008-05-19 13:29:47 -0700224 * Called with subscriber port locked. Routine must temporarily release lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900225 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900227 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100228 * 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
232static void subscr_terminate(struct subscriber *subscriber)
233{
Allan Stephens28353e72008-05-19 13:29:47 -0700234 u32 port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 struct subscription *sub;
236 struct subscription *sub_temp;
237
238 /* Invalidate subscriber reference */
239
Allan Stephens28353e72008-05-19 13:29:47 -0700240 port_ref = subscriber->port_ref;
241 subscriber->port_ref = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100242 spin_unlock_bh(subscriber->lock);
243
Allan Stephens28353e72008-05-19 13:29:47 -0700244 /* Sever connection to subscriber */
245
246 tipc_shutdown(port_ref);
247 tipc_deleteport(port_ref);
248
Per Lidenb97bf3f2006-01-02 19:04:38 +0100249 /* Destroy any existing subscriptions for subscriber */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900250
Per Lidenb97bf3f2006-01-02 19:04:38 +0100251 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 }
Lijun Cheneb409462006-10-16 21:59:42 -0700257 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100258 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
Lijun Cheneb409462006-10-16 21:59:42 -0700259 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100260 }
261
Per Lidenb97bf3f2006-01-02 19:04:38 +0100262 /* 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
Allan Stephens28353e72008-05-19 13:29:47 -0700268 /* Reclaim subscriber lock */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100269
270 spin_lock_bh(subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700271
272 /* Now destroy subscriber */
273
Per Lidenb97bf3f2006-01-02 19:04:38 +0100274 kfree(subscriber);
275}
276
277/**
Lijun Cheneb409462006-10-16 21:59:42 -0700278 * subscr_cancel - handle subscription cancellation request
279 *
Allan Stephens28353e72008-05-19 13:29:47 -0700280 * Called with subscriber port locked. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700281 * 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
287static 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/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100321 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900322 *
Allan Stephens28353e72008-05-19 13:29:47 -0700323 * Called with subscriber port locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100324 */
325
Allan Stephens28353e72008-05-19 13:29:47 -0700326static struct subscription *subscr_subscribe(struct tipc_subscr *s,
327 struct subscriber *subscriber)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100328{
329 struct subscription *sub;
Allan Stephens8e9501f2008-05-19 13:28:32 -0700330 int swap;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100331
Allan Stephens8e9501f2008-05-19 13:28:32 -0700332 /* Determine subscriber's endianness */
Lijun Cheneb409462006-10-16 21:59:42 -0700333
Allan Stephens8e9501f2008-05-19 13:28:32 -0700334 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700335
336 /* Detect & process a subscription cancellation request */
337
Allan Stephens8e9501f2008-05-19 13:28:32 -0700338 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
339 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700340 subscr_cancel(s, subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700341 return NULL;
Lijun Cheneb409462006-10-16 21:59:42 -0700342 }
343
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 /* Refuse subscription if global limit exceeded */
345
346 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700347 warn("Subscription rejected, subscription limit reached (%u)\n",
348 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700350 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100351 }
352
353 /* Allocate subscription object */
354
Allan Stephens28353e72008-05-19 13:29:47 -0700355 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700356 if (!sub) {
357 warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700359 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100360 }
361
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 /* Initialize subscription object */
363
Allan Stephens8e9501f2008-05-19 13:28:32 -0700364 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);
Lijun Cheneb409462006-10-16 21:59:42 -0700369 if ((!(sub->filter & TIPC_SUB_PORTS)
370 == !(sub->filter & TIPC_SUB_SERVICE))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100371 || (sub->seq.lower > sub->seq.upper)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700372 warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100373 kfree(sub);
374 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700375 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376 }
Allan Stephense15f8802008-05-19 13:27:31 -0700377 sub->event_cb = subscr_send_event;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 INIT_LIST_HEAD(&sub->nameseq_list);
379 list_add(&sub->subscription_list, &subscriber->subscription_list);
Allan Stephens28353e72008-05-19 13:29:47 -0700380 sub->server_ref = subscriber->port_ref;
Allan Stephens8e9501f2008-05-19 13:28:32 -0700381 sub->swap = swap;
Allan Stephens28353e72008-05-19 13:29:47 -0700382 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100383 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 }
Allan Stephens28353e72008-05-19 13:29:47 -0700389
390 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100391}
392
393/**
394 * subscr_conn_shutdown_event - handle termination request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700395 *
396 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100397 */
398
399static void subscr_conn_shutdown_event(void *usr_handle,
Allan Stephens28353e72008-05-19 13:29:47 -0700400 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100401 struct sk_buff **buf,
402 unsigned char const *data,
403 unsigned int size,
404 int reason)
405{
Allan Stephens28353e72008-05-19 13:29:47 -0700406 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100407 spinlock_t *subscriber_lock;
408
Allan Stephens28353e72008-05-19 13:29:47 -0700409 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100410 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
Allan Stephens28353e72008-05-19 13:29:47 -0700419 *
420 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100421 */
422
423static 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{
Allan Stephens28353e72008-05-19 13:29:47 -0700429 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100430 spinlock_t *subscriber_lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700431 struct subscription *sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100432
Allan Stephens28353e72008-05-19 13:29:47 -0700433 /*
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)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439 return;
440
441 subscriber_lock = subscriber->lock;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900442
Allan Stephens28353e72008-05-19 13:29:47 -0700443 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 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469}
470
471/**
472 * subscr_named_msg_event - handle request to establish a new subscriber
473 */
474
475static void subscr_named_msg_event(void *usr_handle,
476 u32 port_ref,
477 struct sk_buff **buf,
478 const unchar *data,
479 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900480 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 struct tipc_portid const *orig,
482 struct tipc_name_seq const *dest)
483{
Allan Stephens28353e72008-05-19 13:29:47 -0700484 static struct iovec msg_sect = {NULL, 0};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
Allan Stephens28353e72008-05-19 13:29:47 -0700486 struct subscriber *subscriber;
487 u32 server_port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488
489 /* Create subscriber object */
490
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700491 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100492 if (subscriber == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700493 warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 return;
495 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 INIT_LIST_HEAD(&subscriber->subscription_list);
497 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100498
Allan Stephens28353e72008-05-19 13:29:47 -0700499 /* Create server port & establish connection to subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100500
501 tipc_createport(topsrv.user_ref,
Allan Stephens28353e72008-05-19 13:29:47 -0700502 subscriber,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100503 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800504 NULL,
505 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100506 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800507 NULL,
508 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100509 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800510 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100511 &subscriber->port_ref);
512 if (subscriber->port_ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700513 warn("Subscriber rejected, unable to create port\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 kfree(subscriber);
515 return;
516 }
517 tipc_connect2port(subscriber->port_ref, orig);
518
Allan Stephens28353e72008-05-19 13:29:47 -0700519 /* Lock server port (& save lock address for future use) */
520
521 subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100522
523 /* Add subscriber to topology server's subscriber list */
524
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525 spin_lock_bh(&topsrv.lock);
526 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
527 spin_unlock_bh(&topsrv.lock);
528
Allan Stephens28353e72008-05-19 13:29:47 -0700529 /* Unlock server port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100530
Allan Stephens28353e72008-05-19 13:29:47 -0700531 server_port_ref = subscriber->port_ref;
532 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100533
Allan Stephens28353e72008-05-19 13:29:47 -0700534 /* 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 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100544}
545
Per Liden4323add2006-01-18 00:38:21 +0100546int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100547{
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));
Ingo Molnar34af9462006-06-27 02:53:55 -0700552 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100553 INIT_LIST_HEAD(&topsrv.subscriber_list);
554
555 spin_lock_bh(&topsrv.lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800556 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100557 if (res) {
558 spin_unlock_bh(&topsrv.lock);
559 return res;
560 }
561
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900562 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)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100574 goto failed;
575
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900576 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
577 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100578 goto failed;
579
580 spin_unlock_bh(&topsrv.lock);
581 return 0;
582
583failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100584 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 tipc_detach(topsrv.user_ref);
586 topsrv.user_ref = 0;
587 spin_unlock_bh(&topsrv.lock);
588 return res;
589}
590
Per Liden4323add2006-01-18 00:38:21 +0100591void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100592{
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);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900599 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100600 &topsrv.subscriber_list,
601 subscriber_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100602 subscriber_lock = subscriber->lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700603 spin_lock_bh(subscriber_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100604 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
613int tipc_ispublished(struct tipc_name const *name)
614{
615 u32 domain = 0;
616
Per Liden4323add2006-01-18 00:38:21 +0100617 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100618}
619