blob: ff123e56114a14e5e05671ac846d59a4e5555723 [file] [log] [blame]
Per Lidenb97bf3f2006-01-02 19:04:38 +01001/*
Allan Stephens5b06c85c2008-05-19 13:30:13 -07002 * net/tipc/subscr.c: TIPC network topology service
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +09003 *
Per Liden593a5f22006-01-11 19:14:19 +01004 * Copyright (c) 2000-2006, Ericsson AB
Allan Stephens5b06c85c2008-05-19 13:30:13 -07005 * Copyright (c) 2005-2007, 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010039#include "name_table.h"
Allan Stephens28353e72008-05-19 13:29:47 -070040#include "port.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041#include "ref.h"
Allan Stephens5b06c85c2008-05-19 13:30:13 -070042#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010043
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/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010079 * subscr_send_event - send a message containing a tipc_event to the subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070080 *
81 * Note: Must not hold subscriber's server port lock, since tipc_send() will
82 * try to take the lock if the message is rejected and returned!
Per Lidenb97bf3f2006-01-02 19:04:38 +010083 */
84
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090085static void subscr_send_event(struct subscription *sub,
86 u32 found_lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +010087 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090088 u32 event,
89 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +010090 u32 node)
91{
92 struct iovec msg_sect;
93
94 msg_sect.iov_base = (void *)&sub->evt;
95 msg_sect.iov_len = sizeof(struct tipc_event);
96
Neil Hormand88dca72010-03-08 12:20:58 -080097 sub->evt.event = htonl(event);
98 sub->evt.found_lower = htonl(found_lower);
99 sub->evt.found_upper = htonl(found_upper);
100 sub->evt.port.ref = htonl(port_ref);
101 sub->evt.port.node = htonl(node);
Allan Stephens28353e72008-05-19 13:29:47 -0700102 tipc_send(sub->server_ref, 1, &msg_sect);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100103}
104
105/**
Per Liden4323add2006-01-18 00:38:21 +0100106 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +0100107 *
108 * Returns 1 if there is overlap, otherwise 0.
109 */
110
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900111int tipc_subscr_overlap(struct subscription *sub,
112 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100113 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100114
115{
116 if (found_lower < sub->seq.lower)
117 found_lower = sub->seq.lower;
118 if (found_upper > sub->seq.upper)
119 found_upper = sub->seq.upper;
120 if (found_lower > found_upper)
121 return 0;
122 return 1;
123}
124
125/**
Per Liden4323add2006-01-18 00:38:21 +0100126 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900127 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100128 * Protected by nameseq.lock in name_table.c
129 */
130
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900131void tipc_subscr_report_overlap(struct subscription *sub,
132 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100133 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900134 u32 event,
135 u32 port_ref,
Per Liden4323add2006-01-18 00:38:21 +0100136 u32 node,
137 int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100138{
Per Liden4323add2006-01-18 00:38:21 +0100139 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100140 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700141 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100142 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700143
144 sub->event_cb(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145}
146
147/**
148 * subscr_timeout - subscription timeout has occurred
149 */
150
151static void subscr_timeout(struct subscription *sub)
152{
Allan Stephens28353e72008-05-19 13:29:47 -0700153 struct port *server_port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154
Allan Stephens28353e72008-05-19 13:29:47 -0700155 /* Validate server port reference (in case subscriber is terminating) */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156
Allan Stephens28353e72008-05-19 13:29:47 -0700157 server_port = tipc_port_lock(sub->server_ref);
158 if (server_port == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100159 return;
160
Lijun Cheneb409462006-10-16 21:59:42 -0700161 /* Validate timeout (in case subscription is being cancelled) */
162
163 if (sub->timeout == TIPC_WAIT_FOREVER) {
Allan Stephens28353e72008-05-19 13:29:47 -0700164 tipc_port_unlock(server_port);
Lijun Cheneb409462006-10-16 21:59:42 -0700165 return;
166 }
167
Per Lidenb97bf3f2006-01-02 19:04:38 +0100168 /* Unlink subscription from name table */
169
Per Liden4323add2006-01-18 00:38:21 +0100170 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100171
Allan Stephens28353e72008-05-19 13:29:47 -0700172 /* Unlink subscription from subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100173
Per Lidenb97bf3f2006-01-02 19:04:38 +0100174 list_del(&sub->subscription_list);
175
Allan Stephens28353e72008-05-19 13:29:47 -0700176 /* Release subscriber's server port */
177
178 tipc_port_unlock(server_port);
179
180 /* Notify subscriber of timeout */
181
182 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
183 TIPC_SUBSCR_TIMEOUT, 0, 0);
184
Per Lidenb97bf3f2006-01-02 19:04:38 +0100185 /* Now destroy subscription */
186
Per Lidenb97bf3f2006-01-02 19:04:38 +0100187 k_term_timer(&sub->timer);
188 kfree(sub);
189 atomic_dec(&topsrv.subscription_count);
190}
191
192/**
Lijun Cheneb409462006-10-16 21:59:42 -0700193 * subscr_del - delete a subscription within a subscription list
194 *
Allan Stephens28353e72008-05-19 13:29:47 -0700195 * Called with subscriber port locked.
Lijun Cheneb409462006-10-16 21:59:42 -0700196 */
197
198static void subscr_del(struct subscription *sub)
199{
200 tipc_nametbl_unsubscribe(sub);
201 list_del(&sub->subscription_list);
202 kfree(sub);
203 atomic_dec(&topsrv.subscription_count);
204}
205
206/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900208 *
Allan Stephens28353e72008-05-19 13:29:47 -0700209 * Called with subscriber port locked. Routine must temporarily release lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900210 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900212 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100213 * a new object reference in the interim that uses this lock; this routine will
214 * simply wait for it to be released, then claim it.)
215 */
216
217static void subscr_terminate(struct subscriber *subscriber)
218{
Allan Stephens28353e72008-05-19 13:29:47 -0700219 u32 port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100220 struct subscription *sub;
221 struct subscription *sub_temp;
222
223 /* Invalidate subscriber reference */
224
Allan Stephens28353e72008-05-19 13:29:47 -0700225 port_ref = subscriber->port_ref;
226 subscriber->port_ref = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100227 spin_unlock_bh(subscriber->lock);
228
Allan Stephens28353e72008-05-19 13:29:47 -0700229 /* Sever connection to subscriber */
230
231 tipc_shutdown(port_ref);
232 tipc_deleteport(port_ref);
233
Per Lidenb97bf3f2006-01-02 19:04:38 +0100234 /* Destroy any existing subscriptions for subscriber */
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900235
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
237 subscription_list) {
238 if (sub->timeout != TIPC_WAIT_FOREVER) {
239 k_cancel_timer(&sub->timer);
240 k_term_timer(&sub->timer);
241 }
Lijun Cheneb409462006-10-16 21:59:42 -0700242 dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
Per Lidenb97bf3f2006-01-02 19:04:38 +0100243 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
Lijun Cheneb409462006-10-16 21:59:42 -0700244 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100245 }
246
Per Lidenb97bf3f2006-01-02 19:04:38 +0100247 /* Remove subscriber from topology server's subscriber list */
248
249 spin_lock_bh(&topsrv.lock);
250 list_del(&subscriber->subscriber_list);
251 spin_unlock_bh(&topsrv.lock);
252
Allan Stephens28353e72008-05-19 13:29:47 -0700253 /* Reclaim subscriber lock */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100254
255 spin_lock_bh(subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700256
257 /* Now destroy subscriber */
258
Per Lidenb97bf3f2006-01-02 19:04:38 +0100259 kfree(subscriber);
260}
261
262/**
Lijun Cheneb409462006-10-16 21:59:42 -0700263 * subscr_cancel - handle subscription cancellation request
264 *
Allan Stephens28353e72008-05-19 13:29:47 -0700265 * Called with subscriber port locked. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700266 * to enable the subscription timeout routine to finish without deadlocking;
267 * the lock is then reclaimed to allow caller to release it upon return.
268 *
269 * Note that fields of 's' use subscriber's endianness!
270 */
271
272static void subscr_cancel(struct tipc_subscr *s,
273 struct subscriber *subscriber)
274{
275 struct subscription *sub;
276 struct subscription *sub_temp;
Neil Horman8c974432010-10-21 01:06:15 +0000277 __u32 type, lower, upper;
Lijun Cheneb409462006-10-16 21:59:42 -0700278 int found = 0;
279
280 /* Find first matching subscription, exit if not found */
281
Neil Hormand88dca72010-03-08 12:20:58 -0800282 type = ntohl(s->seq.type);
283 lower = ntohl(s->seq.lower);
284 upper = ntohl(s->seq.upper);
285
Lijun Cheneb409462006-10-16 21:59:42 -0700286 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
287 subscription_list) {
Neil Hormand88dca72010-03-08 12:20:58 -0800288 if ((type == sub->seq.type) &&
289 (lower == sub->seq.lower) &&
Neil Horman8c974432010-10-21 01:06:15 +0000290 (upper == sub->seq.upper)) {
Neil Hormand88dca72010-03-08 12:20:58 -0800291 found = 1;
292 break;
293 }
Lijun Cheneb409462006-10-16 21:59:42 -0700294 }
295 if (!found)
296 return;
297
298 /* Cancel subscription timer (if used), then delete subscription */
299
300 if (sub->timeout != TIPC_WAIT_FOREVER) {
301 sub->timeout = TIPC_WAIT_FOREVER;
302 spin_unlock_bh(subscriber->lock);
303 k_cancel_timer(&sub->timer);
304 k_term_timer(&sub->timer);
305 spin_lock_bh(subscriber->lock);
306 }
Neil Horman8c974432010-10-21 01:06:15 +0000307 dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
Lijun Cheneb409462006-10-16 21:59:42 -0700308 sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
309 subscr_del(sub);
310}
311
312/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100313 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900314 *
Allan Stephens28353e72008-05-19 13:29:47 -0700315 * Called with subscriber port locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100316 */
317
Allan Stephens28353e72008-05-19 13:29:47 -0700318static struct subscription *subscr_subscribe(struct tipc_subscr *s,
319 struct subscriber *subscriber)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320{
321 struct subscription *sub;
Lijun Cheneb409462006-10-16 21:59:42 -0700322
323 /* Detect & process a subscription cancellation request */
324
Neil Hormand88dca72010-03-08 12:20:58 -0800325 if (ntohl(s->filter) & TIPC_SUB_CANCEL) {
Lijun Cheneb409462006-10-16 21:59:42 -0700326 subscr_cancel(s, subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700327 return NULL;
Lijun Cheneb409462006-10-16 21:59:42 -0700328 }
329
Per Lidenb97bf3f2006-01-02 19:04:38 +0100330 /* Refuse subscription if global limit exceeded */
331
332 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700333 warn("Subscription rejected, subscription limit reached (%u)\n",
334 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700336 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100337 }
338
339 /* Allocate subscription object */
340
Allan Stephens28353e72008-05-19 13:29:47 -0700341 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700342 if (!sub) {
343 warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100344 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700345 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100346 }
347
Per Lidenb97bf3f2006-01-02 19:04:38 +0100348 /* Initialize subscription object */
349
Neil Hormand88dca72010-03-08 12:20:58 -0800350 sub->seq.type = ntohl(s->seq.type);
351 sub->seq.lower = ntohl(s->seq.lower);
352 sub->seq.upper = ntohl(s->seq.upper);
353 sub->timeout = ntohl(s->timeout);
354 sub->filter = ntohl(s->filter);
Neil Horman8c974432010-10-21 01:06:15 +0000355 if ((!(sub->filter & TIPC_SUB_PORTS) ==
356 !(sub->filter & TIPC_SUB_SERVICE)) ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800357 (sub->seq.lower > sub->seq.upper)) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700358 warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100359 kfree(sub);
360 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700361 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100362 }
Allan Stephense15f8802008-05-19 13:27:31 -0700363 sub->event_cb = subscr_send_event;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 INIT_LIST_HEAD(&sub->nameseq_list);
365 list_add(&sub->subscription_list, &subscriber->subscription_list);
Allan Stephens28353e72008-05-19 13:29:47 -0700366 sub->server_ref = subscriber->port_ref;
Allan Stephens28353e72008-05-19 13:29:47 -0700367 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100368 atomic_inc(&topsrv.subscription_count);
369 if (sub->timeout != TIPC_WAIT_FOREVER) {
370 k_init_timer(&sub->timer,
371 (Handler)subscr_timeout, (unsigned long)sub);
372 k_start_timer(&sub->timer, sub->timeout);
373 }
Allan Stephens28353e72008-05-19 13:29:47 -0700374
375 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100376}
377
378/**
379 * subscr_conn_shutdown_event - handle termination request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700380 *
381 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100382 */
383
384static void subscr_conn_shutdown_event(void *usr_handle,
Allan Stephens28353e72008-05-19 13:29:47 -0700385 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 struct sk_buff **buf,
387 unsigned char const *data,
388 unsigned int size,
389 int reason)
390{
Allan Stephens28353e72008-05-19 13:29:47 -0700391 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100392 spinlock_t *subscriber_lock;
393
Allan Stephens28353e72008-05-19 13:29:47 -0700394 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100395 return;
396
397 subscriber_lock = subscriber->lock;
398 subscr_terminate(subscriber);
399 spin_unlock_bh(subscriber_lock);
400}
401
402/**
403 * subscr_conn_msg_event - handle new subscription request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700404 *
405 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100406 */
407
408static void subscr_conn_msg_event(void *usr_handle,
409 u32 port_ref,
410 struct sk_buff **buf,
411 const unchar *data,
412 u32 size)
413{
Allan Stephens28353e72008-05-19 13:29:47 -0700414 struct subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100415 spinlock_t *subscriber_lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700416 struct subscription *sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100417
Allan Stephens28353e72008-05-19 13:29:47 -0700418 /*
419 * Lock subscriber's server port (& make a local copy of lock pointer,
420 * in case subscriber is deleted while processing subscription request)
421 */
422
423 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100424 return;
425
426 subscriber_lock = subscriber->lock;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900427
Allan Stephens28353e72008-05-19 13:29:47 -0700428 if (size != sizeof(struct tipc_subscr)) {
429 subscr_terminate(subscriber);
430 spin_unlock_bh(subscriber_lock);
431 } else {
432 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
433 spin_unlock_bh(subscriber_lock);
434 if (sub != NULL) {
435
436 /*
437 * We must release the server port lock before adding a
438 * subscription to the name table since TIPC needs to be
439 * able to (re)acquire the port lock if an event message
440 * issued by the subscription process is rejected and
441 * returned. The subscription cannot be deleted while
442 * it is being added to the name table because:
443 * a) the single-threading of the native API port code
444 * ensures the subscription cannot be cancelled and
445 * the subscriber connection cannot be broken, and
446 * b) the name table lock ensures the subscription
447 * timeout code cannot delete the subscription,
448 * so the subscription object is still protected.
449 */
450
451 tipc_nametbl_subscribe(sub);
452 }
453 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454}
455
456/**
457 * subscr_named_msg_event - handle request to establish a new subscriber
458 */
459
460static void subscr_named_msg_event(void *usr_handle,
461 u32 port_ref,
462 struct sk_buff **buf,
463 const unchar *data,
464 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900465 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100466 struct tipc_portid const *orig,
467 struct tipc_name_seq const *dest)
468{
Allan Stephens28353e72008-05-19 13:29:47 -0700469 static struct iovec msg_sect = {NULL, 0};
Per Lidenb97bf3f2006-01-02 19:04:38 +0100470
Allan Stephens28353e72008-05-19 13:29:47 -0700471 struct subscriber *subscriber;
472 u32 server_port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100473
474 /* Create subscriber object */
475
Panagiotis Issaris0da974f2006-07-21 14:51:30 -0700476 subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100477 if (subscriber == NULL) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700478 warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100479 return;
480 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100481 INIT_LIST_HEAD(&subscriber->subscription_list);
482 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100483
Allan Stephens28353e72008-05-19 13:29:47 -0700484 /* Create server port & establish connection to subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100485
486 tipc_createport(topsrv.user_ref,
Allan Stephens28353e72008-05-19 13:29:47 -0700487 subscriber,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100488 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800489 NULL,
490 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100491 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800492 NULL,
493 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100494 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800495 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100496 &subscriber->port_ref);
497 if (subscriber->port_ref == 0) {
Allan Stephensa10bd922006-06-25 23:52:17 -0700498 warn("Subscriber rejected, unable to create port\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100499 kfree(subscriber);
500 return;
501 }
502 tipc_connect2port(subscriber->port_ref, orig);
503
Allan Stephens28353e72008-05-19 13:29:47 -0700504 /* Lock server port (& save lock address for future use) */
505
506 subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100507
508 /* Add subscriber to topology server's subscriber list */
509
Per Lidenb97bf3f2006-01-02 19:04:38 +0100510 spin_lock_bh(&topsrv.lock);
511 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
512 spin_unlock_bh(&topsrv.lock);
513
Allan Stephens28353e72008-05-19 13:29:47 -0700514 /* Unlock server port */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100515
Allan Stephens28353e72008-05-19 13:29:47 -0700516 server_port_ref = subscriber->port_ref;
517 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100518
Allan Stephens28353e72008-05-19 13:29:47 -0700519 /* Send an ACK- to complete connection handshaking */
520
521 tipc_send(server_port_ref, 1, &msg_sect);
522
523 /* Handle optional subscription request */
524
525 if (size != 0) {
526 subscr_conn_msg_event(subscriber, server_port_ref,
527 buf, data, size);
528 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100529}
530
Per Liden4323add2006-01-18 00:38:21 +0100531int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100532{
533 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
534 int res = -1;
535
536 memset(&topsrv, 0, sizeof (topsrv));
Ingo Molnar34af9462006-06-27 02:53:55 -0700537 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100538 INIT_LIST_HEAD(&topsrv.subscriber_list);
539
540 spin_lock_bh(&topsrv.lock);
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800541 res = tipc_attach(&topsrv.user_ref, NULL, NULL);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 if (res) {
543 spin_unlock_bh(&topsrv.lock);
544 return res;
545 }
546
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900547 res = tipc_createport(topsrv.user_ref,
548 NULL,
549 TIPC_CRITICAL_IMPORTANCE,
550 NULL,
551 NULL,
552 NULL,
553 NULL,
554 subscr_named_msg_event,
555 NULL,
556 NULL,
557 &topsrv.setup_port);
558 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100559 goto failed;
560
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900561 res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
562 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100563 goto failed;
564
565 spin_unlock_bh(&topsrv.lock);
566 return 0;
567
568failed:
Per Lidend0a14a92006-01-11 13:52:51 +0100569 err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100570 tipc_detach(topsrv.user_ref);
571 topsrv.user_ref = 0;
572 spin_unlock_bh(&topsrv.lock);
573 return res;
574}
575
Per Liden4323add2006-01-18 00:38:21 +0100576void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100577{
578 struct subscriber *subscriber;
579 struct subscriber *subscriber_temp;
580 spinlock_t *subscriber_lock;
581
582 if (topsrv.user_ref) {
583 tipc_deleteport(topsrv.setup_port);
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900584 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100585 &topsrv.subscriber_list,
586 subscriber_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100587 subscriber_lock = subscriber->lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700588 spin_lock_bh(subscriber_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100589 subscr_terminate(subscriber);
590 spin_unlock_bh(subscriber_lock);
591 }
592 tipc_detach(topsrv.user_ref);
593 topsrv.user_ref = 0;
594 }
595}
596
597
598int tipc_ispublished(struct tipc_name const *name)
599{
600 u32 domain = 0;
601
Per Liden4323add2006-01-18 00:38:21 +0100602 return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100603}
604