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