subscr.c 16 KB

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