subscr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527
  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. /* Unlink subscription from name table */
  157. tipc_nametbl_unsubscribe(sub);
  158. /* Notify subscriber of timeout, then unlink subscription */
  159. subscr_send_event(sub,
  160. sub->evt.s.seq.lower,
  161. sub->evt.s.seq.upper,
  162. TIPC_SUBSCR_TIMEOUT,
  163. 0,
  164. 0);
  165. list_del(&sub->subscription_list);
  166. /* Now destroy subscription */
  167. tipc_ref_unlock(subscriber_ref);
  168. k_term_timer(&sub->timer);
  169. kfree(sub);
  170. atomic_dec(&topsrv.subscription_count);
  171. }
  172. /**
  173. * subscr_terminate - terminate communication with a subscriber
  174. *
  175. * Called with subscriber locked. Routine must temporarily release this lock
  176. * to enable subscription timeout routine(s) to finish without deadlocking;
  177. * the lock is then reclaimed to allow caller to release it upon return.
  178. * (This should work even in the unlikely event some other thread creates
  179. * a new object reference in the interim that uses this lock; this routine will
  180. * simply wait for it to be released, then claim it.)
  181. */
  182. static void subscr_terminate(struct subscriber *subscriber)
  183. {
  184. struct subscription *sub;
  185. struct subscription *sub_temp;
  186. /* Invalidate subscriber reference */
  187. tipc_ref_discard(subscriber->ref);
  188. spin_unlock_bh(subscriber->lock);
  189. /* Destroy any existing subscriptions for subscriber */
  190. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  191. subscription_list) {
  192. if (sub->timeout != TIPC_WAIT_FOREVER) {
  193. k_cancel_timer(&sub->timer);
  194. k_term_timer(&sub->timer);
  195. }
  196. tipc_nametbl_unsubscribe(sub);
  197. list_del(&sub->subscription_list);
  198. dbg("Term: Removed sub %u,%u,%u from subscriber %x list\n",
  199. sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
  200. kfree(sub);
  201. atomic_dec(&topsrv.subscription_count);
  202. }
  203. /* Sever connection to subscriber */
  204. tipc_shutdown(subscriber->port_ref);
  205. tipc_deleteport(subscriber->port_ref);
  206. /* Remove subscriber from topology server's subscriber list */
  207. spin_lock_bh(&topsrv.lock);
  208. list_del(&subscriber->subscriber_list);
  209. spin_unlock_bh(&topsrv.lock);
  210. /* Now destroy subscriber */
  211. spin_lock_bh(subscriber->lock);
  212. kfree(subscriber);
  213. }
  214. /**
  215. * subscr_subscribe - create subscription for subscriber
  216. *
  217. * Called with subscriber locked
  218. */
  219. static void subscr_subscribe(struct tipc_subscr *s,
  220. struct subscriber *subscriber)
  221. {
  222. struct subscription *sub;
  223. /* Refuse subscription if global limit exceeded */
  224. if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
  225. warn("Failed: max %u subscriptions\n", tipc_max_subscriptions);
  226. subscr_terminate(subscriber);
  227. return;
  228. }
  229. /* Allocate subscription object */
  230. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  231. if (sub == NULL) {
  232. warn("Memory squeeze; ignoring subscription\n");
  233. subscr_terminate(subscriber);
  234. return;
  235. }
  236. /* Determine/update subscriber's endianness */
  237. if ((s->filter == TIPC_SUB_PORTS) || (s->filter == TIPC_SUB_SERVICE))
  238. subscriber->swap = 0;
  239. else
  240. subscriber->swap = 1;
  241. /* Initialize subscription object */
  242. memset(sub, 0, sizeof(*sub));
  243. sub->seq.type = htohl(s->seq.type, subscriber->swap);
  244. sub->seq.lower = htohl(s->seq.lower, subscriber->swap);
  245. sub->seq.upper = htohl(s->seq.upper, subscriber->swap);
  246. sub->timeout = htohl(s->timeout, subscriber->swap);
  247. sub->filter = htohl(s->filter, subscriber->swap);
  248. if ((((sub->filter != TIPC_SUB_PORTS)
  249. && (sub->filter != TIPC_SUB_SERVICE)))
  250. || (sub->seq.lower > sub->seq.upper)) {
  251. warn("Rejecting illegal subscription %u,%u,%u\n",
  252. sub->seq.type, sub->seq.lower, sub->seq.upper);
  253. kfree(sub);
  254. subscr_terminate(subscriber);
  255. return;
  256. }
  257. memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
  258. INIT_LIST_HEAD(&sub->subscription_list);
  259. INIT_LIST_HEAD(&sub->nameseq_list);
  260. list_add(&sub->subscription_list, &subscriber->subscription_list);
  261. atomic_inc(&topsrv.subscription_count);
  262. if (sub->timeout != TIPC_WAIT_FOREVER) {
  263. k_init_timer(&sub->timer,
  264. (Handler)subscr_timeout, (unsigned long)sub);
  265. k_start_timer(&sub->timer, sub->timeout);
  266. }
  267. sub->owner = subscriber;
  268. tipc_nametbl_subscribe(sub);
  269. }
  270. /**
  271. * subscr_conn_shutdown_event - handle termination request from subscriber
  272. */
  273. static void subscr_conn_shutdown_event(void *usr_handle,
  274. u32 portref,
  275. struct sk_buff **buf,
  276. unsigned char const *data,
  277. unsigned int size,
  278. int reason)
  279. {
  280. struct subscriber *subscriber;
  281. spinlock_t *subscriber_lock;
  282. subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
  283. if (subscriber == NULL)
  284. return;
  285. subscriber_lock = subscriber->lock;
  286. subscr_terminate(subscriber);
  287. spin_unlock_bh(subscriber_lock);
  288. }
  289. /**
  290. * subscr_conn_msg_event - handle new subscription request from subscriber
  291. */
  292. static void subscr_conn_msg_event(void *usr_handle,
  293. u32 port_ref,
  294. struct sk_buff **buf,
  295. const unchar *data,
  296. u32 size)
  297. {
  298. struct subscriber *subscriber;
  299. spinlock_t *subscriber_lock;
  300. subscriber = tipc_ref_lock((u32)(unsigned long)usr_handle);
  301. if (subscriber == NULL)
  302. return;
  303. subscriber_lock = subscriber->lock;
  304. if (size != sizeof(struct tipc_subscr))
  305. subscr_terminate(subscriber);
  306. else
  307. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  308. spin_unlock_bh(subscriber_lock);
  309. }
  310. /**
  311. * subscr_named_msg_event - handle request to establish a new subscriber
  312. */
  313. static void subscr_named_msg_event(void *usr_handle,
  314. u32 port_ref,
  315. struct sk_buff **buf,
  316. const unchar *data,
  317. u32 size,
  318. u32 importance,
  319. struct tipc_portid const *orig,
  320. struct tipc_name_seq const *dest)
  321. {
  322. struct subscriber *subscriber;
  323. struct iovec msg_sect = {NULL, 0};
  324. spinlock_t *subscriber_lock;
  325. dbg("subscr_named_msg_event: orig = %x own = %x,\n",
  326. orig->node, tipc_own_addr);
  327. if (size && (size != sizeof(struct tipc_subscr))) {
  328. warn("Received tipc_subscr of invalid size\n");
  329. return;
  330. }
  331. /* Create subscriber object */
  332. subscriber = kmalloc(sizeof(struct subscriber), GFP_ATOMIC);
  333. if (subscriber == NULL) {
  334. warn("Memory squeeze; ignoring subscriber setup\n");
  335. return;
  336. }
  337. memset(subscriber, 0, sizeof(struct subscriber));
  338. INIT_LIST_HEAD(&subscriber->subscription_list);
  339. INIT_LIST_HEAD(&subscriber->subscriber_list);
  340. subscriber->ref = tipc_ref_acquire(subscriber, &subscriber->lock);
  341. if (subscriber->ref == 0) {
  342. warn("Failed to acquire subscriber reference\n");
  343. kfree(subscriber);
  344. return;
  345. }
  346. /* Establish a connection to subscriber */
  347. tipc_createport(topsrv.user_ref,
  348. (void *)(unsigned long)subscriber->ref,
  349. importance,
  350. NULL,
  351. NULL,
  352. subscr_conn_shutdown_event,
  353. NULL,
  354. NULL,
  355. subscr_conn_msg_event,
  356. NULL,
  357. &subscriber->port_ref);
  358. if (subscriber->port_ref == 0) {
  359. warn("Memory squeeze; failed to create subscription port\n");
  360. tipc_ref_discard(subscriber->ref);
  361. kfree(subscriber);
  362. return;
  363. }
  364. tipc_connect2port(subscriber->port_ref, orig);
  365. /* Add subscriber to topology server's subscriber list */
  366. tipc_ref_lock(subscriber->ref);
  367. spin_lock_bh(&topsrv.lock);
  368. list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
  369. spin_unlock_bh(&topsrv.lock);
  370. /*
  371. * Subscribe now if message contains a subscription,
  372. * otherwise send an empty response to complete connection handshaking
  373. */
  374. subscriber_lock = subscriber->lock;
  375. if (size)
  376. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  377. else
  378. tipc_send(subscriber->port_ref, 1, &msg_sect);
  379. spin_unlock_bh(subscriber_lock);
  380. }
  381. int tipc_subscr_start(void)
  382. {
  383. struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
  384. int res = -1;
  385. memset(&topsrv, 0, sizeof (topsrv));
  386. topsrv.lock = SPIN_LOCK_UNLOCKED;
  387. INIT_LIST_HEAD(&topsrv.subscriber_list);
  388. spin_lock_bh(&topsrv.lock);
  389. res = tipc_attach(&topsrv.user_ref, NULL, NULL);
  390. if (res) {
  391. spin_unlock_bh(&topsrv.lock);
  392. return res;
  393. }
  394. res = tipc_createport(topsrv.user_ref,
  395. NULL,
  396. TIPC_CRITICAL_IMPORTANCE,
  397. NULL,
  398. NULL,
  399. NULL,
  400. NULL,
  401. subscr_named_msg_event,
  402. NULL,
  403. NULL,
  404. &topsrv.setup_port);
  405. if (res)
  406. goto failed;
  407. res = tipc_nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
  408. if (res)
  409. goto failed;
  410. spin_unlock_bh(&topsrv.lock);
  411. return 0;
  412. failed:
  413. err("Failed to create subscription service\n");
  414. tipc_detach(topsrv.user_ref);
  415. topsrv.user_ref = 0;
  416. spin_unlock_bh(&topsrv.lock);
  417. return res;
  418. }
  419. void tipc_subscr_stop(void)
  420. {
  421. struct subscriber *subscriber;
  422. struct subscriber *subscriber_temp;
  423. spinlock_t *subscriber_lock;
  424. if (topsrv.user_ref) {
  425. tipc_deleteport(topsrv.setup_port);
  426. list_for_each_entry_safe(subscriber, subscriber_temp,
  427. &topsrv.subscriber_list,
  428. subscriber_list) {
  429. tipc_ref_lock(subscriber->ref);
  430. subscriber_lock = subscriber->lock;
  431. subscr_terminate(subscriber);
  432. spin_unlock_bh(subscriber_lock);
  433. }
  434. tipc_detach(topsrv.user_ref);
  435. topsrv.user_ref = 0;
  436. }
  437. }
  438. int tipc_ispublished(struct tipc_name const *name)
  439. {
  440. u32 domain = 0;
  441. return(tipc_nametbl_translate(name->type, name->instance,&domain) != 0);
  442. }