subscr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589
  1. /*
  2. * net/tipc/subscr.c: TIPC network topology service
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005-2007, 2010-2011, Wind River Systems
  6. * All rights reserved.
  7. *
  8. * Redistribution and use in source and binary forms, with or without
  9. * modification, are permitted provided that the following conditions are met:
  10. *
  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.
  19. *
  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
  34. * POSSIBILITY OF SUCH DAMAGE.
  35. */
  36. #include "core.h"
  37. #include "name_table.h"
  38. #include "port.h"
  39. #include "subscr.h"
  40. /**
  41. * struct tipc_subscriber - TIPC network topology subscriber
  42. * @port_ref: object reference to server port connecting to subscriber
  43. * @lock: pointer to spinlock controlling access to subscriber's server port
  44. * @subscriber_list: adjacent subscribers in top. server's list of subscribers
  45. * @subscription_list: list of subscription objects for this subscriber
  46. */
  47. struct tipc_subscriber {
  48. u32 port_ref;
  49. spinlock_t *lock;
  50. struct list_head subscriber_list;
  51. struct list_head subscription_list;
  52. };
  53. /**
  54. * struct top_srv - TIPC network topology subscription service
  55. * @user_ref: TIPC userid of subscription service
  56. * @setup_port: reference to TIPC port that handles subscription requests
  57. * @subscription_count: number of active subscriptions (not subscribers!)
  58. * @subscriber_list: list of ports subscribing to service
  59. * @lock: spinlock govering access to subscriber list
  60. */
  61. struct top_srv {
  62. u32 setup_port;
  63. atomic_t subscription_count;
  64. struct list_head subscriber_list;
  65. spinlock_t lock;
  66. };
  67. static struct top_srv topsrv;
  68. /**
  69. * htohl - convert value to endianness used by destination
  70. * @in: value to convert
  71. * @swap: non-zero if endianness must be reversed
  72. *
  73. * Returns converted value
  74. */
  75. static u32 htohl(u32 in, int swap)
  76. {
  77. return swap ? swab32(in) : in;
  78. }
  79. /**
  80. * subscr_send_event - send a message containing a tipc_event to the subscriber
  81. *
  82. * Note: Must not hold subscriber's server port lock, since tipc_send() will
  83. * try to take the lock if the message is rejected and returned!
  84. */
  85. static void subscr_send_event(struct tipc_subscription *sub,
  86. u32 found_lower,
  87. u32 found_upper,
  88. u32 event,
  89. u32 port_ref,
  90. u32 node)
  91. {
  92. struct iovec msg_sect;
  93. msg_sect.iov_base = (void *)&sub->evt;
  94. msg_sect.iov_len = sizeof(struct tipc_event);
  95. sub->evt.event = htohl(event, sub->swap);
  96. sub->evt.found_lower = htohl(found_lower, sub->swap);
  97. sub->evt.found_upper = htohl(found_upper, sub->swap);
  98. sub->evt.port.ref = htohl(port_ref, sub->swap);
  99. sub->evt.port.node = htohl(node, sub->swap);
  100. tipc_send(sub->server_ref, 1, &msg_sect, msg_sect.iov_len);
  101. }
  102. /**
  103. * tipc_subscr_overlap - test for subscription overlap with the given values
  104. *
  105. * Returns 1 if there is overlap, otherwise 0.
  106. */
  107. int tipc_subscr_overlap(struct tipc_subscription *sub,
  108. u32 found_lower,
  109. u32 found_upper)
  110. {
  111. if (found_lower < sub->seq.lower)
  112. found_lower = sub->seq.lower;
  113. if (found_upper > sub->seq.upper)
  114. found_upper = sub->seq.upper;
  115. if (found_lower > found_upper)
  116. return 0;
  117. return 1;
  118. }
  119. /**
  120. * tipc_subscr_report_overlap - issue event if there is subscription overlap
  121. *
  122. * Protected by nameseq.lock in name_table.c
  123. */
  124. void tipc_subscr_report_overlap(struct tipc_subscription *sub,
  125. u32 found_lower,
  126. u32 found_upper,
  127. u32 event,
  128. u32 port_ref,
  129. u32 node,
  130. int must)
  131. {
  132. if (!tipc_subscr_overlap(sub, found_lower, found_upper))
  133. return;
  134. if (!must && !(sub->filter & TIPC_SUB_PORTS))
  135. return;
  136. subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
  137. }
  138. /**
  139. * subscr_timeout - subscription timeout has occurred
  140. */
  141. static void subscr_timeout(struct tipc_subscription *sub)
  142. {
  143. struct tipc_port *server_port;
  144. /* Validate server port reference (in case subscriber is terminating) */
  145. server_port = tipc_port_lock(sub->server_ref);
  146. if (server_port == NULL)
  147. return;
  148. /* Validate timeout (in case subscription is being cancelled) */
  149. if (sub->timeout == TIPC_WAIT_FOREVER) {
  150. tipc_port_unlock(server_port);
  151. return;
  152. }
  153. /* Unlink subscription from name table */
  154. tipc_nametbl_unsubscribe(sub);
  155. /* Unlink subscription from subscriber */
  156. list_del(&sub->subscription_list);
  157. /* Release subscriber's server port */
  158. tipc_port_unlock(server_port);
  159. /* Notify subscriber of timeout */
  160. subscr_send_event(sub, sub->evt.s.seq.lower, sub->evt.s.seq.upper,
  161. TIPC_SUBSCR_TIMEOUT, 0, 0);
  162. /* Now destroy subscription */
  163. k_term_timer(&sub->timer);
  164. kfree(sub);
  165. atomic_dec(&topsrv.subscription_count);
  166. }
  167. /**
  168. * subscr_del - delete a subscription within a subscription list
  169. *
  170. * Called with subscriber port locked.
  171. */
  172. static void subscr_del(struct tipc_subscription *sub)
  173. {
  174. tipc_nametbl_unsubscribe(sub);
  175. list_del(&sub->subscription_list);
  176. kfree(sub);
  177. atomic_dec(&topsrv.subscription_count);
  178. }
  179. /**
  180. * subscr_terminate - terminate communication with a subscriber
  181. *
  182. * Called with subscriber port locked. Routine must temporarily release lock
  183. * to enable subscription timeout routine(s) to finish without deadlocking;
  184. * the lock is then reclaimed to allow caller to release it upon return.
  185. * (This should work even in the unlikely event some other thread creates
  186. * a new object reference in the interim that uses this lock; this routine will
  187. * simply wait for it to be released, then claim it.)
  188. */
  189. static void subscr_terminate(struct tipc_subscriber *subscriber)
  190. {
  191. u32 port_ref;
  192. struct tipc_subscription *sub;
  193. struct tipc_subscription *sub_temp;
  194. /* Invalidate subscriber reference */
  195. port_ref = subscriber->port_ref;
  196. subscriber->port_ref = 0;
  197. spin_unlock_bh(subscriber->lock);
  198. /* Sever connection to subscriber */
  199. tipc_shutdown(port_ref);
  200. tipc_deleteport(port_ref);
  201. /* Destroy any existing subscriptions for subscriber */
  202. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  203. subscription_list) {
  204. if (sub->timeout != TIPC_WAIT_FOREVER) {
  205. k_cancel_timer(&sub->timer);
  206. k_term_timer(&sub->timer);
  207. }
  208. subscr_del(sub);
  209. }
  210. /* Remove subscriber from topology server's subscriber list */
  211. spin_lock_bh(&topsrv.lock);
  212. list_del(&subscriber->subscriber_list);
  213. spin_unlock_bh(&topsrv.lock);
  214. /* Reclaim subscriber lock */
  215. spin_lock_bh(subscriber->lock);
  216. /* Now destroy subscriber */
  217. kfree(subscriber);
  218. }
  219. /**
  220. * subscr_cancel - handle subscription cancellation request
  221. *
  222. * Called with subscriber port locked. Routine must temporarily release lock
  223. * to enable the subscription timeout routine to finish without deadlocking;
  224. * the lock is then reclaimed to allow caller to release it upon return.
  225. *
  226. * Note that fields of 's' use subscriber's endianness!
  227. */
  228. static void subscr_cancel(struct tipc_subscr *s,
  229. struct tipc_subscriber *subscriber)
  230. {
  231. struct tipc_subscription *sub;
  232. struct tipc_subscription *sub_temp;
  233. int found = 0;
  234. /* Find first matching subscription, exit if not found */
  235. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  236. subscription_list) {
  237. if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
  238. found = 1;
  239. break;
  240. }
  241. }
  242. if (!found)
  243. return;
  244. /* Cancel subscription timer (if used), then delete subscription */
  245. if (sub->timeout != TIPC_WAIT_FOREVER) {
  246. sub->timeout = TIPC_WAIT_FOREVER;
  247. spin_unlock_bh(subscriber->lock);
  248. k_cancel_timer(&sub->timer);
  249. k_term_timer(&sub->timer);
  250. spin_lock_bh(subscriber->lock);
  251. }
  252. subscr_del(sub);
  253. }
  254. /**
  255. * subscr_subscribe - create subscription for subscriber
  256. *
  257. * Called with subscriber port locked.
  258. */
  259. static struct tipc_subscription *subscr_subscribe(struct tipc_subscr *s,
  260. struct tipc_subscriber *subscriber)
  261. {
  262. struct tipc_subscription *sub;
  263. int swap;
  264. /* Determine subscriber's endianness */
  265. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
  266. /* Detect & process a subscription cancellation request */
  267. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  268. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  269. subscr_cancel(s, subscriber);
  270. return NULL;
  271. }
  272. /* Refuse subscription if global limit exceeded */
  273. if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
  274. warn("Subscription rejected, subscription limit reached (%u)\n",
  275. tipc_max_subscriptions);
  276. subscr_terminate(subscriber);
  277. return NULL;
  278. }
  279. /* Allocate subscription object */
  280. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  281. if (!sub) {
  282. warn("Subscription rejected, no memory\n");
  283. subscr_terminate(subscriber);
  284. return NULL;
  285. }
  286. /* Initialize subscription object */
  287. sub->seq.type = htohl(s->seq.type, swap);
  288. sub->seq.lower = htohl(s->seq.lower, swap);
  289. sub->seq.upper = htohl(s->seq.upper, swap);
  290. sub->timeout = htohl(s->timeout, swap);
  291. sub->filter = htohl(s->filter, swap);
  292. if ((!(sub->filter & TIPC_SUB_PORTS) ==
  293. !(sub->filter & TIPC_SUB_SERVICE)) ||
  294. (sub->seq.lower > sub->seq.upper)) {
  295. warn("Subscription rejected, illegal request\n");
  296. kfree(sub);
  297. subscr_terminate(subscriber);
  298. return NULL;
  299. }
  300. INIT_LIST_HEAD(&sub->nameseq_list);
  301. list_add(&sub->subscription_list, &subscriber->subscription_list);
  302. sub->server_ref = subscriber->port_ref;
  303. sub->swap = swap;
  304. memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
  305. atomic_inc(&topsrv.subscription_count);
  306. if (sub->timeout != TIPC_WAIT_FOREVER) {
  307. k_init_timer(&sub->timer,
  308. (Handler)subscr_timeout, (unsigned long)sub);
  309. k_start_timer(&sub->timer, sub->timeout);
  310. }
  311. return sub;
  312. }
  313. /**
  314. * subscr_conn_shutdown_event - handle termination request from subscriber
  315. *
  316. * Called with subscriber's server port unlocked.
  317. */
  318. static void subscr_conn_shutdown_event(void *usr_handle,
  319. u32 port_ref,
  320. struct sk_buff **buf,
  321. unsigned char const *data,
  322. unsigned int size,
  323. int reason)
  324. {
  325. struct tipc_subscriber *subscriber = usr_handle;
  326. spinlock_t *subscriber_lock;
  327. if (tipc_port_lock(port_ref) == NULL)
  328. return;
  329. subscriber_lock = subscriber->lock;
  330. subscr_terminate(subscriber);
  331. spin_unlock_bh(subscriber_lock);
  332. }
  333. /**
  334. * subscr_conn_msg_event - handle new subscription request from subscriber
  335. *
  336. * Called with subscriber's server port unlocked.
  337. */
  338. static void subscr_conn_msg_event(void *usr_handle,
  339. u32 port_ref,
  340. struct sk_buff **buf,
  341. const unchar *data,
  342. u32 size)
  343. {
  344. struct tipc_subscriber *subscriber = usr_handle;
  345. spinlock_t *subscriber_lock;
  346. struct tipc_subscription *sub;
  347. /*
  348. * Lock subscriber's server port (& make a local copy of lock pointer,
  349. * in case subscriber is deleted while processing subscription request)
  350. */
  351. if (tipc_port_lock(port_ref) == NULL)
  352. return;
  353. subscriber_lock = subscriber->lock;
  354. if (size != sizeof(struct tipc_subscr)) {
  355. subscr_terminate(subscriber);
  356. spin_unlock_bh(subscriber_lock);
  357. } else {
  358. sub = subscr_subscribe((struct tipc_subscr *)data, subscriber);
  359. spin_unlock_bh(subscriber_lock);
  360. if (sub != NULL) {
  361. /*
  362. * We must release the server port lock before adding a
  363. * subscription to the name table since TIPC needs to be
  364. * able to (re)acquire the port lock if an event message
  365. * issued by the subscription process is rejected and
  366. * returned. The subscription cannot be deleted while
  367. * it is being added to the name table because:
  368. * a) the single-threading of the native API port code
  369. * ensures the subscription cannot be cancelled and
  370. * the subscriber connection cannot be broken, and
  371. * b) the name table lock ensures the subscription
  372. * timeout code cannot delete the subscription,
  373. * so the subscription object is still protected.
  374. */
  375. tipc_nametbl_subscribe(sub);
  376. }
  377. }
  378. }
  379. /**
  380. * subscr_named_msg_event - handle request to establish a new subscriber
  381. */
  382. static void subscr_named_msg_event(void *usr_handle,
  383. u32 port_ref,
  384. struct sk_buff **buf,
  385. const unchar *data,
  386. u32 size,
  387. u32 importance,
  388. struct tipc_portid const *orig,
  389. struct tipc_name_seq const *dest)
  390. {
  391. struct tipc_subscriber *subscriber;
  392. u32 server_port_ref;
  393. /* Create subscriber object */
  394. subscriber = kzalloc(sizeof(struct tipc_subscriber), GFP_ATOMIC);
  395. if (subscriber == NULL) {
  396. warn("Subscriber rejected, no memory\n");
  397. return;
  398. }
  399. INIT_LIST_HEAD(&subscriber->subscription_list);
  400. INIT_LIST_HEAD(&subscriber->subscriber_list);
  401. /* Create server port & establish connection to subscriber */
  402. tipc_createport(subscriber,
  403. importance,
  404. NULL,
  405. NULL,
  406. subscr_conn_shutdown_event,
  407. NULL,
  408. NULL,
  409. subscr_conn_msg_event,
  410. NULL,
  411. &subscriber->port_ref);
  412. if (subscriber->port_ref == 0) {
  413. warn("Subscriber rejected, unable to create port\n");
  414. kfree(subscriber);
  415. return;
  416. }
  417. tipc_connect2port(subscriber->port_ref, orig);
  418. /* Lock server port (& save lock address for future use) */
  419. subscriber->lock = tipc_port_lock(subscriber->port_ref)->lock;
  420. /* Add subscriber to topology server's subscriber list */
  421. spin_lock_bh(&topsrv.lock);
  422. list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
  423. spin_unlock_bh(&topsrv.lock);
  424. /* Unlock server port */
  425. server_port_ref = subscriber->port_ref;
  426. spin_unlock_bh(subscriber->lock);
  427. /* Send an ACK- to complete connection handshaking */
  428. tipc_send(server_port_ref, 0, NULL, 0);
  429. /* Handle optional subscription request */
  430. if (size != 0) {
  431. subscr_conn_msg_event(subscriber, server_port_ref,
  432. buf, data, size);
  433. }
  434. }
  435. int tipc_subscr_start(void)
  436. {
  437. struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
  438. int res;
  439. memset(&topsrv, 0, sizeof(topsrv));
  440. spin_lock_init(&topsrv.lock);
  441. INIT_LIST_HEAD(&topsrv.subscriber_list);
  442. res = tipc_createport(NULL,
  443. TIPC_CRITICAL_IMPORTANCE,
  444. NULL,
  445. NULL,
  446. NULL,
  447. NULL,
  448. subscr_named_msg_event,
  449. NULL,
  450. NULL,
  451. &topsrv.setup_port);
  452. if (res)
  453. goto failed;
  454. res = tipc_publish(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
  455. if (res) {
  456. tipc_deleteport(topsrv.setup_port);
  457. topsrv.setup_port = 0;
  458. goto failed;
  459. }
  460. return 0;
  461. failed:
  462. err("Failed to create subscription service\n");
  463. return res;
  464. }
  465. void tipc_subscr_stop(void)
  466. {
  467. struct tipc_subscriber *subscriber;
  468. struct tipc_subscriber *subscriber_temp;
  469. spinlock_t *subscriber_lock;
  470. if (topsrv.setup_port) {
  471. tipc_deleteport(topsrv.setup_port);
  472. topsrv.setup_port = 0;
  473. list_for_each_entry_safe(subscriber, subscriber_temp,
  474. &topsrv.subscriber_list,
  475. subscriber_list) {
  476. subscriber_lock = subscriber->lock;
  477. spin_lock_bh(subscriber_lock);
  478. subscr_terminate(subscriber);
  479. spin_unlock_bh(subscriber_lock);
  480. }
  481. }
  482. }