Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
| 2 | * net/tipc/subscr.c: TIPC subscription service |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 3 | * |
Per Liden | 593a5f2 | 2006-01-11 19:14:19 +0100 | [diff] [blame] | 4 | * Copyright (c) 2000-2006, Ericsson AB |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 5 | * Copyright (c) 2005, Wind River Systems |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 6 | * All rights reserved. |
| 7 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 8 | * Redistribution and use in source and binary forms, with or without |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 9 | * modification, are permitted provided that the following conditions are met: |
| 10 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 11 | * 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 Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 19 | * |
Per Liden | 9ea1fd3 | 2006-01-11 13:30:43 +0100 | [diff] [blame] | 20 | * 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 Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 34 | * POSSIBILITY OF SUCH DAMAGE. |
| 35 | */ |
| 36 | |
| 37 | #include "core.h" |
| 38 | #include "dbg.h" |
| 39 | #include "subscr.h" |
| 40 | #include "name_table.h" |
| 41 | #include "ref.h" |
| 42 | |
| 43 | /** |
| 44 | * struct subscriber - TIPC network topology subscriber |
| 45 | * @ref: object reference to subscriber object itself |
| 46 | * @lock: pointer to spinlock controlling access to subscriber object |
| 47 | * @subscriber_list: adjacent subscribers in top. server's list of subscribers |
| 48 | * @subscription_list: list of subscription objects for this subscriber |
| 49 | * @port_ref: object reference to port used to communicate with subscriber |
| 50 | * @swap: indicates if subscriber uses opposite endianness in its messages |
| 51 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 52 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 53 | struct subscriber { |
| 54 | u32 ref; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 55 | spinlock_t *lock; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 56 | struct list_head subscriber_list; |
| 57 | struct list_head subscription_list; |
| 58 | u32 port_ref; |
| 59 | int swap; |
| 60 | }; |
| 61 | |
| 62 | /** |
| 63 | * struct top_srv - TIPC network topology subscription service |
| 64 | * @user_ref: TIPC userid of subscription service |
| 65 | * @setup_port: reference to TIPC port that handles subscription requests |
| 66 | * @subscription_count: number of active subscriptions (not subscribers!) |
| 67 | * @subscriber_list: list of ports subscribing to service |
| 68 | * @lock: spinlock govering access to subscriber list |
| 69 | */ |
| 70 | |
| 71 | struct top_srv { |
| 72 | u32 user_ref; |
| 73 | u32 setup_port; |
| 74 | atomic_t subscription_count; |
| 75 | struct list_head subscriber_list; |
| 76 | spinlock_t lock; |
| 77 | }; |
| 78 | |
| 79 | static struct top_srv topsrv = { 0 }; |
| 80 | |
| 81 | /** |
| 82 | * htohl - convert value to endianness used by destination |
| 83 | * @in: value to convert |
| 84 | * @swap: non-zero if endianness must be reversed |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 85 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 86 | * Returns converted value |
| 87 | */ |
| 88 | |
Sam Ravnborg | 05790c6 | 2006-03-20 22:37:04 -0800 | [diff] [blame] | 89 | static u32 htohl(u32 in, int swap) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 90 | { |
| 91 | char *c = (char *)∈ |
| 92 | |
| 93 | return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in; |
| 94 | } |
| 95 | |
| 96 | /** |
| 97 | * subscr_send_event - send a message containing a tipc_event to the subscriber |
| 98 | */ |
| 99 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 100 | static void subscr_send_event(struct subscription *sub, |
| 101 | u32 found_lower, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 102 | u32 found_upper, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 103 | u32 event, |
| 104 | u32 port_ref, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 105 | u32 node) |
| 106 | { |
| 107 | struct iovec msg_sect; |
| 108 | |
| 109 | msg_sect.iov_base = (void *)&sub->evt; |
| 110 | msg_sect.iov_len = sizeof(struct tipc_event); |
| 111 | |
| 112 | sub->evt.event = htohl(event, sub->owner->swap); |
| 113 | sub->evt.found_lower = htohl(found_lower, sub->owner->swap); |
| 114 | sub->evt.found_upper = htohl(found_upper, sub->owner->swap); |
| 115 | sub->evt.port.ref = htohl(port_ref, sub->owner->swap); |
| 116 | sub->evt.port.node = htohl(node, sub->owner->swap); |
| 117 | tipc_send(sub->owner->port_ref, 1, &msg_sect); |
| 118 | } |
| 119 | |
| 120 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 121 | * tipc_subscr_overlap - test for subscription overlap with the given values |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 122 | * |
| 123 | * Returns 1 if there is overlap, otherwise 0. |
| 124 | */ |
| 125 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 126 | int tipc_subscr_overlap(struct subscription *sub, |
| 127 | u32 found_lower, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 128 | u32 found_upper) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 129 | |
| 130 | { |
| 131 | if (found_lower < sub->seq.lower) |
| 132 | found_lower = sub->seq.lower; |
| 133 | if (found_upper > sub->seq.upper) |
| 134 | found_upper = sub->seq.upper; |
| 135 | if (found_lower > found_upper) |
| 136 | return 0; |
| 137 | return 1; |
| 138 | } |
| 139 | |
| 140 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 141 | * tipc_subscr_report_overlap - issue event if there is subscription overlap |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 142 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 143 | * Protected by nameseq.lock in name_table.c |
| 144 | */ |
| 145 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 146 | void tipc_subscr_report_overlap(struct subscription *sub, |
| 147 | u32 found_lower, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 148 | u32 found_upper, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 149 | u32 event, |
| 150 | u32 port_ref, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 151 | u32 node, |
| 152 | int must) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 153 | { |
| 154 | dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower, |
| 155 | sub->seq.upper, found_lower, found_upper); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 156 | if (!tipc_subscr_overlap(sub, found_lower, found_upper)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 157 | return; |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 158 | if (!must && !(sub->filter & TIPC_SUB_PORTS)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 159 | return; |
| 160 | subscr_send_event(sub, found_lower, found_upper, event, port_ref, node); |
| 161 | } |
| 162 | |
| 163 | /** |
| 164 | * subscr_timeout - subscription timeout has occurred |
| 165 | */ |
| 166 | |
| 167 | static void subscr_timeout(struct subscription *sub) |
| 168 | { |
| 169 | struct subscriber *subscriber; |
| 170 | u32 subscriber_ref; |
| 171 | |
| 172 | /* Validate subscriber reference (in case subscriber is terminating) */ |
| 173 | |
| 174 | subscriber_ref = sub->owner->ref; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 175 | subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 176 | if (subscriber == NULL) |
| 177 | return; |
| 178 | |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 179 | /* Validate timeout (in case subscription is being cancelled) */ |
| 180 | |
| 181 | if (sub->timeout == TIPC_WAIT_FOREVER) { |
| 182 | tipc_ref_unlock(subscriber_ref); |
| 183 | return; |
| 184 | } |
| 185 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 186 | /* Unlink subscription from name table */ |
| 187 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 188 | tipc_nametbl_unsubscribe(sub); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 189 | |
| 190 | /* Notify subscriber of timeout, then unlink subscription */ |
| 191 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 192 | subscr_send_event(sub, |
| 193 | sub->evt.s.seq.lower, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 194 | sub->evt.s.seq.upper, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 195 | TIPC_SUBSCR_TIMEOUT, |
| 196 | 0, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 197 | 0); |
| 198 | list_del(&sub->subscription_list); |
| 199 | |
| 200 | /* Now destroy subscription */ |
| 201 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 202 | tipc_ref_unlock(subscriber_ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 203 | k_term_timer(&sub->timer); |
| 204 | kfree(sub); |
| 205 | atomic_dec(&topsrv.subscription_count); |
| 206 | } |
| 207 | |
| 208 | /** |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 209 | * subscr_del - delete a subscription within a subscription list |
| 210 | * |
| 211 | * Called with subscriber locked. |
| 212 | */ |
| 213 | |
| 214 | static void subscr_del(struct subscription *sub) |
| 215 | { |
| 216 | tipc_nametbl_unsubscribe(sub); |
| 217 | list_del(&sub->subscription_list); |
| 218 | kfree(sub); |
| 219 | atomic_dec(&topsrv.subscription_count); |
| 220 | } |
| 221 | |
| 222 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 223 | * subscr_terminate - terminate communication with a subscriber |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 224 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 225 | * Called with subscriber locked. Routine must temporarily release this lock |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 226 | * to enable subscription timeout routine(s) to finish without deadlocking; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 227 | * the lock is then reclaimed to allow caller to release it upon return. |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 228 | * (This should work even in the unlikely event some other thread creates |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 229 | * a new object reference in the interim that uses this lock; this routine will |
| 230 | * simply wait for it to be released, then claim it.) |
| 231 | */ |
| 232 | |
| 233 | static void subscr_terminate(struct subscriber *subscriber) |
| 234 | { |
| 235 | struct subscription *sub; |
| 236 | struct subscription *sub_temp; |
| 237 | |
| 238 | /* Invalidate subscriber reference */ |
| 239 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 240 | tipc_ref_discard(subscriber->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 241 | spin_unlock_bh(subscriber->lock); |
| 242 | |
| 243 | /* Destroy any existing subscriptions for subscriber */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 244 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 245 | list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list, |
| 246 | subscription_list) { |
| 247 | if (sub->timeout != TIPC_WAIT_FOREVER) { |
| 248 | k_cancel_timer(&sub->timer); |
| 249 | k_term_timer(&sub->timer); |
| 250 | } |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 251 | dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n", |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 252 | sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 253 | subscr_del(sub); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 254 | } |
| 255 | |
| 256 | /* Sever connection to subscriber */ |
| 257 | |
| 258 | tipc_shutdown(subscriber->port_ref); |
| 259 | tipc_deleteport(subscriber->port_ref); |
| 260 | |
| 261 | /* Remove subscriber from topology server's subscriber list */ |
| 262 | |
| 263 | spin_lock_bh(&topsrv.lock); |
| 264 | list_del(&subscriber->subscriber_list); |
| 265 | spin_unlock_bh(&topsrv.lock); |
| 266 | |
| 267 | /* Now destroy subscriber */ |
| 268 | |
| 269 | spin_lock_bh(subscriber->lock); |
| 270 | kfree(subscriber); |
| 271 | } |
| 272 | |
| 273 | /** |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 274 | * subscr_cancel - handle subscription cancellation request |
| 275 | * |
| 276 | * Called with subscriber locked. Routine must temporarily release this lock |
| 277 | * to enable the subscription timeout routine to finish without deadlocking; |
| 278 | * the lock is then reclaimed to allow caller to release it upon return. |
| 279 | * |
| 280 | * Note that fields of 's' use subscriber's endianness! |
| 281 | */ |
| 282 | |
| 283 | static void subscr_cancel(struct tipc_subscr *s, |
| 284 | struct subscriber *subscriber) |
| 285 | { |
| 286 | struct subscription *sub; |
| 287 | struct subscription *sub_temp; |
| 288 | int found = 0; |
| 289 | |
| 290 | /* Find first matching subscription, exit if not found */ |
| 291 | |
| 292 | list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list, |
| 293 | subscription_list) { |
| 294 | if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) { |
| 295 | found = 1; |
| 296 | break; |
| 297 | } |
| 298 | } |
| 299 | if (!found) |
| 300 | return; |
| 301 | |
| 302 | /* Cancel subscription timer (if used), then delete subscription */ |
| 303 | |
| 304 | if (sub->timeout != TIPC_WAIT_FOREVER) { |
| 305 | sub->timeout = TIPC_WAIT_FOREVER; |
| 306 | spin_unlock_bh(subscriber->lock); |
| 307 | k_cancel_timer(&sub->timer); |
| 308 | k_term_timer(&sub->timer); |
| 309 | spin_lock_bh(subscriber->lock); |
| 310 | } |
| 311 | dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n", |
| 312 | sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber); |
| 313 | subscr_del(sub); |
| 314 | } |
| 315 | |
| 316 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 317 | * subscr_subscribe - create subscription for subscriber |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 318 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 319 | * Called with subscriber locked |
| 320 | */ |
| 321 | |
| 322 | static void subscr_subscribe(struct tipc_subscr *s, |
| 323 | struct subscriber *subscriber) |
| 324 | { |
| 325 | struct subscription *sub; |
| 326 | |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 327 | /* Determine/update subscriber's endianness */ |
| 328 | |
| 329 | if (s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE)) |
| 330 | subscriber->swap = 0; |
| 331 | else |
| 332 | subscriber->swap = 1; |
| 333 | |
| 334 | /* Detect & process a subscription cancellation request */ |
| 335 | |
| 336 | if (s->filter & htohl(TIPC_SUB_CANCEL, subscriber->swap)) { |
| 337 | s->filter &= ~htohl(TIPC_SUB_CANCEL, subscriber->swap); |
| 338 | subscr_cancel(s, subscriber); |
| 339 | return; |
| 340 | } |
| 341 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 342 | /* Refuse subscription if global limit exceeded */ |
| 343 | |
| 344 | if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 345 | warn("Subscription rejected, subscription limit reached (%u)\n", |
| 346 | tipc_max_subscriptions); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 347 | subscr_terminate(subscriber); |
| 348 | return; |
| 349 | } |
| 350 | |
| 351 | /* Allocate subscription object */ |
| 352 | |
Arnaldo Carvalho de Melo | 2710b57 | 2006-11-21 01:22:12 -0200 | [diff] [blame] | 353 | sub = kzalloc(sizeof(*sub), GFP_ATOMIC); |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 354 | if (!sub) { |
| 355 | warn("Subscription rejected, no memory\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 356 | subscr_terminate(subscriber); |
| 357 | return; |
| 358 | } |
| 359 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 360 | /* Initialize subscription object */ |
| 361 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 362 | sub->seq.type = htohl(s->seq.type, subscriber->swap); |
| 363 | sub->seq.lower = htohl(s->seq.lower, subscriber->swap); |
| 364 | sub->seq.upper = htohl(s->seq.upper, subscriber->swap); |
| 365 | sub->timeout = htohl(s->timeout, subscriber->swap); |
| 366 | sub->filter = htohl(s->filter, subscriber->swap); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 367 | if ((!(sub->filter & TIPC_SUB_PORTS) |
| 368 | == !(sub->filter & TIPC_SUB_SERVICE)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 369 | || (sub->seq.lower > sub->seq.upper)) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 370 | warn("Subscription rejected, illegal request\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 371 | kfree(sub); |
| 372 | subscr_terminate(subscriber); |
| 373 | return; |
| 374 | } |
| 375 | memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr)); |
| 376 | INIT_LIST_HEAD(&sub->subscription_list); |
| 377 | INIT_LIST_HEAD(&sub->nameseq_list); |
| 378 | list_add(&sub->subscription_list, &subscriber->subscription_list); |
| 379 | atomic_inc(&topsrv.subscription_count); |
| 380 | if (sub->timeout != TIPC_WAIT_FOREVER) { |
| 381 | k_init_timer(&sub->timer, |
| 382 | (Handler)subscr_timeout, (unsigned long)sub); |
| 383 | k_start_timer(&sub->timer, sub->timeout); |
| 384 | } |
| 385 | sub->owner = subscriber; |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 386 | tipc_nametbl_subscribe(sub); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 387 | } |
| 388 | |
| 389 | /** |
| 390 | * subscr_conn_shutdown_event - handle termination request from subscriber |
| 391 | */ |
| 392 | |
| 393 | static void subscr_conn_shutdown_event(void *usr_handle, |
| 394 | u32 portref, |
| 395 | struct sk_buff **buf, |
| 396 | unsigned char const *data, |
| 397 | unsigned int size, |
| 398 | int reason) |
| 399 | { |
David S. Miller | 880b005 | 2006-01-12 13:22:32 -0800 | [diff] [blame] | 400 | struct subscriber *subscriber; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 401 | spinlock_t *subscriber_lock; |
| 402 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 403 | subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 404 | if (subscriber == NULL) |
| 405 | return; |
| 406 | |
| 407 | subscriber_lock = subscriber->lock; |
| 408 | subscr_terminate(subscriber); |
| 409 | spin_unlock_bh(subscriber_lock); |
| 410 | } |
| 411 | |
| 412 | /** |
| 413 | * subscr_conn_msg_event - handle new subscription request from subscriber |
| 414 | */ |
| 415 | |
| 416 | static void subscr_conn_msg_event(void *usr_handle, |
| 417 | u32 port_ref, |
| 418 | struct sk_buff **buf, |
| 419 | const unchar *data, |
| 420 | u32 size) |
| 421 | { |
David S. Miller | 880b005 | 2006-01-12 13:22:32 -0800 | [diff] [blame] | 422 | struct subscriber *subscriber; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 423 | spinlock_t *subscriber_lock; |
| 424 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 425 | subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 426 | if (subscriber == NULL) |
| 427 | return; |
| 428 | |
| 429 | subscriber_lock = subscriber->lock; |
| 430 | if (size != sizeof(struct tipc_subscr)) |
| 431 | subscr_terminate(subscriber); |
| 432 | else |
| 433 | subscr_subscribe((struct tipc_subscr *)data, subscriber); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 434 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 435 | spin_unlock_bh(subscriber_lock); |
| 436 | } |
| 437 | |
| 438 | /** |
| 439 | * subscr_named_msg_event - handle request to establish a new subscriber |
| 440 | */ |
| 441 | |
| 442 | static void subscr_named_msg_event(void *usr_handle, |
| 443 | u32 port_ref, |
| 444 | struct sk_buff **buf, |
| 445 | const unchar *data, |
| 446 | u32 size, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 447 | u32 importance, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 448 | struct tipc_portid const *orig, |
| 449 | struct tipc_name_seq const *dest) |
| 450 | { |
| 451 | struct subscriber *subscriber; |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 452 | struct iovec msg_sect = {NULL, 0}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 453 | spinlock_t *subscriber_lock; |
| 454 | |
| 455 | dbg("subscr_named_msg_event: orig = %x own = %x,\n", |
| 456 | orig->node, tipc_own_addr); |
| 457 | if (size && (size != sizeof(struct tipc_subscr))) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 458 | warn("Subscriber rejected, invalid subscription size\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 459 | return; |
| 460 | } |
| 461 | |
| 462 | /* Create subscriber object */ |
| 463 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 464 | subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 465 | if (subscriber == NULL) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 466 | warn("Subscriber rejected, no memory\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 467 | return; |
| 468 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 469 | INIT_LIST_HEAD(&subscriber->subscription_list); |
| 470 | INIT_LIST_HEAD(&subscriber->subscriber_list); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 471 | subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 472 | if (subscriber->ref == 0) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 473 | warn("Subscriber rejected, reference table exhausted\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 474 | kfree(subscriber); |
| 475 | return; |
| 476 | } |
| 477 | |
| 478 | /* Establish a connection to subscriber */ |
| 479 | |
| 480 | tipc_createport(topsrv.user_ref, |
David S. Miller | 880b005 | 2006-01-12 13:22:32 -0800 | [diff] [blame] | 481 | (void *)(unsigned long)subscriber->ref, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 482 | importance, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 483 | NULL, |
| 484 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 485 | subscr_conn_shutdown_event, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 486 | NULL, |
| 487 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 488 | subscr_conn_msg_event, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 489 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 490 | &subscriber->port_ref); |
| 491 | if (subscriber->port_ref == 0) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 492 | warn("Subscriber rejected, unable to create port\n"); |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 493 | tipc_ref_discard(subscriber->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 494 | kfree(subscriber); |
| 495 | return; |
| 496 | } |
| 497 | tipc_connect2port(subscriber->port_ref, orig); |
| 498 | |
| 499 | |
| 500 | /* Add subscriber to topology server's subscriber list */ |
| 501 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 502 | tipc_ref_lock(subscriber->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 503 | spin_lock_bh(&topsrv.lock); |
| 504 | list_add(&subscriber->subscriber_list, &topsrv.subscriber_list); |
| 505 | spin_unlock_bh(&topsrv.lock); |
| 506 | |
| 507 | /* |
| 508 | * Subscribe now if message contains a subscription, |
| 509 | * otherwise send an empty response to complete connection handshaking |
| 510 | */ |
| 511 | |
| 512 | subscriber_lock = subscriber->lock; |
| 513 | if (size) |
| 514 | subscr_subscribe((struct tipc_subscr *)data, subscriber); |
| 515 | else |
| 516 | tipc_send(subscriber->port_ref, 1, &msg_sect); |
| 517 | |
| 518 | spin_unlock_bh(subscriber_lock); |
| 519 | } |
| 520 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 521 | int tipc_subscr_start(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 522 | { |
| 523 | struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV}; |
| 524 | int res = -1; |
| 525 | |
| 526 | memset(&topsrv, 0, sizeof (topsrv)); |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 527 | spin_lock_init(&topsrv.lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 528 | INIT_LIST_HEAD(&topsrv.subscriber_list); |
| 529 | |
| 530 | spin_lock_bh(&topsrv.lock); |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 531 | res = tipc_attach(&topsrv.user_ref, NULL, NULL); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 532 | if (res) { |
| 533 | spin_unlock_bh(&topsrv.lock); |
| 534 | return res; |
| 535 | } |
| 536 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 537 | res = tipc_createport(topsrv.user_ref, |
| 538 | NULL, |
| 539 | TIPC_CRITICAL_IMPORTANCE, |
| 540 | NULL, |
| 541 | NULL, |
| 542 | NULL, |
| 543 | NULL, |
| 544 | subscr_named_msg_event, |
| 545 | NULL, |
| 546 | NULL, |
| 547 | &topsrv.setup_port); |
| 548 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 549 | goto failed; |
| 550 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 551 | res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq); |
| 552 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 553 | goto failed; |
| 554 | |
| 555 | spin_unlock_bh(&topsrv.lock); |
| 556 | return 0; |
| 557 | |
| 558 | failed: |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 559 | err("Failed to create subscription service\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 560 | tipc_detach(topsrv.user_ref); |
| 561 | topsrv.user_ref = 0; |
| 562 | spin_unlock_bh(&topsrv.lock); |
| 563 | return res; |
| 564 | } |
| 565 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 566 | void tipc_subscr_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 567 | { |
| 568 | struct subscriber *subscriber; |
| 569 | struct subscriber *subscriber_temp; |
| 570 | spinlock_t *subscriber_lock; |
| 571 | |
| 572 | if (topsrv.user_ref) { |
| 573 | tipc_deleteport(topsrv.setup_port); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 574 | list_for_each_entry_safe(subscriber, subscriber_temp, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 575 | &topsrv.subscriber_list, |
| 576 | subscriber_list) { |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 577 | tipc_ref_lock(subscriber->ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 578 | subscriber_lock = subscriber->lock; |
| 579 | subscr_terminate(subscriber); |
| 580 | spin_unlock_bh(subscriber_lock); |
| 581 | } |
| 582 | tipc_detach(topsrv.user_ref); |
| 583 | topsrv.user_ref = 0; |
| 584 | } |
| 585 | } |
| 586 | |
| 587 | |
| 588 | int tipc_ispublished(struct tipc_name const *name) |
| 589 | { |
| 590 | u32 domain = 0; |
| 591 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 592 | return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 593 | } |
| 594 | |