subscr.c 16 KB

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