subscr.c 16 KB

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