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