subscr.c 16 KB

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