subscr.c 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526
  1. /*
  2. * net/tipc/subscr.c: TIPC subscription service
  3. *
  4. * Copyright (c) 2003-2005, Ericsson Research Canada
  5. * Copyright (c) 2005, Wind River Systems
  6. * Copyright (c) 2005-2006, Ericsson AB
  7. * All rights reserved.
  8. *
  9. * Redistribution and use in source and binary forms, with or without
  10. * modification, are permitted provided that the following conditions are met:
  11. *
  12. * 1. Redistributions of source code must retain the above copyright
  13. * notice, this list of conditions and the following disclaimer.
  14. * 2. Redistributions in binary form must reproduce the above copyright
  15. * notice, this list of conditions and the following disclaimer in the
  16. * documentation and/or other materials provided with the distribution.
  17. * 3. Neither the names of the copyright holders nor the names of its
  18. * contributors may be used to endorse or promote products derived from
  19. * this software without specific prior written permission.
  20. *
  21. * Alternatively, this software may be distributed under the terms of the
  22. * GNU General Public License ("GPL") version 2 as published by the Free
  23. * Software Foundation.
  24. *
  25. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  26. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  27. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  28. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  29. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  30. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  31. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  32. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  33. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  34. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  35. * POSSIBILITY OF SUCH DAMAGE.
  36. */
  37. #include "core.h"
  38. #include "dbg.h"
  39. #include "subscr.h"
  40. #include "name_table.h"
  41. #include "ref.h"
  42. /**
  43. * struct subscriber - TIPC network topology subscriber
  44. * @ref: object reference to subscriber object itself
  45. * @lock: pointer to spinlock controlling access to subscriber object
  46. * @subscriber_list: adjacent subscribers in top. server's list of subscribers
  47. * @subscription_list: list of subscription objects for this subscriber
  48. * @port_ref: object reference to port used to communicate with subscriber
  49. * @swap: indicates if subscriber uses opposite endianness in its messages
  50. */
  51. struct subscriber {
  52. u32 ref;
  53. spinlock_t *lock;
  54. struct list_head subscriber_list;
  55. struct list_head subscription_list;
  56. u32 port_ref;
  57. int swap;
  58. };
  59. /**
  60. * struct top_srv - TIPC network topology subscription service
  61. * @user_ref: TIPC userid of subscription service
  62. * @setup_port: reference to TIPC port that handles subscription requests
  63. * @subscription_count: number of active subscriptions (not subscribers!)
  64. * @subscriber_list: list of ports subscribing to service
  65. * @lock: spinlock govering access to subscriber list
  66. */
  67. struct top_srv {
  68. u32 user_ref;
  69. u32 setup_port;
  70. atomic_t subscription_count;
  71. struct list_head subscriber_list;
  72. spinlock_t lock;
  73. };
  74. static struct top_srv topsrv = { 0 };
  75. /**
  76. * htohl - convert value to endianness used by destination
  77. * @in: value to convert
  78. * @swap: non-zero if endianness must be reversed
  79. *
  80. * Returns converted value
  81. */
  82. static inline u32 htohl(u32 in, int swap)
  83. {
  84. char *c = (char *)∈
  85. return swap ? ((c[3] << 3) + (c[2] << 2) + (c[1] << 1) + c[0]) : in;
  86. }
  87. /**
  88. * subscr_send_event - send a message containing a tipc_event to the subscriber
  89. */
  90. static void subscr_send_event(struct subscription *sub,
  91. u32 found_lower,
  92. u32 found_upper,
  93. u32 event,
  94. u32 port_ref,
  95. u32 node)
  96. {
  97. struct iovec msg_sect;
  98. msg_sect.iov_base = (void *)&sub->evt;
  99. msg_sect.iov_len = sizeof(struct tipc_event);
  100. sub->evt.event = htohl(event, sub->owner->swap);
  101. sub->evt.found_lower = htohl(found_lower, sub->owner->swap);
  102. sub->evt.found_upper = htohl(found_upper, sub->owner->swap);
  103. sub->evt.port.ref = htohl(port_ref, sub->owner->swap);
  104. sub->evt.port.node = htohl(node, sub->owner->swap);
  105. tipc_send(sub->owner->port_ref, 1, &msg_sect);
  106. }
  107. /**
  108. * subscr_overlap - test for subscription overlap with the given values
  109. *
  110. * Returns 1 if there is overlap, otherwise 0.
  111. */
  112. int subscr_overlap(struct subscription *sub,
  113. u32 found_lower,
  114. u32 found_upper)
  115. {
  116. if (found_lower < sub->seq.lower)
  117. found_lower = sub->seq.lower;
  118. if (found_upper > sub->seq.upper)
  119. found_upper = sub->seq.upper;
  120. if (found_lower > found_upper)
  121. return 0;
  122. return 1;
  123. }
  124. /**
  125. * subscr_report_overlap - issue event if there is subscription overlap
  126. *
  127. * Protected by nameseq.lock in name_table.c
  128. */
  129. void subscr_report_overlap(struct subscription *sub,
  130. u32 found_lower,
  131. u32 found_upper,
  132. u32 event,
  133. u32 port_ref,
  134. u32 node,
  135. int must)
  136. {
  137. dbg("Rep overlap %u:%u,%u<->%u,%u\n", sub->seq.type, sub->seq.lower,
  138. sub->seq.upper, found_lower, found_upper);
  139. if (!subscr_overlap(sub, found_lower, found_upper))
  140. return;
  141. if (!must && (sub->filter != TIPC_SUB_PORTS))
  142. return;
  143. subscr_send_event(sub, found_lower, found_upper, event, port_ref, node);
  144. }
  145. /**
  146. * subscr_timeout - subscription timeout has occurred
  147. */
  148. static void subscr_timeout(struct subscription *sub)
  149. {
  150. struct subscriber *subscriber;
  151. u32 subscriber_ref;
  152. /* Validate subscriber reference (in case subscriber is terminating) */
  153. subscriber_ref = sub->owner->ref;
  154. subscriber = (struct subscriber *)ref_lock(subscriber_ref);
  155. if (subscriber == NULL)
  156. return;
  157. /* Unlink subscription from name table */
  158. nametbl_unsubscribe(sub);
  159. /* Notify subscriber of timeout, then unlink subscription */
  160. subscr_send_event(sub,
  161. sub->evt.s.seq.lower,
  162. sub->evt.s.seq.upper,
  163. TIPC_SUBSCR_TIMEOUT,
  164. 0,
  165. 0);
  166. list_del(&sub->subscription_list);
  167. /* Now destroy subscription */
  168. ref_unlock(subscriber_ref);
  169. k_term_timer(&sub->timer);
  170. kfree(sub);
  171. atomic_dec(&topsrv.subscription_count);
  172. }
  173. /**
  174. * subscr_terminate - terminate communication with a subscriber
  175. *
  176. * Called with subscriber locked. Routine must temporarily release this lock
  177. * to enable subscription timeout routine(s) to finish without deadlocking;
  178. * the lock is then reclaimed to allow caller to release it upon return.
  179. * (This should work even in the unlikely event some other thread creates
  180. * a new object reference in the interim that uses this lock; this routine will
  181. * simply wait for it to be released, then claim it.)
  182. */
  183. static void subscr_terminate(struct subscriber *subscriber)
  184. {
  185. struct subscription *sub;
  186. struct subscription *sub_temp;
  187. /* Invalidate subscriber reference */
  188. ref_discard(subscriber->ref);
  189. spin_unlock_bh(subscriber->lock);
  190. /* Destroy any existing subscriptions for subscriber */
  191. list_for_each_entry_safe(sub, sub_temp, &subscriber->subscription_list,
  192. subscription_list) {
  193. if (sub->timeout != TIPC_WAIT_FOREVER) {
  194. k_cancel_timer(&sub->timer);
  195. k_term_timer(&sub->timer);
  196. }
  197. nametbl_unsubscribe(sub);
  198. list_del(&sub->subscription_list);
  199. dbg("Term: Removed sub %u,%u,%u from subscriber %x list\n",
  200. sub->seq.type, sub->seq.lower, sub->seq.upper, subscriber);
  201. kfree(sub);
  202. atomic_dec(&topsrv.subscription_count);
  203. }
  204. /* Sever connection to subscriber */
  205. tipc_shutdown(subscriber->port_ref);
  206. tipc_deleteport(subscriber->port_ref);
  207. /* Remove subscriber from topology server's subscriber list */
  208. spin_lock_bh(&topsrv.lock);
  209. list_del(&subscriber->subscriber_list);
  210. spin_unlock_bh(&topsrv.lock);
  211. /* Now destroy subscriber */
  212. spin_lock_bh(subscriber->lock);
  213. kfree(subscriber);
  214. }
  215. /**
  216. * subscr_subscribe - create subscription for subscriber
  217. *
  218. * Called with subscriber locked
  219. */
  220. static void subscr_subscribe(struct tipc_subscr *s,
  221. struct subscriber *subscriber)
  222. {
  223. struct subscription *sub;
  224. /* Refuse subscription if global limit exceeded */
  225. if (atomic_read(&topsrv.subscription_count) >= tipc_max_subscriptions) {
  226. warn("Failed: max %u subscriptions\n", tipc_max_subscriptions);
  227. subscr_terminate(subscriber);
  228. return;
  229. }
  230. /* Allocate subscription object */
  231. sub = kmalloc(sizeof(*sub), GFP_ATOMIC);
  232. if (sub == NULL) {
  233. warn("Memory squeeze; ignoring subscription\n");
  234. subscr_terminate(subscriber);
  235. return;
  236. }
  237. /* Determine/update subscriber's endianness */
  238. if ((s->filter == TIPC_SUB_PORTS) || (s->filter == TIPC_SUB_SERVICE))
  239. subscriber->swap = 0;
  240. else
  241. subscriber->swap = 1;
  242. /* Initialize subscription object */
  243. memset(sub, 0, sizeof(*sub));
  244. sub->seq.type = htohl(s->seq.type, subscriber->swap);
  245. sub->seq.lower = htohl(s->seq.lower, subscriber->swap);
  246. sub->seq.upper = htohl(s->seq.upper, subscriber->swap);
  247. sub->timeout = htohl(s->timeout, subscriber->swap);
  248. sub->filter = htohl(s->filter, subscriber->swap);
  249. if ((((sub->filter != TIPC_SUB_PORTS)
  250. && (sub->filter != TIPC_SUB_SERVICE)))
  251. || (sub->seq.lower > sub->seq.upper)) {
  252. warn("Rejecting illegal subscription %u,%u,%u\n",
  253. sub->seq.type, sub->seq.lower, sub->seq.upper);
  254. kfree(sub);
  255. subscr_terminate(subscriber);
  256. return;
  257. }
  258. memcpy(&sub->evt.s, s, sizeof(struct tipc_subscr));
  259. INIT_LIST_HEAD(&sub->subscription_list);
  260. INIT_LIST_HEAD(&sub->nameseq_list);
  261. list_add(&sub->subscription_list, &subscriber->subscription_list);
  262. atomic_inc(&topsrv.subscription_count);
  263. if (sub->timeout != TIPC_WAIT_FOREVER) {
  264. k_init_timer(&sub->timer,
  265. (Handler)subscr_timeout, (unsigned long)sub);
  266. k_start_timer(&sub->timer, sub->timeout);
  267. }
  268. sub->owner = subscriber;
  269. nametbl_subscribe(sub);
  270. }
  271. /**
  272. * subscr_conn_shutdown_event - handle termination request from subscriber
  273. */
  274. static void subscr_conn_shutdown_event(void *usr_handle,
  275. u32 portref,
  276. struct sk_buff **buf,
  277. unsigned char const *data,
  278. unsigned int size,
  279. int reason)
  280. {
  281. struct subscriber *subscriber = ref_lock((u32)usr_handle);
  282. spinlock_t *subscriber_lock;
  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 = ref_lock((u32)usr_handle);
  299. spinlock_t *subscriber_lock;
  300. if (subscriber == NULL)
  301. return;
  302. subscriber_lock = subscriber->lock;
  303. if (size != sizeof(struct tipc_subscr))
  304. subscr_terminate(subscriber);
  305. else
  306. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  307. spin_unlock_bh(subscriber_lock);
  308. }
  309. /**
  310. * subscr_named_msg_event - handle request to establish a new subscriber
  311. */
  312. static void subscr_named_msg_event(void *usr_handle,
  313. u32 port_ref,
  314. struct sk_buff **buf,
  315. const unchar *data,
  316. u32 size,
  317. u32 importance,
  318. struct tipc_portid const *orig,
  319. struct tipc_name_seq const *dest)
  320. {
  321. struct subscriber *subscriber;
  322. struct iovec msg_sect = {0, 0};
  323. spinlock_t *subscriber_lock;
  324. dbg("subscr_named_msg_event: orig = %x own = %x,\n",
  325. orig->node, tipc_own_addr);
  326. if (size && (size != sizeof(struct tipc_subscr))) {
  327. warn("Received tipc_subscr of invalid size\n");
  328. return;
  329. }
  330. /* Create subscriber object */
  331. subscriber = kmalloc(sizeof(struct subscriber), GFP_ATOMIC);
  332. if (subscriber == NULL) {
  333. warn("Memory squeeze; ignoring subscriber setup\n");
  334. return;
  335. }
  336. memset(subscriber, 0, sizeof(struct subscriber));
  337. INIT_LIST_HEAD(&subscriber->subscription_list);
  338. INIT_LIST_HEAD(&subscriber->subscriber_list);
  339. subscriber->ref = ref_acquire(subscriber, &subscriber->lock);
  340. if (subscriber->ref == 0) {
  341. warn("Failed to acquire subscriber reference\n");
  342. kfree(subscriber);
  343. return;
  344. }
  345. /* Establish a connection to subscriber */
  346. tipc_createport(topsrv.user_ref,
  347. (void *)subscriber->ref,
  348. importance,
  349. 0,
  350. 0,
  351. subscr_conn_shutdown_event,
  352. 0,
  353. 0,
  354. subscr_conn_msg_event,
  355. 0,
  356. &subscriber->port_ref);
  357. if (subscriber->port_ref == 0) {
  358. warn("Memory squeeze; failed to create subscription port\n");
  359. ref_discard(subscriber->ref);
  360. kfree(subscriber);
  361. return;
  362. }
  363. tipc_connect2port(subscriber->port_ref, orig);
  364. /* Add subscriber to topology server's subscriber list */
  365. ref_lock(subscriber->ref);
  366. spin_lock_bh(&topsrv.lock);
  367. list_add(&subscriber->subscriber_list, &topsrv.subscriber_list);
  368. spin_unlock_bh(&topsrv.lock);
  369. /*
  370. * Subscribe now if message contains a subscription,
  371. * otherwise send an empty response to complete connection handshaking
  372. */
  373. subscriber_lock = subscriber->lock;
  374. if (size)
  375. subscr_subscribe((struct tipc_subscr *)data, subscriber);
  376. else
  377. tipc_send(subscriber->port_ref, 1, &msg_sect);
  378. spin_unlock_bh(subscriber_lock);
  379. }
  380. int subscr_start(void)
  381. {
  382. struct tipc_name_seq seq = {TIPC_TOP_SRV, TIPC_TOP_SRV, TIPC_TOP_SRV};
  383. int res = -1;
  384. memset(&topsrv, 0, sizeof (topsrv));
  385. topsrv.lock = SPIN_LOCK_UNLOCKED;
  386. INIT_LIST_HEAD(&topsrv.subscriber_list);
  387. spin_lock_bh(&topsrv.lock);
  388. res = tipc_attach(&topsrv.user_ref, 0, 0);
  389. if (res) {
  390. spin_unlock_bh(&topsrv.lock);
  391. return res;
  392. }
  393. res = tipc_createport(topsrv.user_ref,
  394. 0,
  395. TIPC_CRITICAL_IMPORTANCE,
  396. 0,
  397. 0,
  398. 0,
  399. 0,
  400. subscr_named_msg_event,
  401. 0,
  402. 0,
  403. &topsrv.setup_port);
  404. if (res)
  405. goto failed;
  406. res = nametbl_publish_rsv(topsrv.setup_port, TIPC_NODE_SCOPE, &seq);
  407. if (res)
  408. goto failed;
  409. spin_unlock_bh(&topsrv.lock);
  410. return 0;
  411. failed:
  412. err("Unable to create subscription service\n");
  413. tipc_detach(topsrv.user_ref);
  414. topsrv.user_ref = 0;
  415. spin_unlock_bh(&topsrv.lock);
  416. return res;
  417. }
  418. void subscr_stop(void)
  419. {
  420. struct subscriber *subscriber;
  421. struct subscriber *subscriber_temp;
  422. spinlock_t *subscriber_lock;
  423. if (topsrv.user_ref) {
  424. tipc_deleteport(topsrv.setup_port);
  425. list_for_each_entry_safe(subscriber, subscriber_temp,
  426. &topsrv.subscriber_list,
  427. subscriber_list) {
  428. ref_lock(subscriber->ref);
  429. subscriber_lock = subscriber->lock;
  430. subscr_terminate(subscriber);
  431. spin_unlock_bh(subscriber_lock);
  432. }
  433. tipc_detach(topsrv.user_ref);
  434. topsrv.user_ref = 0;
  435. }
  436. }
  437. int tipc_ispublished(struct tipc_name const *name)
  438. {
  439. u32 domain = 0;
  440. return(nametbl_translate(name->type, name->instance,&domain) != 0);
  441. }