Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 1 | /* |
Allan Stephens | 5b06c85c | 2008-05-19 13:30:13 -0700 | [diff] [blame] | 2 | * net/tipc/subscr.c: TIPC network topology 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 |
Allan Stephens | 5b06c85c | 2008-05-19 13:30:13 -0700 | [diff] [blame] | 5 | * Copyright (c) 2005-2007, 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" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 38 | #include "name_table.h" |
Allan Stephens | d265fef | 2010-11-30 12:00:53 +0000 | [diff] [blame] | 39 | #include "user_reg.h" |
Allan Stephens | 5b06c85c | 2008-05-19 13:30:13 -0700 | [diff] [blame] | 40 | #include "subscr.h" |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 41 | |
| 42 | /** |
| 43 | * struct subscriber - TIPC network topology subscriber |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 44 | * @port_ref: object reference to server port connecting to subscriber |
| 45 | * @lock: pointer to spinlock controlling access to subscriber's server port |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 46 | * @subscriber_list: adjacent subscribers in top. server's list of subscribers |
| 47 | * @subscription_list: list of subscription objects for this subscriber |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 48 | */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 49 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 50 | struct subscriber { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 51 | u32 port_ref; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 52 | spinlock_t *lock; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 53 | struct list_head subscriber_list; |
| 54 | struct list_head subscription_list; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 55 | }; |
| 56 | |
| 57 | /** |
| 58 | * struct top_srv - TIPC network topology subscription service |
| 59 | * @user_ref: TIPC userid of subscription service |
| 60 | * @setup_port: reference to TIPC port that handles subscription requests |
| 61 | * @subscription_count: number of active subscriptions (not subscribers!) |
| 62 | * @subscriber_list: list of ports subscribing to service |
| 63 | * @lock: spinlock govering access to subscriber list |
| 64 | */ |
| 65 | |
| 66 | struct top_srv { |
| 67 | u32 user_ref; |
| 68 | u32 setup_port; |
| 69 | atomic_t subscription_count; |
| 70 | struct list_head subscriber_list; |
| 71 | spinlock_t lock; |
| 72 | }; |
| 73 | |
| 74 | static struct top_srv topsrv = { 0 }; |
| 75 | |
| 76 | /** |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 77 | * htohl - convert value to endianness used by destination |
| 78 | * @in: value to convert |
| 79 | * @swap: non-zero if endianness must be reversed |
| 80 | * |
| 81 | * Returns converted value |
| 82 | */ |
| 83 | |
| 84 | static u32 htohl(u32 in, int swap) |
| 85 | { |
| 86 | return swap ? swab32(in) : in; |
| 87 | } |
| 88 | |
| 89 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 90 | * subscr_send_event - send a message containing a tipc_event to the subscriber |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 91 | * |
| 92 | * Note: Must not hold subscriber's server port lock, since tipc_send() will |
| 93 | * try to take the lock if the message is rejected and returned! |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 94 | */ |
| 95 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 96 | static void subscr_send_event(struct subscription *sub, |
| 97 | u32 found_lower, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 98 | u32 found_upper, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 99 | u32 event, |
| 100 | u32 port_ref, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 101 | u32 node) |
| 102 | { |
| 103 | struct iovec msg_sect; |
| 104 | |
| 105 | msg_sect.iov_base = (void *)&sub->evt; |
| 106 | msg_sect.iov_len = sizeof(struct tipc_event); |
| 107 | |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 108 | sub->evt.event = htohl(event, sub->swap); |
| 109 | sub->evt.found_lower = htohl(found_lower, sub->swap); |
| 110 | sub->evt.found_upper = htohl(found_upper, sub->swap); |
| 111 | sub->evt.port.ref = htohl(port_ref, sub->swap); |
| 112 | sub->evt.port.node = htohl(node, sub->swap); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 113 | tipc_send(sub->server_ref, 1, &msg_sect); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 114 | } |
| 115 | |
| 116 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 117 | * tipc_subscr_overlap - test for subscription overlap with the given values |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 118 | * |
| 119 | * Returns 1 if there is overlap, otherwise 0. |
| 120 | */ |
| 121 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 122 | int tipc_subscr_overlap(struct subscription *sub, |
| 123 | u32 found_lower, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 124 | u32 found_upper) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 125 | |
| 126 | { |
| 127 | if (found_lower < sub->seq.lower) |
| 128 | found_lower = sub->seq.lower; |
| 129 | if (found_upper > sub->seq.upper) |
| 130 | found_upper = sub->seq.upper; |
| 131 | if (found_lower > found_upper) |
| 132 | return 0; |
| 133 | return 1; |
| 134 | } |
| 135 | |
| 136 | /** |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 137 | * tipc_subscr_report_overlap - issue event if there is subscription overlap |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 138 | * |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 139 | * Protected by nameseq.lock in name_table.c |
| 140 | */ |
| 141 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 142 | void tipc_subscr_report_overlap(struct subscription *sub, |
| 143 | u32 found_lower, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 144 | u32 found_upper, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 145 | u32 event, |
| 146 | u32 port_ref, |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 147 | u32 node, |
| 148 | int must) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 149 | { |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 150 | if (!tipc_subscr_overlap(sub, found_lower, found_upper)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 151 | return; |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 152 | if (!must && !(sub->filter & TIPC_SUB_PORTS)) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 153 | return; |
Allan Stephens | e15f880 | 2008-05-19 13:27:31 -0700 | [diff] [blame] | 154 | |
| 155 | sub->event_cb(sub, found_lower, found_upper, event, port_ref, node); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 156 | } |
| 157 | |
| 158 | /** |
| 159 | * subscr_timeout - subscription timeout has occurred |
| 160 | */ |
| 161 | |
| 162 | static void subscr_timeout(struct subscription *sub) |
| 163 | { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 164 | struct port *server_port; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 165 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 166 | /* Validate server port reference (in case subscriber is terminating) */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 167 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 168 | server_port = tipc_port_lock(sub->server_ref); |
| 169 | if (server_port == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 170 | return; |
| 171 | |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 172 | /* Validate timeout (in case subscription is being cancelled) */ |
| 173 | |
| 174 | if (sub->timeout == TIPC_WAIT_FOREVER) { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 175 | tipc_port_unlock(server_port); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 176 | return; |
| 177 | } |
| 178 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 179 | /* Unlink subscription from name table */ |
| 180 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 181 | tipc_nametbl_unsubscribe(sub); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 182 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 183 | /* Unlink subscription from subscriber */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 184 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 185 | list_del(&sub->subscription_list); |
| 186 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 187 | /* Release subscriber's server port */ |
| 188 | |
| 189 | tipc_port_unlock(server_port); |
| 190 | |
| 191 | /* Notify subscriber of timeout */ |
| 192 | |
| 193 | subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper, |
| 194 | TIPC_SUBSCR_TIMEOUT, 0, 0); |
| 195 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 196 | /* Now destroy subscription */ |
| 197 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 198 | k_term_timer(&sub->timer); |
| 199 | kfree(sub); |
| 200 | atomic_dec(&topsrv.subscription_count); |
| 201 | } |
| 202 | |
| 203 | /** |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 204 | * subscr_del - delete a subscription within a subscription list |
| 205 | * |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 206 | * Called with subscriber port locked. |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 207 | */ |
| 208 | |
| 209 | static void subscr_del(struct subscription *sub) |
| 210 | { |
| 211 | tipc_nametbl_unsubscribe(sub); |
| 212 | list_del(&sub->subscription_list); |
| 213 | kfree(sub); |
| 214 | atomic_dec(&topsrv.subscription_count); |
| 215 | } |
| 216 | |
| 217 | /** |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 218 | * subscr_terminate - terminate communication with a subscriber |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 219 | * |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 220 | * Called with subscriber port locked. Routine must temporarily release lock |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 221 | * to enable subscription timeout routine(s) to finish without deadlocking; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 222 | * 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] | 223 | * (This should work even in the unlikely event some other thread creates |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 224 | * a new object reference in the interim that uses this lock; this routine will |
| 225 | * simply wait for it to be released, then claim it.) |
| 226 | */ |
| 227 | |
| 228 | static void subscr_terminate(struct subscriber *subscriber) |
| 229 | { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 230 | u32 port_ref; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 231 | struct subscription *sub; |
| 232 | struct subscription *sub_temp; |
| 233 | |
| 234 | /* Invalidate subscriber reference */ |
| 235 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 236 | port_ref = subscriber->port_ref; |
| 237 | subscriber->port_ref = 0; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 238 | spin_unlock_bh(subscriber->lock); |
| 239 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 240 | /* Sever connection to subscriber */ |
| 241 | |
| 242 | tipc_shutdown(port_ref); |
| 243 | tipc_deleteport(port_ref); |
| 244 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 245 | /* Destroy any existing subscriptions for subscriber */ |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 246 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 247 | list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list, |
| 248 | subscription_list) { |
| 249 | if (sub->timeout != TIPC_WAIT_FOREVER) { |
| 250 | k_cancel_timer(&sub->timer); |
| 251 | k_term_timer(&sub->timer); |
| 252 | } |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 253 | dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n", |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 254 | sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 255 | subscr_del(sub); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 256 | } |
| 257 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 258 | /* Remove subscriber from topology server's subscriber list */ |
| 259 | |
| 260 | spin_lock_bh(&topsrv.lock); |
| 261 | list_del(&subscriber->subscriber_list); |
| 262 | spin_unlock_bh(&topsrv.lock); |
| 263 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 264 | /* Reclaim subscriber lock */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 265 | |
| 266 | spin_lock_bh(subscriber->lock); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 267 | |
| 268 | /* Now destroy subscriber */ |
| 269 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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 | * |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 276 | * Called with subscriber port locked. Routine must temporarily release lock |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 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) { |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 294 | if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) { |
| 295 | found = 1; |
| 296 | break; |
| 297 | } |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 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 | } |
Neil Horman | 8c97443 | 2010-10-21 01:06:15 +0000 | [diff] [blame] | 311 | dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n", |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 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 | * |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 319 | * Called with subscriber port locked. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 320 | */ |
| 321 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 322 | static struct subscription *subscr_subscribe(struct tipc_subscr *s, |
| 323 | struct subscriber *subscriber) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 324 | { |
| 325 | struct subscription *sub; |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 326 | int swap; |
| 327 | |
| 328 | /* Determine subscriber's endianness */ |
| 329 | |
| 330 | swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE)); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 331 | |
| 332 | /* Detect & process a subscription cancellation request */ |
| 333 | |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 334 | if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) { |
| 335 | s->filter &= ~htohl(TIPC_SUB_CANCEL, swap); |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 336 | subscr_cancel(s, subscriber); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 337 | return NULL; |
Lijun Chen | eb40946 | 2006-10-16 21:59:42 -0700 | [diff] [blame] | 338 | } |
| 339 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 340 | /* Refuse subscription if global limit exceeded */ |
| 341 | |
| 342 | if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 343 | warn("Subscription rejected, subscription limit reached (%u)\n", |
| 344 | tipc_max_subscriptions); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 345 | subscr_terminate(subscriber); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 346 | return NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 347 | } |
| 348 | |
| 349 | /* Allocate subscription object */ |
| 350 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 351 | sub = kmalloc(sizeof(*sub), GFP_ATOMIC); |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 352 | if (!sub) { |
| 353 | warn("Subscription rejected, no memory\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 354 | subscr_terminate(subscriber); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 355 | return NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 356 | } |
| 357 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 358 | /* Initialize subscription object */ |
| 359 | |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 360 | sub->seq.type = htohl(s->seq.type, swap); |
| 361 | sub->seq.lower = htohl(s->seq.lower, swap); |
| 362 | sub->seq.upper = htohl(s->seq.upper, swap); |
| 363 | sub->timeout = htohl(s->timeout, swap); |
| 364 | sub->filter = htohl(s->filter, swap); |
Neil Horman | 8c97443 | 2010-10-21 01:06:15 +0000 | [diff] [blame] | 365 | if ((!(sub->filter & TIPC_SUB_PORTS) == |
| 366 | !(sub->filter & TIPC_SUB_SERVICE)) || |
Joe Perches | f64f9e7 | 2009-11-29 16:55:45 -0800 | [diff] [blame] | 367 | (sub->seq.lower > sub->seq.upper)) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 368 | warn("Subscription rejected, illegal request\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 369 | kfree(sub); |
| 370 | subscr_terminate(subscriber); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 371 | return NULL; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 372 | } |
Allan Stephens | e15f880 | 2008-05-19 13:27:31 -0700 | [diff] [blame] | 373 | sub->event_cb = subscr_send_event; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 374 | INIT_LIST_HEAD(&sub->nameseq_list); |
| 375 | list_add(&sub->subscription_list, &subscriber->subscription_list); |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 376 | sub->server_ref = subscriber->port_ref; |
Neil Horman | db5a753 | 2010-10-21 01:06:16 +0000 | [diff] [blame] | 377 | sub->swap = swap; |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 378 | memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr)); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 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 | } |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 385 | |
| 386 | return 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 |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 391 | * |
| 392 | * Called with subscriber's server port unlocked. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 393 | */ |
| 394 | |
| 395 | static void subscr_conn_shutdown_event(void *usr_handle, |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 396 | u32 port_ref, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 397 | struct sk_buff **buf, |
| 398 | unsigned char const *data, |
| 399 | unsigned int size, |
| 400 | int reason) |
| 401 | { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 402 | struct subscriber *subscriber = usr_handle; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 403 | spinlock_t *subscriber_lock; |
| 404 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 405 | if (tipc_port_lock(port_ref) == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 406 | return; |
| 407 | |
| 408 | subscriber_lock = subscriber->lock; |
| 409 | subscr_terminate(subscriber); |
| 410 | spin_unlock_bh(subscriber_lock); |
| 411 | } |
| 412 | |
| 413 | /** |
| 414 | * subscr_conn_msg_event - handle new subscription request from subscriber |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 415 | * |
| 416 | * Called with subscriber's server port unlocked. |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 417 | */ |
| 418 | |
| 419 | static void subscr_conn_msg_event(void *usr_handle, |
| 420 | u32 port_ref, |
| 421 | struct sk_buff **buf, |
| 422 | const unchar *data, |
| 423 | u32 size) |
| 424 | { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 425 | struct subscriber *subscriber = usr_handle; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 426 | spinlock_t *subscriber_lock; |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 427 | struct subscription *sub; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 428 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 429 | /* |
| 430 | * Lock subscriber's server port (& make a local copy of lock pointer, |
| 431 | * in case subscriber is deleted while processing subscription request) |
| 432 | */ |
| 433 | |
| 434 | if (tipc_port_lock(port_ref) == NULL) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 435 | return; |
| 436 | |
| 437 | subscriber_lock = subscriber->lock; |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 438 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 439 | if (size != sizeof(struct tipc_subscr)) { |
| 440 | subscr_terminate(subscriber); |
| 441 | spin_unlock_bh(subscriber_lock); |
| 442 | } else { |
| 443 | sub = subscr_subscribe((struct tipc_subscr *)data, subscriber); |
| 444 | spin_unlock_bh(subscriber_lock); |
| 445 | if (sub != NULL) { |
| 446 | |
| 447 | /* |
| 448 | * We must release the server port lock before adding a |
| 449 | * subscription to the name table since TIPC needs to be |
| 450 | * able to (re)acquire the port lock if an event message |
| 451 | * issued by the subscription process is rejected and |
| 452 | * returned. The subscription cannot be deleted while |
| 453 | * it is being added to the name table because: |
| 454 | * a) the single-threading of the native API port code |
| 455 | * ensures the subscription cannot be cancelled and |
| 456 | * the subscriber connection cannot be broken, and |
| 457 | * b) the name table lock ensures the subscription |
| 458 | * timeout code cannot delete the subscription, |
| 459 | * so the subscription object is still protected. |
| 460 | */ |
| 461 | |
| 462 | tipc_nametbl_subscribe(sub); |
| 463 | } |
| 464 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 465 | } |
| 466 | |
| 467 | /** |
| 468 | * subscr_named_msg_event - handle request to establish a new subscriber |
| 469 | */ |
| 470 | |
| 471 | static void subscr_named_msg_event(void *usr_handle, |
| 472 | u32 port_ref, |
| 473 | struct sk_buff **buf, |
| 474 | const unchar *data, |
| 475 | u32 size, |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 476 | u32 importance, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 477 | struct tipc_portid const *orig, |
| 478 | struct tipc_name_seq const *dest) |
| 479 | { |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 480 | static struct iovec msg_sect = {NULL, 0}; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 481 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 482 | struct subscriber *subscriber; |
| 483 | u32 server_port_ref; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 484 | |
| 485 | /* Create subscriber object */ |
| 486 | |
Panagiotis Issaris | 0da974f | 2006-07-21 14:51:30 -0700 | [diff] [blame] | 487 | subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 488 | if (subscriber == NULL) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 489 | warn("Subscriber rejected, no memory\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 490 | return; |
| 491 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 492 | INIT_LIST_HEAD(&subscriber->subscription_list); |
| 493 | INIT_LIST_HEAD(&subscriber->subscriber_list); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 494 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 495 | /* Create server port & establish connection to subscriber */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 496 | |
| 497 | tipc_createport(topsrv.user_ref, |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 498 | subscriber, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 499 | importance, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 500 | NULL, |
| 501 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 502 | subscr_conn_shutdown_event, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 503 | NULL, |
| 504 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 505 | subscr_conn_msg_event, |
Sam Ravnborg | 1fc54d8 | 2006-03-20 22:36:47 -0800 | [diff] [blame] | 506 | NULL, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 507 | &subscriber->port_ref); |
| 508 | if (subscriber->port_ref == 0) { |
Allan Stephens | a10bd92 | 2006-06-25 23:52:17 -0700 | [diff] [blame] | 509 | warn("Subscriber rejected, unable to create port\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 510 | kfree(subscriber); |
| 511 | return; |
| 512 | } |
| 513 | tipc_connect2port(subscriber->port_ref, orig); |
| 514 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 515 | /* Lock server port (& save lock address for future use) */ |
| 516 | |
| 517 | subscriber->lock = tipc_port_lock(subscriber->port_ref)->publ.lock; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 518 | |
| 519 | /* Add subscriber to topology server's subscriber list */ |
| 520 | |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 521 | spin_lock_bh(&topsrv.lock); |
| 522 | list_add(&subscriber->subscriber_list, &topsrv.subscriber_list); |
| 523 | spin_unlock_bh(&topsrv.lock); |
| 524 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 525 | /* Unlock server port */ |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 526 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 527 | server_port_ref = subscriber->port_ref; |
| 528 | spin_unlock_bh(subscriber->lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 529 | |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 530 | /* Send an ACK- to complete connection handshaking */ |
| 531 | |
| 532 | tipc_send(server_port_ref, 1, &msg_sect); |
| 533 | |
| 534 | /* Handle optional subscription request */ |
| 535 | |
| 536 | if (size != 0) { |
| 537 | subscr_conn_msg_event(subscriber, server_port_ref, |
| 538 | buf, data, size); |
| 539 | } |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 540 | } |
| 541 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 542 | int tipc_subscr_start(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 543 | { |
| 544 | struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV}; |
Allan Stephens | 52fe7b7 | 2010-11-30 12:01:00 +0000 | [diff] [blame^] | 545 | int res; |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 546 | |
| 547 | memset(&topsrv, 0, sizeof (topsrv)); |
Ingo Molnar | 34af946 | 2006-06-27 02:53:55 -0700 | [diff] [blame] | 548 | spin_lock_init(&topsrv.lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 549 | INIT_LIST_HEAD(&topsrv.subscriber_list); |
| 550 | |
| 551 | spin_lock_bh(&topsrv.lock); |
Allan Stephens | a5c2af9 | 2010-11-30 12:00:58 +0000 | [diff] [blame] | 552 | res = tipc_attach(&topsrv.user_ref); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 553 | if (res) { |
| 554 | spin_unlock_bh(&topsrv.lock); |
| 555 | return res; |
| 556 | } |
| 557 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 558 | res = tipc_createport(topsrv.user_ref, |
| 559 | NULL, |
| 560 | TIPC_CRITICAL_IMPORTANCE, |
| 561 | NULL, |
| 562 | NULL, |
| 563 | NULL, |
| 564 | NULL, |
| 565 | subscr_named_msg_event, |
| 566 | NULL, |
| 567 | NULL, |
| 568 | &topsrv.setup_port); |
| 569 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 570 | goto failed; |
| 571 | |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 572 | res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq); |
| 573 | if (res) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 574 | goto failed; |
| 575 | |
| 576 | spin_unlock_bh(&topsrv.lock); |
| 577 | return 0; |
| 578 | |
| 579 | failed: |
Per Liden | d0a14a9 | 2006-01-11 13:52:51 +0100 | [diff] [blame] | 580 | err("Failed to create subscription service\n"); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 581 | tipc_detach(topsrv.user_ref); |
| 582 | topsrv.user_ref = 0; |
| 583 | spin_unlock_bh(&topsrv.lock); |
| 584 | return res; |
| 585 | } |
| 586 | |
Per Liden | 4323add | 2006-01-18 00:38:21 +0100 | [diff] [blame] | 587 | void tipc_subscr_stop(void) |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 588 | { |
| 589 | struct subscriber *subscriber; |
| 590 | struct subscriber *subscriber_temp; |
| 591 | spinlock_t *subscriber_lock; |
| 592 | |
| 593 | if (topsrv.user_ref) { |
| 594 | tipc_deleteport(topsrv.setup_port); |
YOSHIFUJI Hideaki | c430728 | 2007-02-09 23:25:21 +0900 | [diff] [blame] | 595 | list_for_each_entry_safe(subscriber, subscriber_temp, |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 596 | &topsrv.subscriber_list, |
| 597 | subscriber_list) { |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 598 | subscriber_lock = subscriber->lock; |
Allan Stephens | 28353e7 | 2008-05-19 13:29:47 -0700 | [diff] [blame] | 599 | spin_lock_bh(subscriber_lock); |
Per Liden | b97bf3f | 2006-01-02 19:04:38 +0100 | [diff] [blame] | 600 | subscr_terminate(subscriber); |
| 601 | spin_unlock_bh(subscriber_lock); |
| 602 | } |
| 603 | tipc_detach(topsrv.user_ref); |
| 604 | topsrv.user_ref = 0; |
| 605 | } |
| 606 | } |