blob: 5ed5965eb0bee40ec7e475d814370426b1e45a88 [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 Stephens23dd4cc2011-01-07 11:43:40 -05005 * Copyright (c) 2005-2007, 2010-2011, 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"
Per Lidenb97bf3f2006-01-02 19:04:38 +010038#include "name_table.h"
Allan Stephensaa702002010-12-31 18:59:21 +000039#include "port.h"
Allan Stephens5b06c85c2008-05-19 13:30:13 -070040#include "subscr.h"
Per Lidenb97bf3f2006-01-02 19:04:38 +010041
42/**
Paul Gortmaker11f99902011-12-29 20:49:39 -050043 * struct tipc_subscriber - TIPC network topology subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070044 * @port_ref: object reference to server port connecting to subscriber
45 * @lock: pointer to spinlock controlling access to subscriber's server port
Per Lidenb97bf3f2006-01-02 19:04:38 +010046 * @subscriber_list: adjacent subscribers in top. server's list of subscribers
47 * @subscription_list: list of subscription objects for this subscriber
Per Lidenb97bf3f2006-01-02 19:04:38 +010048 */
Paul Gortmaker11f99902011-12-29 20:49:39 -050049struct tipc_subscriber {
Allan Stephens28353e72008-05-19 13:29:47 -070050 u32 port_ref;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090051 spinlock_t *lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +010052 struct list_head subscriber_list;
53 struct list_head subscription_list;
Per Lidenb97bf3f2006-01-02 19:04:38 +010054};
55
56/**
57 * struct top_srv - TIPC network topology subscription service
Per Lidenb97bf3f2006-01-02 19:04:38 +010058 * @setup_port: reference to TIPC port that handles subscription requests
59 * @subscription_count: number of active subscriptions (not subscribers!)
60 * @subscriber_list: list of ports subscribing to service
61 * @lock: spinlock govering access to subscriber list
62 */
Per Lidenb97bf3f2006-01-02 19:04:38 +010063struct top_srv {
Per Lidenb97bf3f2006-01-02 19:04:38 +010064 u32 setup_port;
65 atomic_t subscription_count;
66 struct list_head subscriber_list;
67 spinlock_t lock;
68};
69
Allan Stephense3ec9c72010-12-31 18:59:34 +000070static struct top_srv topsrv;
Per Lidenb97bf3f2006-01-02 19:04:38 +010071
72/**
Neil Hormandb5a7532010-10-21 01:06:16 +000073 * htohl - convert value to endianness used by destination
74 * @in: value to convert
75 * @swap: non-zero if endianness must be reversed
76 *
77 * Returns converted value
78 */
Neil Hormandb5a7532010-10-21 01:06:16 +000079static u32 htohl(u32 in, int swap)
80{
81 return swap ? swab32(in) : in;
82}
83
84/**
Per Lidenb97bf3f2006-01-02 19:04:38 +010085 * subscr_send_event - send a message containing a tipc_event to the subscriber
Allan Stephens28353e72008-05-19 13:29:47 -070086 *
87 * Note: Must not hold subscriber's server port lock, since tipc_send() will
88 * try to take the lock if the message is rejected and returned!
Per Lidenb97bf3f2006-01-02 19:04:38 +010089 */
Paul Gortmakerfead3902011-12-29 20:43:44 -050090static void subscr_send_event(struct tipc_subscription *sub,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090091 u32 found_lower,
Per Lidenb97bf3f2006-01-02 19:04:38 +010092 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +090093 u32 event,
94 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +010095 u32 node)
96{
97 struct iovec msg_sect;
98
99 msg_sect.iov_base = (void *)&sub->evt;
100 msg_sect.iov_len = sizeof(struct tipc_event);
101
Neil Hormandb5a7532010-10-21 01:06:16 +0000102 sub->evt.event = htohl(event, sub->swap);
103 sub->evt.found_lower = htohl(found_lower, sub->swap);
104 sub->evt.found_upper = htohl(found_upper, sub->swap);
105 sub->evt.port.ref = htohl(port_ref, sub->swap);
106 sub->evt.port.node = htohl(node, sub->swap);
Allan Stephens26896902011-04-21 10:42:07 -0500107 tipc_send(sub->server_ref, 1, &msg_sect, msg_sect.iov_len);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100108}
109
110/**
Per Liden4323add2006-01-18 00:38:21 +0100111 * tipc_subscr_overlap - test for subscription overlap with the given values
Per Lidenb97bf3f2006-01-02 19:04:38 +0100112 *
113 * Returns 1 if there is overlap, otherwise 0.
114 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500115int tipc_subscr_overlap(struct tipc_subscription *sub,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900116 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100117 u32 found_upper)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100118
119{
120 if (found_lower < sub->seq.lower)
121 found_lower = sub->seq.lower;
122 if (found_upper > sub->seq.upper)
123 found_upper = sub->seq.upper;
124 if (found_lower > found_upper)
125 return 0;
126 return 1;
127}
128
129/**
Per Liden4323add2006-01-18 00:38:21 +0100130 * tipc_subscr_report_overlap - issue event if there is subscription overlap
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900131 *
Per Lidenb97bf3f2006-01-02 19:04:38 +0100132 * Protected by nameseq.lock in name_table.c
133 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500134void tipc_subscr_report_overlap(struct tipc_subscription *sub,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900135 u32 found_lower,
Per Liden4323add2006-01-18 00:38:21 +0100136 u32 found_upper,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900137 u32 event,
138 u32 port_ref,
Per Liden4323add2006-01-18 00:38:21 +0100139 u32 node,
140 int must)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100141{
Per Liden4323add2006-01-18 00:38:21 +0100142 if (!tipc_subscr_overlap(sub, found_lower, found_upper))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100143 return;
Lijun Cheneb409462006-10-16 21:59:42 -0700144 if (!must && !(sub->filter & TIPC_SUB_PORTS))
Per Lidenb97bf3f2006-01-02 19:04:38 +0100145 return;
Allan Stephense15f8802008-05-19 13:27:31 -0700146
Ying Xue7e244772011-07-19 04:21:56 -0400147 subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100148}
149
150/**
151 * subscr_timeout - subscription timeout has occurred
152 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500153static void subscr_timeout(struct tipc_subscription *sub)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100154{
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500155 struct tipc_port *server_port;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100156
Allan Stephens28353e72008-05-19 13:29:47 -0700157 /* Validate server port reference (in case subscriber is terminating) */
Allan Stephens28353e72008-05-19 13:29:47 -0700158 server_port = tipc_port_lock(sub->server_ref);
159 if (server_port == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100160 return;
161
Lijun Cheneb409462006-10-16 21:59:42 -0700162 /* Validate timeout (in case subscription is being cancelled) */
Lijun Cheneb409462006-10-16 21:59:42 -0700163 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 */
Per Liden4323add2006-01-18 00:38:21 +0100169 tipc_nametbl_unsubscribe(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100170
Allan Stephens28353e72008-05-19 13:29:47 -0700171 /* Unlink subscription from subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100172 list_del(&sub->subscription_list);
173
Allan Stephens28353e72008-05-19 13:29:47 -0700174 /* Release subscriber's server port */
Allan Stephens28353e72008-05-19 13:29:47 -0700175 tipc_port_unlock(server_port);
176
177 /* Notify subscriber of timeout */
Allan Stephens28353e72008-05-19 13:29:47 -0700178 subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
179 TIPC_SUBSCR_TIMEOUT, 0, 0);
180
Per Lidenb97bf3f2006-01-02 19:04:38 +0100181 /* Now destroy subscription */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100182 k_term_timer(&sub->timer);
183 kfree(sub);
184 atomic_dec(&topsrv.subscription_count);
185}
186
187/**
Lijun Cheneb409462006-10-16 21:59:42 -0700188 * subscr_del - delete a subscription within a subscription list
189 *
Allan Stephens28353e72008-05-19 13:29:47 -0700190 * Called with subscriber port locked.
Lijun Cheneb409462006-10-16 21:59:42 -0700191 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500192static void subscr_del(struct tipc_subscription *sub)
Lijun Cheneb409462006-10-16 21:59:42 -0700193{
194 tipc_nametbl_unsubscribe(sub);
195 list_del(&sub->subscription_list);
196 kfree(sub);
197 atomic_dec(&topsrv.subscription_count);
198}
199
200/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100201 * subscr_terminate - terminate communication with a subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900202 *
Allan Stephens28353e72008-05-19 13:29:47 -0700203 * Called with subscriber port locked. Routine must temporarily release lock
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900204 * to enable subscription timeout routine(s) to finish without deadlocking;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100205 * the lock is then reclaimed to allow caller to release it upon return.
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900206 * (This should work even in the unlikely event some other thread creates
Per Lidenb97bf3f2006-01-02 19:04:38 +0100207 * a new object reference in the interim that uses this lock; this routine will
208 * simply wait for it to be released, then claim it.)
209 */
Paul Gortmaker11f99902011-12-29 20:49:39 -0500210static void subscr_terminate(struct tipc_subscriber *subscriber)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100211{
Allan Stephens28353e72008-05-19 13:29:47 -0700212 u32 port_ref;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500213 struct tipc_subscription *sub;
214 struct tipc_subscription *sub_temp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100215
216 /* Invalidate subscriber reference */
Allan Stephens28353e72008-05-19 13:29:47 -0700217 port_ref = subscriber->port_ref;
218 subscriber->port_ref = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100219 spin_unlock_bh(subscriber->lock);
220
Allan Stephens28353e72008-05-19 13:29:47 -0700221 /* Sever connection to subscriber */
Allan Stephens28353e72008-05-19 13:29:47 -0700222 tipc_shutdown(port_ref);
223 tipc_deleteport(port_ref);
224
Per Lidenb97bf3f2006-01-02 19:04:38 +0100225 /* Destroy any existing subscriptions for subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100226 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
227 subscription_list) {
228 if (sub->timeout != TIPC_WAIT_FOREVER) {
229 k_cancel_timer(&sub->timer);
230 k_term_timer(&sub->timer);
231 }
Lijun Cheneb409462006-10-16 21:59:42 -0700232 subscr_del(sub);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100233 }
234
Per Lidenb97bf3f2006-01-02 19:04:38 +0100235 /* Remove subscriber from topology server's subscriber list */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100236 spin_lock_bh(&topsrv.lock);
237 list_del(&subscriber->subscriber_list);
238 spin_unlock_bh(&topsrv.lock);
239
Allan Stephens28353e72008-05-19 13:29:47 -0700240 /* Reclaim subscriber lock */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100241 spin_lock_bh(subscriber->lock);
Allan Stephens28353e72008-05-19 13:29:47 -0700242
243 /* Now destroy subscriber */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100244 kfree(subscriber);
245}
246
247/**
Lijun Cheneb409462006-10-16 21:59:42 -0700248 * subscr_cancel - handle subscription cancellation request
249 *
Allan Stephens28353e72008-05-19 13:29:47 -0700250 * Called with subscriber port locked. Routine must temporarily release lock
Lijun Cheneb409462006-10-16 21:59:42 -0700251 * to enable the subscription timeout routine to finish without deadlocking;
252 * the lock is then reclaimed to allow caller to release it upon return.
253 *
254 * Note that fields of 's' use subscriber's endianness!
255 */
Lijun Cheneb409462006-10-16 21:59:42 -0700256static void subscr_cancel(struct tipc_subscr *s,
Paul Gortmaker11f99902011-12-29 20:49:39 -0500257 struct tipc_subscriber *subscriber)
Lijun Cheneb409462006-10-16 21:59:42 -0700258{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500259 struct tipc_subscription *sub;
260 struct tipc_subscription *sub_temp;
Lijun Cheneb409462006-10-16 21:59:42 -0700261 int found = 0;
262
263 /* Find first matching subscription, exit if not found */
Lijun Cheneb409462006-10-16 21:59:42 -0700264 list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
265 subscription_list) {
Neil Hormandb5a7532010-10-21 01:06:16 +0000266 if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
267 found = 1;
268 break;
269 }
Lijun Cheneb409462006-10-16 21:59:42 -0700270 }
271 if (!found)
272 return;
273
274 /* Cancel subscription timer (if used), then delete subscription */
Lijun Cheneb409462006-10-16 21:59:42 -0700275 if (sub->timeout != TIPC_WAIT_FOREVER) {
276 sub->timeout = TIPC_WAIT_FOREVER;
277 spin_unlock_bh(subscriber->lock);
278 k_cancel_timer(&sub->timer);
279 k_term_timer(&sub->timer);
280 spin_lock_bh(subscriber->lock);
281 }
Lijun Cheneb409462006-10-16 21:59:42 -0700282 subscr_del(sub);
283}
284
285/**
Per Lidenb97bf3f2006-01-02 19:04:38 +0100286 * subscr_subscribe - create subscription for subscriber
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900287 *
Allan Stephens28353e72008-05-19 13:29:47 -0700288 * Called with subscriber port locked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100289 */
Paul Gortmakerfead3902011-12-29 20:43:44 -0500290static struct tipc_subscription *subscr_subscribe(struct tipc_subscr *s,
Paul Gortmaker11f99902011-12-29 20:49:39 -0500291 struct tipc_subscriber *subscriber)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100292{
Paul Gortmakerfead3902011-12-29 20:43:44 -0500293 struct tipc_subscription *sub;
Neil Hormandb5a7532010-10-21 01:06:16 +0000294 int swap;
295
296 /* Determine subscriber's endianness */
Neil Hormandb5a7532010-10-21 01:06:16 +0000297 swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
Lijun Cheneb409462006-10-16 21:59:42 -0700298
299 /* Detect & process a subscription cancellation request */
Neil Hormandb5a7532010-10-21 01:06:16 +0000300 if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
301 s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
Lijun Cheneb409462006-10-16 21:59:42 -0700302 subscr_cancel(s, subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700303 return NULL;
Lijun Cheneb409462006-10-16 21:59:42 -0700304 }
305
Per Lidenb97bf3f2006-01-02 19:04:38 +0100306 /* Refuse subscription if global limit exceeded */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100307 if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400308 pr_warn("Subscription rejected, limit reached (%u)\n",
309 tipc_max_subscriptions);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100310 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700311 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100312 }
313
314 /* Allocate subscription object */
Allan Stephens28353e72008-05-19 13:29:47 -0700315 sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
Allan Stephensa10bd922006-06-25 23:52:17 -0700316 if (!sub) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400317 pr_warn("Subscription rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100318 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700319 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100320 }
321
Per Lidenb97bf3f2006-01-02 19:04:38 +0100322 /* Initialize subscription object */
Neil Hormandb5a7532010-10-21 01:06:16 +0000323 sub->seq.type = htohl(s->seq.type, swap);
324 sub->seq.lower = htohl(s->seq.lower, swap);
325 sub->seq.upper = htohl(s->seq.upper, swap);
326 sub->timeout = htohl(s->timeout, swap);
327 sub->filter = htohl(s->filter, swap);
Neil Horman8c974432010-10-21 01:06:15 +0000328 if ((!(sub->filter & TIPC_SUB_PORTS) ==
329 !(sub->filter & TIPC_SUB_SERVICE)) ||
Joe Perchesf64f9e72009-11-29 16:55:45 -0800330 (sub->seq.lower > sub->seq.upper)) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400331 pr_warn("Subscription rejected, illegal request\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100332 kfree(sub);
333 subscr_terminate(subscriber);
Allan Stephens28353e72008-05-19 13:29:47 -0700334 return NULL;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100335 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100336 INIT_LIST_HEAD(&sub->nameseq_list);
337 list_add(&sub->subscription_list, &subscriber->subscription_list);
Allan Stephens28353e72008-05-19 13:29:47 -0700338 sub->server_ref = subscriber->port_ref;
Neil Hormandb5a7532010-10-21 01:06:16 +0000339 sub->swap = swap;
Allan Stephens28353e72008-05-19 13:29:47 -0700340 memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
Per Lidenb97bf3f2006-01-02 19:04:38 +0100341 atomic_inc(&topsrv.subscription_count);
342 if (sub->timeout != TIPC_WAIT_FOREVER) {
343 k_init_timer(&sub->timer,
344 (Handler)subscr_timeout, (unsigned long)sub);
345 k_start_timer(&sub->timer, sub->timeout);
346 }
Allan Stephens28353e72008-05-19 13:29:47 -0700347
348 return sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100349}
350
351/**
352 * subscr_conn_shutdown_event - handle termination request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700353 *
354 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100355 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100356static void subscr_conn_shutdown_event(void *usr_handle,
Allan Stephens28353e72008-05-19 13:29:47 -0700357 u32 port_ref,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100358 struct sk_buff **buf,
359 unsigned char const *data,
360 unsigned int size,
361 int reason)
362{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500363 struct tipc_subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100364 spinlock_t *subscriber_lock;
365
Allan Stephens28353e72008-05-19 13:29:47 -0700366 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100367 return;
368
369 subscriber_lock = subscriber->lock;
370 subscr_terminate(subscriber);
371 spin_unlock_bh(subscriber_lock);
372}
373
374/**
375 * subscr_conn_msg_event - handle new subscription request from subscriber
Allan Stephens28353e72008-05-19 13:29:47 -0700376 *
377 * Called with subscriber's server port unlocked.
Per Lidenb97bf3f2006-01-02 19:04:38 +0100378 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100379static void subscr_conn_msg_event(void *usr_handle,
380 u32 port_ref,
381 struct sk_buff **buf,
382 const unchar *data,
383 u32 size)
384{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500385 struct tipc_subscriber *subscriber = usr_handle;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100386 spinlock_t *subscriber_lock;
Paul Gortmakerfead3902011-12-29 20:43:44 -0500387 struct tipc_subscription *sub;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100388
Allan Stephens28353e72008-05-19 13:29:47 -0700389 /*
390 * Lock subscriber's server port (& make a local copy of lock pointer,
391 * in case subscriber is deleted while processing subscription request)
392 */
Allan Stephens28353e72008-05-19 13:29:47 -0700393 if (tipc_port_lock(port_ref) == NULL)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100394 return;
395
396 subscriber_lock = subscriber->lock;
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900397
Allan Stephens28353e72008-05-19 13:29:47 -0700398 if (size != sizeof(struct tipc_subscr)) {
399 subscr_terminate(subscriber);
400 spin_unlock_bh(subscriber_lock);
401 } else {
402 sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
403 spin_unlock_bh(subscriber_lock);
404 if (sub != NULL) {
405
406 /*
407 * We must release the server port lock before adding a
408 * subscription to the name table since TIPC needs to be
409 * able to (re)acquire the port lock if an event message
410 * issued by the subscription process is rejected and
411 * returned. The subscription cannot be deleted while
412 * it is being added to the name table because:
413 * a) the single-threading of the native API port code
414 * ensures the subscription cannot be cancelled and
415 * the subscriber connection cannot be broken, and
416 * b) the name table lock ensures the subscription
417 * timeout code cannot delete the subscription,
418 * so the subscription object is still protected.
419 */
Allan Stephens28353e72008-05-19 13:29:47 -0700420 tipc_nametbl_subscribe(sub);
421 }
422 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100423}
424
425/**
426 * subscr_named_msg_event - handle request to establish a new subscriber
427 */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100428static void subscr_named_msg_event(void *usr_handle,
429 u32 port_ref,
430 struct sk_buff **buf,
431 const unchar *data,
432 u32 size,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900433 u32 importance,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100434 struct tipc_portid const *orig,
435 struct tipc_name_seq const *dest)
436{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500437 struct tipc_subscriber *subscriber;
Allan Stephens28353e72008-05-19 13:29:47 -0700438 u32 server_port_ref;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100439
440 /* Create subscriber object */
Paul Gortmaker11f99902011-12-29 20:49:39 -0500441 subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100442 if (subscriber == NULL) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400443 pr_warn("Subscriber rejected, no memory\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100444 return;
445 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100446 INIT_LIST_HEAD(&subscriber->subscription_list);
447 INIT_LIST_HEAD(&subscriber->subscriber_list);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100448
Allan Stephens28353e72008-05-19 13:29:47 -0700449 /* Create server port & establish connection to subscriber */
Allan Stephensb0c1e922010-12-31 18:59:22 +0000450 tipc_createport(subscriber,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100451 importance,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800452 NULL,
453 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100454 subscr_conn_shutdown_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800455 NULL,
456 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100457 subscr_conn_msg_event,
Sam Ravnborg1fc54d82006-03-20 22:36:47 -0800458 NULL,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100459 &subscriber->port_ref);
460 if (subscriber->port_ref == 0) {
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400461 pr_warn("Subscriber rejected, unable to create port\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100462 kfree(subscriber);
463 return;
464 }
465 tipc_connect2port(subscriber->port_ref, orig);
466
Allan Stephens28353e72008-05-19 13:29:47 -0700467 /* Lock server port (& save lock address for future use) */
Allan Stephens23dd4cc2011-01-07 11:43:40 -0500468 subscriber->lock = tipc_port_lock(subscriber->port_ref)->lock;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100469
470 /* Add subscriber to topology server's subscriber list */
Per Lidenb97bf3f2006-01-02 19:04:38 +0100471 spin_lock_bh(&topsrv.lock);
472 list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
473 spin_unlock_bh(&topsrv.lock);
474
Allan Stephens28353e72008-05-19 13:29:47 -0700475 /* Unlock server port */
Allan Stephens28353e72008-05-19 13:29:47 -0700476 server_port_ref = subscriber->port_ref;
477 spin_unlock_bh(subscriber->lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100478
Allan Stephens28353e72008-05-19 13:29:47 -0700479 /* Send an ACK- to complete connection handshaking */
Allan Stephens26896902011-04-21 10:42:07 -0500480 tipc_send(server_port_ref, 0, NULL, 0);
Allan Stephens28353e72008-05-19 13:29:47 -0700481
482 /* Handle optional subscription request */
Allan Stephens28353e72008-05-19 13:29:47 -0700483 if (size != 0) {
484 subscr_conn_msg_event(subscriber, server_port_ref,
485 buf, data, size);
486 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100487}
488
Per Liden4323add2006-01-18 00:38:21 +0100489int tipc_subscr_start(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100490{
491 struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
Allan Stephens52fe7b72010-11-30 12:01:00 +0000492 int res;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100493
Ingo Molnar34af9462006-06-27 02:53:55 -0700494 spin_lock_init(&topsrv.lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100495 INIT_LIST_HEAD(&topsrv.subscriber_list);
496
Allan Stephensb0c1e922010-12-31 18:59:22 +0000497 res = tipc_createport(NULL,
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900498 TIPC_CRITICAL_IMPORTANCE,
499 NULL,
500 NULL,
501 NULL,
502 NULL,
503 subscr_named_msg_event,
504 NULL,
505 NULL,
506 &topsrv.setup_port);
507 if (res)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100508 goto failed;
509
Allan Stephensc422f1b2011-11-02 15:49:40 -0400510 res = tipc_publish(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
Allan Stephensaa702002010-12-31 18:59:21 +0000511 if (res) {
512 tipc_deleteport(topsrv.setup_port);
513 topsrv.setup_port = 0;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100514 goto failed;
Allan Stephensaa702002010-12-31 18:59:21 +0000515 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100516
Per Lidenb97bf3f2006-01-02 19:04:38 +0100517 return 0;
518
519failed:
Erik Hugne2cf8aa12012-06-29 00:16:37 -0400520 pr_err("Failed to create subscription service\n");
Per Lidenb97bf3f2006-01-02 19:04:38 +0100521 return res;
522}
523
Per Liden4323add2006-01-18 00:38:21 +0100524void tipc_subscr_stop(void)
Per Lidenb97bf3f2006-01-02 19:04:38 +0100525{
Paul Gortmaker11f99902011-12-29 20:49:39 -0500526 struct tipc_subscriber *subscriber;
527 struct tipc_subscriber *subscriber_temp;
Per Lidenb97bf3f2006-01-02 19:04:38 +0100528 spinlock_t *subscriber_lock;
529
Allan Stephensaa702002010-12-31 18:59:21 +0000530 if (topsrv.setup_port) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100531 tipc_deleteport(topsrv.setup_port);
Allan Stephensaa702002010-12-31 18:59:21 +0000532 topsrv.setup_port = 0;
533
YOSHIFUJI Hideakic4307282007-02-09 23:25:21 +0900534 list_for_each_entry_safe(subscriber, subscriber_temp,
Per Lidenb97bf3f2006-01-02 19:04:38 +0100535 &topsrv.subscriber_list,
536 subscriber_list) {
Per Lidenb97bf3f2006-01-02 19:04:38 +0100537 subscriber_lock = subscriber->lock;
Allan Stephens28353e72008-05-19 13:29:47 -0700538 spin_lock_bh(subscriber_lock);
Per Lidenb97bf3f2006-01-02 19:04:38 +0100539 subscr_terminate(subscriber);
540 spin_unlock_bh(subscriber_lock);
541 }
Per Lidenb97bf3f2006-01-02 19:04:38 +0100542 }
543}