subscr.c 16 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592
  1. /*
  2. * net/tipc/subscr.c: TIPC subscription service
  3. *
  4. * Copyright (c) 2000-2006, Ericsson AB
  5. * Copyright (c) 2005, 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 "subscr.h"
  39. #include "name_table.h"
  40. #include "ref.h"
  41. /**
  42. * struct subscriber - TIPC network topology subscriber
  43. * @ref: object reference to subscriber object itself
  44. * @lock: pointer to spinlock controlling access to subscriber object
  45. * @subscriber_list: adjacent subscribers in top. server's list of subscribers
  46. * @subscription_list: list of subscription objects for this subscriber
  47. * @port_ref: object reference to port used to communicate with subscriber
  48. */
  49. struct subscriber {
  50. u32 ref;
  51. spinlock_t *lock;
  52. struct list_head subscriber_list;
  53. struct list_head subscription_list;
  54. u32 port_ref;
  55. };
  56. /**
  57. * struct top_srv - TIPC network topology subscription service
  58. * @user_ref: TIPC userid of subscription service
  59. * @setup_port: reference to TIPC port that handles subscription requests
  60. * @subscription_count: number of active subscriptions (not subscribers!)
  61. * @subscriber_list: list of ports subscribing to service
  62. * @lock: spinlock govering access to subscriber list
  63. */
  64. struct top_srv {
  65. u32 user_ref;
  66. u32 setup_port;
  67. atomic_t subscription_count;
  68. struct list_head subscriber_list;
  69. spinlock_t lock;
  70. };
  71. static struct top_srv topsrv = { 0 };
  72. /**
  73. * htohl - convert value to endianness used by destination
  74. * @in: value to convert
  75. * @swap: non-zero if endianness must be reversed
  76. *
  77. * Returns converted value
  78. */
  79. static u32 htohl(u32 in, int swap)
  80. {
  81. return swap ? (u32)___constant_swab32(in) : in;
  82. }
  83. /**
  84. * subscr_send_event - send a message containing a tipc_event to the subscriber
  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->owner->port_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. dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
  134. sub->seq.upper, found_lower, found_upper);
  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 subscriber *subscriber;
  147. u32 subscriber_ref;
  148. /* Validate subscriber reference (in case subscriber is terminating) */
  149. subscriber_ref = sub->owner->ref;
  150. subscriber = (struct subscriber *)tipc_ref_lock(subscriber_ref);
  151. if (subscriber == NULL)
  152. return;
  153. /* Validate timeout (in case subscription is being cancelled) */
  154. if (sub->timeout == TIPC_WAIT_FOREVER) {
  155. tipc_ref_unlock(subscriber_ref);
  156. return;
  157. }
  158. /* Unlink subscription from name table */
  159. tipc_nametbl_unsubscribe(sub);
  160. /* Notify subscriber of timeout, then unlink subscription */
  161. subscr_send_event(sub,
  162. sub->evt.s.seq.lower,
  163. sub->evt.s.seq.upper,
  164. TIPC_SUBSCR_TIMEOUT,
  165. 0,
  166. 0);
  167. list_del(&sub->subscription_list);
  168. /* Now destroy subscription */
  169. tipc_ref_unlock(subscriber_ref);
  170. k_term_timer(&sub->timer);
  171. kfree(sub);
  172. atomic_dec(&topsrv.subscription_count);
  173. }
  174. /**
  175. * subscr_del - delete a subscription within a subscription list
  176. *
  177. * Called with subscriber locked.
  178. */
  179. static void subscr_del(struct subscription *sub)
  180. {
  181. tipc_nametbl_unsubscribe(sub);
  182. list_del(&sub->subscription_list);
  183. kfree(sub);
  184. atomic_dec(&topsrv.subscription_count);
  185. }
  186. /**
  187. * subscr_terminate - terminate communication with a subscriber
  188. *
  189. * Called with subscriber locked. Routine must temporarily release this lock
  190. * to enable subscription timeout routine(s) to finish without deadlocking;
  191. * the lock is then reclaimed to allow caller to release it upon return.
  192. * (This should work even in the unlikely event some other thread creates
  193. * a new object reference in the interim that uses this lock; this routine will
  194. * simply wait for it to be released, then claim it.)
  195. */
  196. static void subscr_terminate(struct subscriber *subscriber)
  197. {
  198. struct subscription *sub;
  199. struct subscription *sub_temp;
  200. /* Invalidate subscriber reference */
  201. tipc_ref_discard(subscriber->ref);
  202. spin_unlock_bh(subscriber->lock);
  203. /* Destroy any existing subscriptions for subscriber */
  204. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  205. subscription_list) {
  206. if (sub->timeout != TIPC_WAIT_FOREVER) {
  207. k_cancel_timer(&sub->timer);
  208. k_term_timer(&sub->timer);
  209. }
  210. dbg("Term: Removing sub %u,%u,%u from subscriber %x list\n",
  211. sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
  212. subscr_del(sub);
  213. }
  214. /* Sever connection to subscriber */
  215. tipc_shutdown(subscriber->port_ref);
  216. tipc_deleteport(subscriber->port_ref);
  217. /* Remove subscriber from topology server's subscriber list */
  218. spin_lock_bh(&topsrv.lock);
  219. list_del(&subscriber->subscriber_list);
  220. spin_unlock_bh(&topsrv.lock);
  221. /* Now destroy subscriber */
  222. spin_lock_bh(subscriber->lock);
  223. kfree(subscriber);
  224. }
  225. /**
  226. * subscr_cancel - handle subscription cancellation request
  227. *
  228. * Called with subscriber locked. Routine must temporarily release this lock
  229. * to enable the subscription timeout routine to finish without deadlocking;
  230. * the lock is then reclaimed to allow caller to release it upon return.
  231. *
  232. * Note that fields of 's' use subscriber's endianness!
  233. */
  234. static void subscr_cancel(struct tipc_subscr *s,
  235. struct subscriber *subscriber)
  236. {
  237. struct subscription *sub;
  238. struct subscription *sub_temp;
  239. int found = 0;
  240. /* Find first matching subscription, exit if not found */
  241. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  242. subscription_list) {
  243. if (!memcmp(s, &sub->evt.s, sizeof(struct tipc_subscr))) {
  244. found = 1;
  245. break;
  246. }
  247. }
  248. if (!found)
  249. return;
  250. /* Cancel subscription timer (if used), then delete subscription */
  251. if (sub->timeout != TIPC_WAIT_FOREVER) {
  252. sub->timeout = TIPC_WAIT_FOREVER;
  253. spin_unlock_bh(subscriber->lock);
  254. k_cancel_timer(&sub->timer);
  255. k_term_timer(&sub->timer);
  256. spin_lock_bh(subscriber->lock);
  257. }
  258. dbg("Cancel: removing sub %u,%u,%u from subscriber %x list\n",
  259. sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
  260. subscr_del(sub);
  261. }
  262. /**
  263. * subscr_subscribe - create subscription for subscriber
  264. *
  265. * Called with subscriber locked
  266. */
  267. static void subscr_subscribe(struct tipc_subscr *s,
  268. struct subscriber *subscriber)
  269. {
  270. struct subscription *sub;
  271. int swap;
  272. /* Determine subscriber's endianness */
  273. swap = !(s->filter & (TIPC_SUB_PORTS | TIPC_SUB_SERVICE));
  274. /* Detect & process a subscription cancellation request */
  275. if (s->filter & htohl(TIPC_SUB_CANCEL, swap)) {
  276. s->filter &= ~htohl(TIPC_SUB_CANCEL, swap);
  277. subscr_cancel(s, subscriber);
  278. return;
  279. }
  280. /* Refuse subscription if global limit exceeded */
  281. if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
  282. warn("Subscription rejected, subscription limit reached (%u)\n",
  283. tipc_max_subscriptions);
  284. subscr_terminate(subscriber);
  285. return;
  286. }
  287. /* Allocate subscription object */
  288. sub = kzalloc(sizeof(*sub), GFP_ATOMIC);
  289. if (!sub) {
  290. warn("Subscription rejected, no memory\n");
  291. subscr_terminate(subscriber);
  292. return;
  293. }
  294. /* Initialize subscription object */
  295. sub->seq.type = htohl(s->seq.type, swap);
  296. sub->seq.lower = htohl(s->seq.lower, swap);
  297. sub->seq.upper = htohl(s->seq.upper, swap);
  298. sub->timeout = htohl(s->timeout, swap);
  299. sub->filter = htohl(s->filter, swap);
  300. if ((!(sub->filter & TIPC_SUB_PORTS)
  301. == !(sub->filter & TIPC_SUB_SERVICE))
  302. || (sub->seq.lower > sub->seq.upper)) {
  303. warn("Subscription rejected, illegal request\n");
  304. kfree(sub);
  305. subscr_terminate(subscriber);
  306. return;
  307. }
  308. sub->event_cb = subscr_send_event;
  309. memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
  310. INIT_LIST_HEAD(&sub->subscription_list);
  311. INIT_LIST_HEAD(&sub->nameseq_list);
  312. list_add(&sub->subscription_list, &subscriber->subscription_list);
  313. sub->swap = swap;
  314. atomic_inc(&topsrv.subscription_count);
  315. if (sub->timeout != TIPC_WAIT_FOREVER) {
  316. k_init_timer(&sub->timer,
  317. (Handler)subscr_timeout, (unsigned long)sub);
  318. k_start_timer(&sub->timer, sub->timeout);
  319. }
  320. sub->owner = subscriber;
  321. tipc_nametbl_subscribe(sub);
  322. }
  323. /**
  324. * subscr_conn_shutdown_event - handle termination request from subscriber
  325. */
  326. static void subscr_conn_shutdown_event(void *usr_handle,
  327. u32 portref,
  328. struct sk_buff **buf,
  329. unsigned char const *data,
  330. unsigned int size,
  331. int reason)
  332. {
  333. struct subscriber *subscriber;
  334. spinlock_t *subscriber_lock;
  335. subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
  336. if (subscriber == NULL)
  337. return;
  338. subscriber_lock = subscriber->lock;
  339. subscr_terminate(subscriber);
  340. spin_unlock_bh(subscriber_lock);
  341. }
  342. /**
  343. * subscr_conn_msg_event - handle new subscription request from subscriber
  344. */
  345. static void subscr_conn_msg_event(void *usr_handle,
  346. u32 port_ref,
  347. struct sk_buff **buf,
  348. const unchar *data,
  349. u32 size)
  350. {
  351. struct subscriber *subscriber;
  352. spinlock_t *subscriber_lock;
  353. subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
  354. if (subscriber == NULL)
  355. return;
  356. subscriber_lock = subscriber->lock;
  357. if (size != sizeof(struct tipc_subscr))
  358. subscr_terminate(subscriber);
  359. else
  360. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  361. spin_unlock_bh(subscriber_lock);
  362. }
  363. /**
  364. * subscr_named_msg_event - handle request to establish a new subscriber
  365. */
  366. static void subscr_named_msg_event(void *usr_handle,
  367. u32 port_ref,
  368. struct sk_buff **buf,
  369. const unchar *data,
  370. u32 size,
  371. u32 importance,
  372. struct tipc_portid const *orig,
  373. struct tipc_name_seq const *dest)
  374. {
  375. struct subscriber *subscriber;
  376. struct iovec msg_sect = {NULL, 0};
  377. spinlock_t *subscriber_lock;
  378. dbg("subscr_named_msg_event: orig = %x own = %x,\n",
  379. orig->node, tipc_own_addr);
  380. if (size && (size != sizeof(struct tipc_subscr))) {
  381. warn("Subscriber rejected, invalid subscription size\n");
  382. return;
  383. }
  384. /* Create subscriber object */
  385. subscriber = kzalloc(sizeof(struct subscriber), GFP_ATOMIC);
  386. if (subscriber == NULL) {
  387. warn("Subscriber rejected, no memory\n");
  388. return;
  389. }
  390. INIT_LIST_HEAD(&subscriber->subscription_list);
  391. INIT_LIST_HEAD(&subscriber->subscriber_list);
  392. subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
  393. if (subscriber->ref == 0) {
  394. warn("Subscriber rejected, reference table exhausted\n");
  395. kfree(subscriber);
  396. return;
  397. }
  398. spin_unlock_bh(subscriber->lock);
  399. /* Establish a connection to subscriber */
  400. tipc_createport(topsrv.user_ref,
  401. (void *)(unsigned long)subscriber->ref,
  402. importance,
  403. NULL,
  404. NULL,
  405. subscr_conn_shutdown_event,
  406. NULL,
  407. NULL,
  408. subscr_conn_msg_event,
  409. NULL,
  410. &subscriber->port_ref);
  411. if (subscriber->port_ref == 0) {
  412. warn("Subscriber rejected, unable to create port\n");
  413. tipc_ref_discard(subscriber->ref);
  414. kfree(subscriber);
  415. return;
  416. }
  417. tipc_connect2port(subscriber->port_ref, orig);
  418. /* Add subscriber to topology server's subscriber list */
  419. tipc_ref_lock(subscriber->ref);
  420. spin_lock_bh(&topsrv.lock);
  421. list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
  422. spin_unlock_bh(&topsrv.lock);
  423. /*
  424. * Subscribe now if message contains a subscription,
  425. * otherwise send an empty response to complete connection handshaking
  426. */
  427. subscriber_lock = subscriber->lock;
  428. if (size)
  429. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  430. else
  431. tipc_send(subscriber->port_ref, 1, &msg_sect);
  432. spin_unlock_bh(subscriber_lock);
  433. }
  434. int tipc_subscr_start(void)
  435. {
  436. struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
  437. int res = -1;
  438. memset(&topsrv, 0, sizeof (topsrv));
  439. spin_lock_init(&topsrv.lock);
  440. INIT_LIST_HEAD(&topsrv.subscriber_list);
  441. spin_lock_bh(&topsrv.lock);
  442. res = tipc_attach(&topsrv.user_ref, NULL, NULL);
  443. if (res) {
  444. spin_unlock_bh(&topsrv.lock);
  445. return res;
  446. }
  447. res = tipc_createport(topsrv.user_ref,
  448. NULL,
  449. TIPC_CRITICAL_IMPORTANCE,
  450. NULL,
  451. NULL,
  452. NULL,
  453. NULL,
  454. subscr_named_msg_event,
  455. NULL,
  456. NULL,
  457. &topsrv.setup_port);
  458. if (res)
  459. goto failed;
  460. res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
  461. if (res)
  462. goto failed;
  463. spin_unlock_bh(&topsrv.lock);
  464. return 0;
  465. failed:
  466. err("Failed to create subscription service\n");
  467. tipc_detach(topsrv.user_ref);
  468. topsrv.user_ref = 0;
  469. spin_unlock_bh(&topsrv.lock);
  470. return res;
  471. }
  472. void tipc_subscr_stop(void)
  473. {
  474. struct subscriber *subscriber;
  475. struct subscriber *subscriber_temp;
  476. spinlock_t *subscriber_lock;
  477. if (topsrv.user_ref) {
  478. tipc_deleteport(topsrv.setup_port);
  479. list_for_each_entry_safe(subscriber, subscriber_temp,
  480. &topsrv.subscriber_list,
  481. subscriber_list) {
  482. tipc_ref_lock(subscriber->ref);
  483. subscriber_lock = subscriber->lock;
  484. subscr_terminate(subscriber);
  485. spin_unlock_bh(subscriber_lock);
  486. }
  487. tipc_detach(topsrv.user_ref);
  488. topsrv.user_ref = 0;
  489. }
  490. }
  491. int tipc_ispublished(struct tipc_name const *name)
  492. {
  493. u32 domain = 0;
  494. return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
  495. }