lowcomms.c 47 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980
  1. // SPDX-License-Identifier: GPL-2.0-only
  2. /******************************************************************************
  3. *******************************************************************************
  4. **
  5. ** Copyright (C) Sistina Software, Inc. 1997-2003 All rights reserved.
  6. ** Copyright (C) 2004-2009 Red Hat, Inc. All rights reserved.
  7. **
  8. **
  9. *******************************************************************************
  10. ******************************************************************************/
  11. /*
  12. * lowcomms.c
  13. *
  14. * This is the "low-level" comms layer.
  15. *
  16. * It is responsible for sending/receiving messages
  17. * from other nodes in the cluster.
  18. *
  19. * Cluster nodes are referred to by their nodeids. nodeids are
  20. * simply 32 bit numbers to the locking module - if they need to
  21. * be expanded for the cluster infrastructure then that is its
  22. * responsibility. It is this layer's
  23. * responsibility to resolve these into IP address or
  24. * whatever it needs for inter-node communication.
  25. *
  26. * The comms level is two kernel threads that deal mainly with
  27. * the receiving of messages from other nodes and passing them
  28. * up to the mid-level comms layer (which understands the
  29. * message format) for execution by the locking core, and
  30. * a send thread which does all the setting up of connections
  31. * to remote nodes and the sending of data. Threads are not allowed
  32. * to send their own data because it may cause them to wait in times
  33. * of high load. Also, this way, the sending thread can collect together
  34. * messages bound for one node and send them in one block.
  35. *
  36. * lowcomms will choose to use either TCP or SCTP as its transport layer
  37. * depending on the configuration variable 'protocol'. This should be set
  38. * to 0 (default) for TCP or 1 for SCTP. It should be configured using a
  39. * cluster-wide mechanism as it must be the same on all nodes of the cluster
  40. * for the DLM to function.
  41. *
  42. */
  43. #include <asm/ioctls.h>
  44. #include <net/sock.h>
  45. #include <net/tcp.h>
  46. #include <linux/pagemap.h>
  47. #include <linux/file.h>
  48. #include <linux/mutex.h>
  49. #include <linux/sctp.h>
  50. #include <linux/slab.h>
  51. #include <net/sctp/sctp.h>
  52. #include <net/ipv6.h>
  53. #include <trace/events/dlm.h>
  54. #include <trace/events/sock.h>
  55. #include "dlm_internal.h"
  56. #include "lowcomms.h"
  57. #include "midcomms.h"
  58. #include "memory.h"
  59. #include "config.h"
  60. #define DLM_SHUTDOWN_WAIT_TIMEOUT msecs_to_jiffies(5000)
  61. #define DLM_MAX_PROCESS_BUFFERS 24
  62. #define NEEDED_RMEM (4*1024*1024)
  63. struct connection {
  64. struct socket *sock; /* NULL if not connected */
  65. uint32_t nodeid; /* So we know who we are in the list */
  66. /* this semaphore is used to allow parallel recv/send in read
  67. * lock mode. When we release a sock we need to held the write lock.
  68. *
  69. * However this is locking code and not nice. When we remove the
  70. * othercon handling we can look into other mechanism to synchronize
  71. * io handling to call sock_release() at the right time.
  72. */
  73. struct rw_semaphore sock_lock;
  74. unsigned long flags;
  75. #define CF_APP_LIMITED 0
  76. #define CF_RECV_PENDING 1
  77. #define CF_SEND_PENDING 2
  78. #define CF_RECV_INTR 3
  79. #define CF_IO_STOP 4
  80. #define CF_IS_OTHERCON 5
  81. struct list_head writequeue; /* List of outgoing writequeue_entries */
  82. spinlock_t writequeue_lock;
  83. int retries;
  84. struct hlist_node list;
  85. /* due some connect()/accept() races we currently have this cross over
  86. * connection attempt second connection for one node.
  87. *
  88. * There is a solution to avoid the race by introducing a connect
  89. * rule as e.g. our_nodeid > nodeid_to_connect who is allowed to
  90. * connect. Otherside can connect but will only be considered that
  91. * the other side wants to have a reconnect.
  92. *
  93. * However changing to this behaviour will break backwards compatible.
  94. * In a DLM protocol major version upgrade we should remove this!
  95. */
  96. struct connection *othercon;
  97. struct work_struct rwork; /* receive worker */
  98. struct work_struct swork; /* send worker */
  99. wait_queue_head_t shutdown_wait;
  100. unsigned char rx_leftover_buf[DLM_MAX_SOCKET_BUFSIZE];
  101. int rx_leftover;
  102. int mark;
  103. int addr_count;
  104. int curr_addr_index;
  105. struct sockaddr_storage addr[DLM_MAX_ADDR_COUNT];
  106. spinlock_t addrs_lock;
  107. struct rcu_head rcu;
  108. };
  109. #define sock2con(x) ((struct connection *)(x)->sk_user_data)
  110. struct listen_connection {
  111. struct socket *sock;
  112. struct work_struct rwork;
  113. };
  114. #define DLM_WQ_REMAIN_BYTES(e) (PAGE_SIZE - e->end)
  115. #define DLM_WQ_LENGTH_BYTES(e) (e->end - e->offset)
  116. /* An entry waiting to be sent */
  117. struct writequeue_entry {
  118. struct list_head list;
  119. struct page *page;
  120. int offset;
  121. int len;
  122. int end;
  123. int users;
  124. bool dirty;
  125. struct connection *con;
  126. struct list_head msgs;
  127. struct kref ref;
  128. };
  129. struct dlm_msg {
  130. struct writequeue_entry *entry;
  131. struct dlm_msg *orig_msg;
  132. bool retransmit;
  133. void *ppc;
  134. int len;
  135. int idx; /* new()/commit() idx exchange */
  136. struct list_head list;
  137. struct kref ref;
  138. };
  139. struct processqueue_entry {
  140. unsigned char *buf;
  141. int nodeid;
  142. int buflen;
  143. struct list_head list;
  144. };
  145. struct dlm_proto_ops {
  146. bool try_new_addr;
  147. const char *name;
  148. int proto;
  149. void (*sockopts)(struct socket *sock);
  150. int (*bind)(struct socket *sock);
  151. int (*listen_validate)(void);
  152. void (*listen_sockopts)(struct socket *sock);
  153. int (*listen_bind)(struct socket *sock);
  154. };
  155. static struct listen_sock_callbacks {
  156. void (*sk_error_report)(struct sock *);
  157. void (*sk_data_ready)(struct sock *);
  158. void (*sk_state_change)(struct sock *);
  159. void (*sk_write_space)(struct sock *);
  160. } listen_sock;
  161. static struct listen_connection listen_con;
  162. static struct sockaddr_storage dlm_local_addr[DLM_MAX_ADDR_COUNT];
  163. static int dlm_local_count;
  164. /* Work queues */
  165. static struct workqueue_struct *io_workqueue;
  166. static struct workqueue_struct *process_workqueue;
  167. static struct hlist_head connection_hash[CONN_HASH_SIZE];
  168. static DEFINE_SPINLOCK(connections_lock);
  169. DEFINE_STATIC_SRCU(connections_srcu);
  170. static const struct dlm_proto_ops *dlm_proto_ops;
  171. #define DLM_IO_SUCCESS 0
  172. #define DLM_IO_END 1
  173. #define DLM_IO_EOF 2
  174. #define DLM_IO_RESCHED 3
  175. #define DLM_IO_FLUSH 4
  176. static void process_recv_sockets(struct work_struct *work);
  177. static void process_send_sockets(struct work_struct *work);
  178. static void process_dlm_messages(struct work_struct *work);
  179. static DECLARE_WORK(process_work, process_dlm_messages);
  180. static DEFINE_SPINLOCK(processqueue_lock);
  181. static bool process_dlm_messages_pending;
  182. static DECLARE_WAIT_QUEUE_HEAD(processqueue_wq);
  183. static atomic_t processqueue_count;
  184. static LIST_HEAD(processqueue);
  185. bool dlm_lowcomms_is_running(void)
  186. {
  187. return !!listen_con.sock;
  188. }
  189. static void lowcomms_queue_swork(struct connection *con)
  190. {
  191. assert_spin_locked(&con->writequeue_lock);
  192. if (!test_bit(CF_IO_STOP, &con->flags) &&
  193. !test_bit(CF_APP_LIMITED, &con->flags) &&
  194. !test_and_set_bit(CF_SEND_PENDING, &con->flags))
  195. queue_work(io_workqueue, &con->swork);
  196. }
  197. static void lowcomms_queue_rwork(struct connection *con)
  198. {
  199. #ifdef CONFIG_LOCKDEP
  200. WARN_ON_ONCE(!lockdep_sock_is_held(con->sock->sk));
  201. #endif
  202. if (!test_bit(CF_IO_STOP, &con->flags) &&
  203. !test_and_set_bit(CF_RECV_PENDING, &con->flags))
  204. queue_work(io_workqueue, &con->rwork);
  205. }
  206. static void writequeue_entry_ctor(void *data)
  207. {
  208. struct writequeue_entry *entry = data;
  209. INIT_LIST_HEAD(&entry->msgs);
  210. }
  211. struct kmem_cache *dlm_lowcomms_writequeue_cache_create(void)
  212. {
  213. return kmem_cache_create("dlm_writequeue", sizeof(struct writequeue_entry),
  214. 0, 0, writequeue_entry_ctor);
  215. }
  216. struct kmem_cache *dlm_lowcomms_msg_cache_create(void)
  217. {
  218. return KMEM_CACHE(dlm_msg, 0);
  219. }
  220. /* need to held writequeue_lock */
  221. static struct writequeue_entry *con_next_wq(struct connection *con)
  222. {
  223. struct writequeue_entry *e;
  224. e = list_first_entry_or_null(&con->writequeue, struct writequeue_entry,
  225. list);
  226. /* if len is zero nothing is to send, if there are users filling
  227. * buffers we wait until the users are done so we can send more.
  228. */
  229. if (!e || e->users || e->len == 0)
  230. return NULL;
  231. return e;
  232. }
  233. static struct connection *__find_con(int nodeid, int r)
  234. {
  235. struct connection *con;
  236. hlist_for_each_entry_rcu(con, &connection_hash[r], list) {
  237. if (con->nodeid == nodeid)
  238. return con;
  239. }
  240. return NULL;
  241. }
  242. static void dlm_con_init(struct connection *con, int nodeid)
  243. {
  244. con->nodeid = nodeid;
  245. init_rwsem(&con->sock_lock);
  246. INIT_LIST_HEAD(&con->writequeue);
  247. spin_lock_init(&con->writequeue_lock);
  248. INIT_WORK(&con->swork, process_send_sockets);
  249. INIT_WORK(&con->rwork, process_recv_sockets);
  250. spin_lock_init(&con->addrs_lock);
  251. init_waitqueue_head(&con->shutdown_wait);
  252. }
  253. /*
  254. * If 'allocation' is zero then we don't attempt to create a new
  255. * connection structure for this node.
  256. */
  257. static struct connection *nodeid2con(int nodeid, gfp_t alloc)
  258. {
  259. struct connection *con, *tmp;
  260. int r;
  261. r = nodeid_hash(nodeid);
  262. con = __find_con(nodeid, r);
  263. if (con || !alloc)
  264. return con;
  265. con = kzalloc(sizeof(*con), alloc);
  266. if (!con)
  267. return NULL;
  268. dlm_con_init(con, nodeid);
  269. spin_lock(&connections_lock);
  270. /* Because multiple workqueues/threads calls this function it can
  271. * race on multiple cpu's. Instead of locking hot path __find_con()
  272. * we just check in rare cases of recently added nodes again
  273. * under protection of connections_lock. If this is the case we
  274. * abort our connection creation and return the existing connection.
  275. */
  276. tmp = __find_con(nodeid, r);
  277. if (tmp) {
  278. spin_unlock(&connections_lock);
  279. kfree(con);
  280. return tmp;
  281. }
  282. hlist_add_head_rcu(&con->list, &connection_hash[r]);
  283. spin_unlock(&connections_lock);
  284. return con;
  285. }
  286. static int addr_compare(const struct sockaddr_storage *x,
  287. const struct sockaddr_storage *y)
  288. {
  289. switch (x->ss_family) {
  290. case AF_INET: {
  291. struct sockaddr_in *sinx = (struct sockaddr_in *)x;
  292. struct sockaddr_in *siny = (struct sockaddr_in *)y;
  293. if (sinx->sin_addr.s_addr != siny->sin_addr.s_addr)
  294. return 0;
  295. if (sinx->sin_port != siny->sin_port)
  296. return 0;
  297. break;
  298. }
  299. case AF_INET6: {
  300. struct sockaddr_in6 *sinx = (struct sockaddr_in6 *)x;
  301. struct sockaddr_in6 *siny = (struct sockaddr_in6 *)y;
  302. if (!ipv6_addr_equal(&sinx->sin6_addr, &siny->sin6_addr))
  303. return 0;
  304. if (sinx->sin6_port != siny->sin6_port)
  305. return 0;
  306. break;
  307. }
  308. default:
  309. return 0;
  310. }
  311. return 1;
  312. }
  313. static int nodeid_to_addr(int nodeid, struct sockaddr_storage *sas_out,
  314. struct sockaddr *sa_out, bool try_new_addr,
  315. unsigned int *mark)
  316. {
  317. struct sockaddr_storage sas;
  318. struct connection *con;
  319. int idx;
  320. if (!dlm_local_count)
  321. return -1;
  322. idx = srcu_read_lock(&connections_srcu);
  323. con = nodeid2con(nodeid, 0);
  324. if (!con) {
  325. srcu_read_unlock(&connections_srcu, idx);
  326. return -ENOENT;
  327. }
  328. spin_lock(&con->addrs_lock);
  329. if (!con->addr_count) {
  330. spin_unlock(&con->addrs_lock);
  331. srcu_read_unlock(&connections_srcu, idx);
  332. return -ENOENT;
  333. }
  334. memcpy(&sas, &con->addr[con->curr_addr_index],
  335. sizeof(struct sockaddr_storage));
  336. if (try_new_addr) {
  337. con->curr_addr_index++;
  338. if (con->curr_addr_index == con->addr_count)
  339. con->curr_addr_index = 0;
  340. }
  341. *mark = con->mark;
  342. spin_unlock(&con->addrs_lock);
  343. if (sas_out)
  344. memcpy(sas_out, &sas, sizeof(struct sockaddr_storage));
  345. if (!sa_out) {
  346. srcu_read_unlock(&connections_srcu, idx);
  347. return 0;
  348. }
  349. if (dlm_local_addr[0].ss_family == AF_INET) {
  350. struct sockaddr_in *in4 = (struct sockaddr_in *) &sas;
  351. struct sockaddr_in *ret4 = (struct sockaddr_in *) sa_out;
  352. ret4->sin_addr.s_addr = in4->sin_addr.s_addr;
  353. } else {
  354. struct sockaddr_in6 *in6 = (struct sockaddr_in6 *) &sas;
  355. struct sockaddr_in6 *ret6 = (struct sockaddr_in6 *) sa_out;
  356. ret6->sin6_addr = in6->sin6_addr;
  357. }
  358. srcu_read_unlock(&connections_srcu, idx);
  359. return 0;
  360. }
  361. static int addr_to_nodeid(struct sockaddr_storage *addr, int *nodeid,
  362. unsigned int *mark)
  363. {
  364. struct connection *con;
  365. int i, idx, addr_i;
  366. idx = srcu_read_lock(&connections_srcu);
  367. for (i = 0; i < CONN_HASH_SIZE; i++) {
  368. hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
  369. WARN_ON_ONCE(!con->addr_count);
  370. spin_lock(&con->addrs_lock);
  371. for (addr_i = 0; addr_i < con->addr_count; addr_i++) {
  372. if (addr_compare(&con->addr[addr_i], addr)) {
  373. *nodeid = con->nodeid;
  374. *mark = con->mark;
  375. spin_unlock(&con->addrs_lock);
  376. srcu_read_unlock(&connections_srcu, idx);
  377. return 0;
  378. }
  379. }
  380. spin_unlock(&con->addrs_lock);
  381. }
  382. }
  383. srcu_read_unlock(&connections_srcu, idx);
  384. return -ENOENT;
  385. }
  386. static bool dlm_lowcomms_con_has_addr(const struct connection *con,
  387. const struct sockaddr_storage *addr)
  388. {
  389. int i;
  390. for (i = 0; i < con->addr_count; i++) {
  391. if (addr_compare(&con->addr[i], addr))
  392. return true;
  393. }
  394. return false;
  395. }
  396. int dlm_lowcomms_addr(int nodeid, struct sockaddr_storage *addr)
  397. {
  398. struct connection *con;
  399. bool ret;
  400. int idx;
  401. idx = srcu_read_lock(&connections_srcu);
  402. con = nodeid2con(nodeid, GFP_NOFS);
  403. if (!con) {
  404. srcu_read_unlock(&connections_srcu, idx);
  405. return -ENOMEM;
  406. }
  407. spin_lock(&con->addrs_lock);
  408. if (!con->addr_count) {
  409. memcpy(&con->addr[0], addr, sizeof(*addr));
  410. con->addr_count = 1;
  411. con->mark = dlm_config.ci_mark;
  412. spin_unlock(&con->addrs_lock);
  413. srcu_read_unlock(&connections_srcu, idx);
  414. return 0;
  415. }
  416. ret = dlm_lowcomms_con_has_addr(con, addr);
  417. if (ret) {
  418. spin_unlock(&con->addrs_lock);
  419. srcu_read_unlock(&connections_srcu, idx);
  420. return -EEXIST;
  421. }
  422. if (con->addr_count >= DLM_MAX_ADDR_COUNT) {
  423. spin_unlock(&con->addrs_lock);
  424. srcu_read_unlock(&connections_srcu, idx);
  425. return -ENOSPC;
  426. }
  427. memcpy(&con->addr[con->addr_count++], addr, sizeof(*addr));
  428. srcu_read_unlock(&connections_srcu, idx);
  429. spin_unlock(&con->addrs_lock);
  430. return 0;
  431. }
  432. /* Data available on socket or listen socket received a connect */
  433. static void lowcomms_data_ready(struct sock *sk)
  434. {
  435. struct connection *con = sock2con(sk);
  436. trace_sk_data_ready(sk);
  437. set_bit(CF_RECV_INTR, &con->flags);
  438. lowcomms_queue_rwork(con);
  439. }
  440. static void lowcomms_write_space(struct sock *sk)
  441. {
  442. struct connection *con = sock2con(sk);
  443. clear_bit(SOCK_NOSPACE, &con->sock->flags);
  444. spin_lock_bh(&con->writequeue_lock);
  445. if (test_and_clear_bit(CF_APP_LIMITED, &con->flags)) {
  446. con->sock->sk->sk_write_pending--;
  447. clear_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags);
  448. }
  449. lowcomms_queue_swork(con);
  450. spin_unlock_bh(&con->writequeue_lock);
  451. }
  452. static void lowcomms_state_change(struct sock *sk)
  453. {
  454. /* SCTP layer is not calling sk_data_ready when the connection
  455. * is done, so we catch the signal through here.
  456. */
  457. if (sk->sk_shutdown == RCV_SHUTDOWN)
  458. lowcomms_data_ready(sk);
  459. }
  460. static void lowcomms_listen_data_ready(struct sock *sk)
  461. {
  462. trace_sk_data_ready(sk);
  463. queue_work(io_workqueue, &listen_con.rwork);
  464. }
  465. int dlm_lowcomms_connect_node(int nodeid)
  466. {
  467. struct connection *con;
  468. int idx;
  469. idx = srcu_read_lock(&connections_srcu);
  470. con = nodeid2con(nodeid, 0);
  471. if (WARN_ON_ONCE(!con)) {
  472. srcu_read_unlock(&connections_srcu, idx);
  473. return -ENOENT;
  474. }
  475. down_read(&con->sock_lock);
  476. if (!con->sock) {
  477. spin_lock_bh(&con->writequeue_lock);
  478. lowcomms_queue_swork(con);
  479. spin_unlock_bh(&con->writequeue_lock);
  480. }
  481. up_read(&con->sock_lock);
  482. srcu_read_unlock(&connections_srcu, idx);
  483. cond_resched();
  484. return 0;
  485. }
  486. int dlm_lowcomms_nodes_set_mark(int nodeid, unsigned int mark)
  487. {
  488. struct connection *con;
  489. int idx;
  490. idx = srcu_read_lock(&connections_srcu);
  491. con = nodeid2con(nodeid, 0);
  492. if (!con) {
  493. srcu_read_unlock(&connections_srcu, idx);
  494. return -ENOENT;
  495. }
  496. spin_lock(&con->addrs_lock);
  497. con->mark = mark;
  498. spin_unlock(&con->addrs_lock);
  499. srcu_read_unlock(&connections_srcu, idx);
  500. return 0;
  501. }
  502. static void lowcomms_error_report(struct sock *sk)
  503. {
  504. struct connection *con = sock2con(sk);
  505. struct inet_sock *inet;
  506. inet = inet_sk(sk);
  507. switch (sk->sk_family) {
  508. case AF_INET:
  509. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  510. "sending to node %d at %pI4, dport %d, "
  511. "sk_err=%d/%d\n", dlm_our_nodeid(),
  512. con->nodeid, &inet->inet_daddr,
  513. ntohs(inet->inet_dport), sk->sk_err,
  514. READ_ONCE(sk->sk_err_soft));
  515. break;
  516. #if IS_ENABLED(CONFIG_IPV6)
  517. case AF_INET6:
  518. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  519. "sending to node %d at %pI6c, "
  520. "dport %d, sk_err=%d/%d\n", dlm_our_nodeid(),
  521. con->nodeid, &sk->sk_v6_daddr,
  522. ntohs(inet->inet_dport), sk->sk_err,
  523. READ_ONCE(sk->sk_err_soft));
  524. break;
  525. #endif
  526. default:
  527. printk_ratelimited(KERN_ERR "dlm: node %d: socket error "
  528. "invalid socket family %d set, "
  529. "sk_err=%d/%d\n", dlm_our_nodeid(),
  530. sk->sk_family, sk->sk_err,
  531. READ_ONCE(sk->sk_err_soft));
  532. break;
  533. }
  534. dlm_midcomms_unack_msg_resend(con->nodeid);
  535. listen_sock.sk_error_report(sk);
  536. }
  537. static void restore_callbacks(struct sock *sk)
  538. {
  539. #ifdef CONFIG_LOCKDEP
  540. WARN_ON_ONCE(!lockdep_sock_is_held(sk));
  541. #endif
  542. sk->sk_user_data = NULL;
  543. sk->sk_data_ready = listen_sock.sk_data_ready;
  544. sk->sk_state_change = listen_sock.sk_state_change;
  545. sk->sk_write_space = listen_sock.sk_write_space;
  546. sk->sk_error_report = listen_sock.sk_error_report;
  547. }
  548. /* Make a socket active */
  549. static void add_sock(struct socket *sock, struct connection *con)
  550. {
  551. struct sock *sk = sock->sk;
  552. lock_sock(sk);
  553. con->sock = sock;
  554. sk->sk_user_data = con;
  555. sk->sk_data_ready = lowcomms_data_ready;
  556. sk->sk_write_space = lowcomms_write_space;
  557. if (dlm_config.ci_protocol == DLM_PROTO_SCTP)
  558. sk->sk_state_change = lowcomms_state_change;
  559. sk->sk_allocation = GFP_NOFS;
  560. sk->sk_use_task_frag = false;
  561. sk->sk_error_report = lowcomms_error_report;
  562. release_sock(sk);
  563. }
  564. /* Add the port number to an IPv6 or 4 sockaddr and return the address
  565. length */
  566. static void make_sockaddr(struct sockaddr_storage *saddr, uint16_t port,
  567. int *addr_len)
  568. {
  569. saddr->ss_family = dlm_local_addr[0].ss_family;
  570. if (saddr->ss_family == AF_INET) {
  571. struct sockaddr_in *in4_addr = (struct sockaddr_in *)saddr;
  572. in4_addr->sin_port = cpu_to_be16(port);
  573. *addr_len = sizeof(struct sockaddr_in);
  574. memset(&in4_addr->sin_zero, 0, sizeof(in4_addr->sin_zero));
  575. } else {
  576. struct sockaddr_in6 *in6_addr = (struct sockaddr_in6 *)saddr;
  577. in6_addr->sin6_port = cpu_to_be16(port);
  578. *addr_len = sizeof(struct sockaddr_in6);
  579. }
  580. memset((char *)saddr + *addr_len, 0, sizeof(struct sockaddr_storage) - *addr_len);
  581. }
  582. static void dlm_page_release(struct kref *kref)
  583. {
  584. struct writequeue_entry *e = container_of(kref, struct writequeue_entry,
  585. ref);
  586. __free_page(e->page);
  587. dlm_free_writequeue(e);
  588. }
  589. static void dlm_msg_release(struct kref *kref)
  590. {
  591. struct dlm_msg *msg = container_of(kref, struct dlm_msg, ref);
  592. kref_put(&msg->entry->ref, dlm_page_release);
  593. dlm_free_msg(msg);
  594. }
  595. static void free_entry(struct writequeue_entry *e)
  596. {
  597. struct dlm_msg *msg, *tmp;
  598. list_for_each_entry_safe(msg, tmp, &e->msgs, list) {
  599. if (msg->orig_msg) {
  600. msg->orig_msg->retransmit = false;
  601. kref_put(&msg->orig_msg->ref, dlm_msg_release);
  602. }
  603. list_del(&msg->list);
  604. kref_put(&msg->ref, dlm_msg_release);
  605. }
  606. list_del(&e->list);
  607. kref_put(&e->ref, dlm_page_release);
  608. }
  609. static void dlm_close_sock(struct socket **sock)
  610. {
  611. lock_sock((*sock)->sk);
  612. restore_callbacks((*sock)->sk);
  613. release_sock((*sock)->sk);
  614. sock_release(*sock);
  615. *sock = NULL;
  616. }
  617. static void allow_connection_io(struct connection *con)
  618. {
  619. if (con->othercon)
  620. clear_bit(CF_IO_STOP, &con->othercon->flags);
  621. clear_bit(CF_IO_STOP, &con->flags);
  622. }
  623. static void stop_connection_io(struct connection *con)
  624. {
  625. if (con->othercon)
  626. stop_connection_io(con->othercon);
  627. spin_lock_bh(&con->writequeue_lock);
  628. set_bit(CF_IO_STOP, &con->flags);
  629. spin_unlock_bh(&con->writequeue_lock);
  630. down_write(&con->sock_lock);
  631. if (con->sock) {
  632. lock_sock(con->sock->sk);
  633. restore_callbacks(con->sock->sk);
  634. release_sock(con->sock->sk);
  635. }
  636. up_write(&con->sock_lock);
  637. cancel_work_sync(&con->swork);
  638. cancel_work_sync(&con->rwork);
  639. }
  640. /* Close a remote connection and tidy up */
  641. static void close_connection(struct connection *con, bool and_other)
  642. {
  643. struct writequeue_entry *e;
  644. if (con->othercon && and_other)
  645. close_connection(con->othercon, false);
  646. down_write(&con->sock_lock);
  647. if (!con->sock) {
  648. up_write(&con->sock_lock);
  649. return;
  650. }
  651. dlm_close_sock(&con->sock);
  652. /* if we send a writequeue entry only a half way, we drop the
  653. * whole entry because reconnection and that we not start of the
  654. * middle of a msg which will confuse the other end.
  655. *
  656. * we can always drop messages because retransmits, but what we
  657. * cannot allow is to transmit half messages which may be processed
  658. * at the other side.
  659. *
  660. * our policy is to start on a clean state when disconnects, we don't
  661. * know what's send/received on transport layer in this case.
  662. */
  663. spin_lock_bh(&con->writequeue_lock);
  664. if (!list_empty(&con->writequeue)) {
  665. e = list_first_entry(&con->writequeue, struct writequeue_entry,
  666. list);
  667. if (e->dirty)
  668. free_entry(e);
  669. }
  670. spin_unlock_bh(&con->writequeue_lock);
  671. con->rx_leftover = 0;
  672. con->retries = 0;
  673. clear_bit(CF_APP_LIMITED, &con->flags);
  674. clear_bit(CF_RECV_PENDING, &con->flags);
  675. clear_bit(CF_SEND_PENDING, &con->flags);
  676. up_write(&con->sock_lock);
  677. }
  678. static void shutdown_connection(struct connection *con, bool and_other)
  679. {
  680. int ret;
  681. if (con->othercon && and_other)
  682. shutdown_connection(con->othercon, false);
  683. flush_workqueue(io_workqueue);
  684. down_read(&con->sock_lock);
  685. /* nothing to shutdown */
  686. if (!con->sock) {
  687. up_read(&con->sock_lock);
  688. return;
  689. }
  690. ret = kernel_sock_shutdown(con->sock, SHUT_WR);
  691. up_read(&con->sock_lock);
  692. if (ret) {
  693. log_print("Connection %p failed to shutdown: %d will force close",
  694. con, ret);
  695. goto force_close;
  696. } else {
  697. ret = wait_event_timeout(con->shutdown_wait, !con->sock,
  698. DLM_SHUTDOWN_WAIT_TIMEOUT);
  699. if (ret == 0) {
  700. log_print("Connection %p shutdown timed out, will force close",
  701. con);
  702. goto force_close;
  703. }
  704. }
  705. return;
  706. force_close:
  707. close_connection(con, false);
  708. }
  709. static struct processqueue_entry *new_processqueue_entry(int nodeid,
  710. int buflen)
  711. {
  712. struct processqueue_entry *pentry;
  713. pentry = kmalloc(sizeof(*pentry), GFP_NOFS);
  714. if (!pentry)
  715. return NULL;
  716. pentry->buf = kmalloc(buflen, GFP_NOFS);
  717. if (!pentry->buf) {
  718. kfree(pentry);
  719. return NULL;
  720. }
  721. pentry->nodeid = nodeid;
  722. return pentry;
  723. }
  724. static void free_processqueue_entry(struct processqueue_entry *pentry)
  725. {
  726. kfree(pentry->buf);
  727. kfree(pentry);
  728. }
  729. static void process_dlm_messages(struct work_struct *work)
  730. {
  731. struct processqueue_entry *pentry;
  732. spin_lock_bh(&processqueue_lock);
  733. pentry = list_first_entry_or_null(&processqueue,
  734. struct processqueue_entry, list);
  735. if (WARN_ON_ONCE(!pentry)) {
  736. process_dlm_messages_pending = false;
  737. spin_unlock_bh(&processqueue_lock);
  738. return;
  739. }
  740. list_del(&pentry->list);
  741. if (atomic_dec_and_test(&processqueue_count))
  742. wake_up(&processqueue_wq);
  743. spin_unlock_bh(&processqueue_lock);
  744. for (;;) {
  745. dlm_process_incoming_buffer(pentry->nodeid, pentry->buf,
  746. pentry->buflen);
  747. free_processqueue_entry(pentry);
  748. spin_lock_bh(&processqueue_lock);
  749. pentry = list_first_entry_or_null(&processqueue,
  750. struct processqueue_entry, list);
  751. if (!pentry) {
  752. process_dlm_messages_pending = false;
  753. spin_unlock_bh(&processqueue_lock);
  754. break;
  755. }
  756. list_del(&pentry->list);
  757. if (atomic_dec_and_test(&processqueue_count))
  758. wake_up(&processqueue_wq);
  759. spin_unlock_bh(&processqueue_lock);
  760. }
  761. }
  762. /* Data received from remote end */
  763. static int receive_from_sock(struct connection *con, int buflen)
  764. {
  765. struct processqueue_entry *pentry;
  766. int ret, buflen_real;
  767. struct msghdr msg;
  768. struct kvec iov;
  769. pentry = new_processqueue_entry(con->nodeid, buflen);
  770. if (!pentry)
  771. return DLM_IO_RESCHED;
  772. memcpy(pentry->buf, con->rx_leftover_buf, con->rx_leftover);
  773. /* calculate new buffer parameter regarding last receive and
  774. * possible leftover bytes
  775. */
  776. iov.iov_base = pentry->buf + con->rx_leftover;
  777. iov.iov_len = buflen - con->rx_leftover;
  778. memset(&msg, 0, sizeof(msg));
  779. msg.msg_flags = MSG_DONTWAIT | MSG_NOSIGNAL;
  780. clear_bit(CF_RECV_INTR, &con->flags);
  781. again:
  782. ret = kernel_recvmsg(con->sock, &msg, &iov, 1, iov.iov_len,
  783. msg.msg_flags);
  784. trace_dlm_recv(con->nodeid, ret);
  785. if (ret == -EAGAIN) {
  786. lock_sock(con->sock->sk);
  787. if (test_and_clear_bit(CF_RECV_INTR, &con->flags)) {
  788. release_sock(con->sock->sk);
  789. goto again;
  790. }
  791. clear_bit(CF_RECV_PENDING, &con->flags);
  792. release_sock(con->sock->sk);
  793. free_processqueue_entry(pentry);
  794. return DLM_IO_END;
  795. } else if (ret == 0) {
  796. /* close will clear CF_RECV_PENDING */
  797. free_processqueue_entry(pentry);
  798. return DLM_IO_EOF;
  799. } else if (ret < 0) {
  800. free_processqueue_entry(pentry);
  801. return ret;
  802. }
  803. /* new buflen according readed bytes and leftover from last receive */
  804. buflen_real = ret + con->rx_leftover;
  805. ret = dlm_validate_incoming_buffer(con->nodeid, pentry->buf,
  806. buflen_real);
  807. if (ret < 0) {
  808. free_processqueue_entry(pentry);
  809. return ret;
  810. }
  811. pentry->buflen = ret;
  812. /* calculate leftover bytes from process and put it into begin of
  813. * the receive buffer, so next receive we have the full message
  814. * at the start address of the receive buffer.
  815. */
  816. con->rx_leftover = buflen_real - ret;
  817. memmove(con->rx_leftover_buf, pentry->buf + ret,
  818. con->rx_leftover);
  819. spin_lock_bh(&processqueue_lock);
  820. ret = atomic_inc_return(&processqueue_count);
  821. list_add_tail(&pentry->list, &processqueue);
  822. if (!process_dlm_messages_pending) {
  823. process_dlm_messages_pending = true;
  824. queue_work(process_workqueue, &process_work);
  825. }
  826. spin_unlock_bh(&processqueue_lock);
  827. if (ret > DLM_MAX_PROCESS_BUFFERS)
  828. return DLM_IO_FLUSH;
  829. return DLM_IO_SUCCESS;
  830. }
  831. /* Listening socket is busy, accept a connection */
  832. static int accept_from_sock(void)
  833. {
  834. struct sockaddr_storage peeraddr;
  835. int len, idx, result, nodeid;
  836. struct connection *newcon;
  837. struct socket *newsock;
  838. unsigned int mark;
  839. result = kernel_accept(listen_con.sock, &newsock, O_NONBLOCK);
  840. if (result == -EAGAIN)
  841. return DLM_IO_END;
  842. else if (result < 0)
  843. goto accept_err;
  844. /* Get the connected socket's peer */
  845. memset(&peeraddr, 0, sizeof(peeraddr));
  846. len = newsock->ops->getname(newsock, (struct sockaddr *)&peeraddr, 2);
  847. if (len < 0) {
  848. result = -ECONNABORTED;
  849. goto accept_err;
  850. }
  851. /* Get the new node's NODEID */
  852. make_sockaddr(&peeraddr, 0, &len);
  853. if (addr_to_nodeid(&peeraddr, &nodeid, &mark)) {
  854. switch (peeraddr.ss_family) {
  855. case AF_INET: {
  856. struct sockaddr_in *sin = (struct sockaddr_in *)&peeraddr;
  857. log_print("connect from non cluster IPv4 node %pI4",
  858. &sin->sin_addr);
  859. break;
  860. }
  861. #if IS_ENABLED(CONFIG_IPV6)
  862. case AF_INET6: {
  863. struct sockaddr_in6 *sin6 = (struct sockaddr_in6 *)&peeraddr;
  864. log_print("connect from non cluster IPv6 node %pI6c",
  865. &sin6->sin6_addr);
  866. break;
  867. }
  868. #endif
  869. default:
  870. log_print("invalid family from non cluster node");
  871. break;
  872. }
  873. sock_release(newsock);
  874. return -1;
  875. }
  876. log_print("got connection from %d", nodeid);
  877. /* Check to see if we already have a connection to this node. This
  878. * could happen if the two nodes initiate a connection at roughly
  879. * the same time and the connections cross on the wire.
  880. * In this case we store the incoming one in "othercon"
  881. */
  882. idx = srcu_read_lock(&connections_srcu);
  883. newcon = nodeid2con(nodeid, 0);
  884. if (WARN_ON_ONCE(!newcon)) {
  885. srcu_read_unlock(&connections_srcu, idx);
  886. result = -ENOENT;
  887. goto accept_err;
  888. }
  889. sock_set_mark(newsock->sk, mark);
  890. down_write(&newcon->sock_lock);
  891. if (newcon->sock) {
  892. struct connection *othercon = newcon->othercon;
  893. if (!othercon) {
  894. othercon = kzalloc(sizeof(*othercon), GFP_NOFS);
  895. if (!othercon) {
  896. log_print("failed to allocate incoming socket");
  897. up_write(&newcon->sock_lock);
  898. srcu_read_unlock(&connections_srcu, idx);
  899. result = -ENOMEM;
  900. goto accept_err;
  901. }
  902. dlm_con_init(othercon, nodeid);
  903. lockdep_set_subclass(&othercon->sock_lock, 1);
  904. newcon->othercon = othercon;
  905. set_bit(CF_IS_OTHERCON, &othercon->flags);
  906. } else {
  907. /* close other sock con if we have something new */
  908. close_connection(othercon, false);
  909. }
  910. down_write(&othercon->sock_lock);
  911. add_sock(newsock, othercon);
  912. /* check if we receved something while adding */
  913. lock_sock(othercon->sock->sk);
  914. lowcomms_queue_rwork(othercon);
  915. release_sock(othercon->sock->sk);
  916. up_write(&othercon->sock_lock);
  917. }
  918. else {
  919. /* accept copies the sk after we've saved the callbacks, so we
  920. don't want to save them a second time or comm errors will
  921. result in calling sk_error_report recursively. */
  922. add_sock(newsock, newcon);
  923. /* check if we receved something while adding */
  924. lock_sock(newcon->sock->sk);
  925. lowcomms_queue_rwork(newcon);
  926. release_sock(newcon->sock->sk);
  927. }
  928. up_write(&newcon->sock_lock);
  929. srcu_read_unlock(&connections_srcu, idx);
  930. return DLM_IO_SUCCESS;
  931. accept_err:
  932. if (newsock)
  933. sock_release(newsock);
  934. return result;
  935. }
  936. /*
  937. * writequeue_entry_complete - try to delete and free write queue entry
  938. * @e: write queue entry to try to delete
  939. * @completed: bytes completed
  940. *
  941. * writequeue_lock must be held.
  942. */
  943. static void writequeue_entry_complete(struct writequeue_entry *e, int completed)
  944. {
  945. e->offset += completed;
  946. e->len -= completed;
  947. /* signal that page was half way transmitted */
  948. e->dirty = true;
  949. if (e->len == 0 && e->users == 0)
  950. free_entry(e);
  951. }
  952. /*
  953. * sctp_bind_addrs - bind a SCTP socket to all our addresses
  954. */
  955. static int sctp_bind_addrs(struct socket *sock, uint16_t port)
  956. {
  957. struct sockaddr_storage localaddr;
  958. struct sockaddr *addr = (struct sockaddr *)&localaddr;
  959. int i, addr_len, result = 0;
  960. for (i = 0; i < dlm_local_count; i++) {
  961. memcpy(&localaddr, &dlm_local_addr[i], sizeof(localaddr));
  962. make_sockaddr(&localaddr, port, &addr_len);
  963. if (!i)
  964. result = kernel_bind(sock, addr, addr_len);
  965. else
  966. result = sock_bind_add(sock->sk, addr, addr_len);
  967. if (result < 0) {
  968. log_print("Can't bind to %d addr number %d, %d.\n",
  969. port, i + 1, result);
  970. break;
  971. }
  972. }
  973. return result;
  974. }
  975. /* Get local addresses */
  976. static void init_local(void)
  977. {
  978. struct sockaddr_storage sas;
  979. int i;
  980. dlm_local_count = 0;
  981. for (i = 0; i < DLM_MAX_ADDR_COUNT; i++) {
  982. if (dlm_our_addr(&sas, i))
  983. break;
  984. memcpy(&dlm_local_addr[dlm_local_count++], &sas, sizeof(sas));
  985. }
  986. }
  987. static struct writequeue_entry *new_writequeue_entry(struct connection *con)
  988. {
  989. struct writequeue_entry *entry;
  990. entry = dlm_allocate_writequeue();
  991. if (!entry)
  992. return NULL;
  993. entry->page = alloc_page(GFP_ATOMIC | __GFP_ZERO);
  994. if (!entry->page) {
  995. dlm_free_writequeue(entry);
  996. return NULL;
  997. }
  998. entry->offset = 0;
  999. entry->len = 0;
  1000. entry->end = 0;
  1001. entry->dirty = false;
  1002. entry->con = con;
  1003. entry->users = 1;
  1004. kref_init(&entry->ref);
  1005. return entry;
  1006. }
  1007. static struct writequeue_entry *new_wq_entry(struct connection *con, int len,
  1008. char **ppc, void (*cb)(void *data),
  1009. void *data)
  1010. {
  1011. struct writequeue_entry *e;
  1012. spin_lock_bh(&con->writequeue_lock);
  1013. if (!list_empty(&con->writequeue)) {
  1014. e = list_last_entry(&con->writequeue, struct writequeue_entry, list);
  1015. if (DLM_WQ_REMAIN_BYTES(e) >= len) {
  1016. kref_get(&e->ref);
  1017. *ppc = page_address(e->page) + e->end;
  1018. if (cb)
  1019. cb(data);
  1020. e->end += len;
  1021. e->users++;
  1022. goto out;
  1023. }
  1024. }
  1025. e = new_writequeue_entry(con);
  1026. if (!e)
  1027. goto out;
  1028. kref_get(&e->ref);
  1029. *ppc = page_address(e->page);
  1030. e->end += len;
  1031. if (cb)
  1032. cb(data);
  1033. list_add_tail(&e->list, &con->writequeue);
  1034. out:
  1035. spin_unlock_bh(&con->writequeue_lock);
  1036. return e;
  1037. };
  1038. static struct dlm_msg *dlm_lowcomms_new_msg_con(struct connection *con, int len,
  1039. char **ppc, void (*cb)(void *data),
  1040. void *data)
  1041. {
  1042. struct writequeue_entry *e;
  1043. struct dlm_msg *msg;
  1044. msg = dlm_allocate_msg();
  1045. if (!msg)
  1046. return NULL;
  1047. kref_init(&msg->ref);
  1048. e = new_wq_entry(con, len, ppc, cb, data);
  1049. if (!e) {
  1050. dlm_free_msg(msg);
  1051. return NULL;
  1052. }
  1053. msg->retransmit = false;
  1054. msg->orig_msg = NULL;
  1055. msg->ppc = *ppc;
  1056. msg->len = len;
  1057. msg->entry = e;
  1058. return msg;
  1059. }
  1060. /* avoid false positive for nodes_srcu, unlock happens in
  1061. * dlm_lowcomms_commit_msg which is a must call if success
  1062. */
  1063. #ifndef __CHECKER__
  1064. struct dlm_msg *dlm_lowcomms_new_msg(int nodeid, int len, char **ppc,
  1065. void (*cb)(void *data), void *data)
  1066. {
  1067. struct connection *con;
  1068. struct dlm_msg *msg;
  1069. int idx;
  1070. if (len > DLM_MAX_SOCKET_BUFSIZE ||
  1071. len < sizeof(struct dlm_header)) {
  1072. BUILD_BUG_ON(PAGE_SIZE < DLM_MAX_SOCKET_BUFSIZE);
  1073. log_print("failed to allocate a buffer of size %d", len);
  1074. WARN_ON_ONCE(1);
  1075. return NULL;
  1076. }
  1077. idx = srcu_read_lock(&connections_srcu);
  1078. con = nodeid2con(nodeid, 0);
  1079. if (WARN_ON_ONCE(!con)) {
  1080. srcu_read_unlock(&connections_srcu, idx);
  1081. return NULL;
  1082. }
  1083. msg = dlm_lowcomms_new_msg_con(con, len, ppc, cb, data);
  1084. if (!msg) {
  1085. srcu_read_unlock(&connections_srcu, idx);
  1086. return NULL;
  1087. }
  1088. /* for dlm_lowcomms_commit_msg() */
  1089. kref_get(&msg->ref);
  1090. /* we assume if successful commit must called */
  1091. msg->idx = idx;
  1092. return msg;
  1093. }
  1094. #endif
  1095. static void _dlm_lowcomms_commit_msg(struct dlm_msg *msg)
  1096. {
  1097. struct writequeue_entry *e = msg->entry;
  1098. struct connection *con = e->con;
  1099. int users;
  1100. spin_lock_bh(&con->writequeue_lock);
  1101. kref_get(&msg->ref);
  1102. list_add(&msg->list, &e->msgs);
  1103. users = --e->users;
  1104. if (users)
  1105. goto out;
  1106. e->len = DLM_WQ_LENGTH_BYTES(e);
  1107. lowcomms_queue_swork(con);
  1108. out:
  1109. spin_unlock_bh(&con->writequeue_lock);
  1110. return;
  1111. }
  1112. /* avoid false positive for nodes_srcu, lock was happen in
  1113. * dlm_lowcomms_new_msg
  1114. */
  1115. #ifndef __CHECKER__
  1116. void dlm_lowcomms_commit_msg(struct dlm_msg *msg)
  1117. {
  1118. _dlm_lowcomms_commit_msg(msg);
  1119. srcu_read_unlock(&connections_srcu, msg->idx);
  1120. /* because dlm_lowcomms_new_msg() */
  1121. kref_put(&msg->ref, dlm_msg_release);
  1122. }
  1123. #endif
  1124. void dlm_lowcomms_put_msg(struct dlm_msg *msg)
  1125. {
  1126. kref_put(&msg->ref, dlm_msg_release);
  1127. }
  1128. /* does not held connections_srcu, usage lowcomms_error_report only */
  1129. int dlm_lowcomms_resend_msg(struct dlm_msg *msg)
  1130. {
  1131. struct dlm_msg *msg_resend;
  1132. char *ppc;
  1133. if (msg->retransmit)
  1134. return 1;
  1135. msg_resend = dlm_lowcomms_new_msg_con(msg->entry->con, msg->len, &ppc,
  1136. NULL, NULL);
  1137. if (!msg_resend)
  1138. return -ENOMEM;
  1139. msg->retransmit = true;
  1140. kref_get(&msg->ref);
  1141. msg_resend->orig_msg = msg;
  1142. memcpy(ppc, msg->ppc, msg->len);
  1143. _dlm_lowcomms_commit_msg(msg_resend);
  1144. dlm_lowcomms_put_msg(msg_resend);
  1145. return 0;
  1146. }
  1147. /* Send a message */
  1148. static int send_to_sock(struct connection *con)
  1149. {
  1150. struct writequeue_entry *e;
  1151. struct bio_vec bvec;
  1152. struct msghdr msg = {
  1153. .msg_flags = MSG_SPLICE_PAGES | MSG_DONTWAIT | MSG_NOSIGNAL,
  1154. };
  1155. int len, offset, ret;
  1156. spin_lock_bh(&con->writequeue_lock);
  1157. e = con_next_wq(con);
  1158. if (!e) {
  1159. clear_bit(CF_SEND_PENDING, &con->flags);
  1160. spin_unlock_bh(&con->writequeue_lock);
  1161. return DLM_IO_END;
  1162. }
  1163. len = e->len;
  1164. offset = e->offset;
  1165. WARN_ON_ONCE(len == 0 && e->users == 0);
  1166. spin_unlock_bh(&con->writequeue_lock);
  1167. bvec_set_page(&bvec, e->page, len, offset);
  1168. iov_iter_bvec(&msg.msg_iter, ITER_SOURCE, &bvec, 1, len);
  1169. ret = sock_sendmsg(con->sock, &msg);
  1170. trace_dlm_send(con->nodeid, ret);
  1171. if (ret == -EAGAIN || ret == 0) {
  1172. lock_sock(con->sock->sk);
  1173. spin_lock_bh(&con->writequeue_lock);
  1174. if (test_bit(SOCKWQ_ASYNC_NOSPACE, &con->sock->flags) &&
  1175. !test_and_set_bit(CF_APP_LIMITED, &con->flags)) {
  1176. /* Notify TCP that we're limited by the
  1177. * application window size.
  1178. */
  1179. set_bit(SOCK_NOSPACE, &con->sock->sk->sk_socket->flags);
  1180. con->sock->sk->sk_write_pending++;
  1181. clear_bit(CF_SEND_PENDING, &con->flags);
  1182. spin_unlock_bh(&con->writequeue_lock);
  1183. release_sock(con->sock->sk);
  1184. /* wait for write_space() event */
  1185. return DLM_IO_END;
  1186. }
  1187. spin_unlock_bh(&con->writequeue_lock);
  1188. release_sock(con->sock->sk);
  1189. return DLM_IO_RESCHED;
  1190. } else if (ret < 0) {
  1191. return ret;
  1192. }
  1193. spin_lock_bh(&con->writequeue_lock);
  1194. writequeue_entry_complete(e, ret);
  1195. spin_unlock_bh(&con->writequeue_lock);
  1196. return DLM_IO_SUCCESS;
  1197. }
  1198. static void clean_one_writequeue(struct connection *con)
  1199. {
  1200. struct writequeue_entry *e, *safe;
  1201. spin_lock_bh(&con->writequeue_lock);
  1202. list_for_each_entry_safe(e, safe, &con->writequeue, list) {
  1203. free_entry(e);
  1204. }
  1205. spin_unlock_bh(&con->writequeue_lock);
  1206. }
  1207. static void connection_release(struct rcu_head *rcu)
  1208. {
  1209. struct connection *con = container_of(rcu, struct connection, rcu);
  1210. WARN_ON_ONCE(!list_empty(&con->writequeue));
  1211. WARN_ON_ONCE(con->sock);
  1212. kfree(con);
  1213. }
  1214. /* Called from recovery when it knows that a node has
  1215. left the cluster */
  1216. int dlm_lowcomms_close(int nodeid)
  1217. {
  1218. struct connection *con;
  1219. int idx;
  1220. log_print("closing connection to node %d", nodeid);
  1221. idx = srcu_read_lock(&connections_srcu);
  1222. con = nodeid2con(nodeid, 0);
  1223. if (WARN_ON_ONCE(!con)) {
  1224. srcu_read_unlock(&connections_srcu, idx);
  1225. return -ENOENT;
  1226. }
  1227. stop_connection_io(con);
  1228. log_print("io handling for node: %d stopped", nodeid);
  1229. close_connection(con, true);
  1230. spin_lock(&connections_lock);
  1231. hlist_del_rcu(&con->list);
  1232. spin_unlock(&connections_lock);
  1233. clean_one_writequeue(con);
  1234. call_srcu(&connections_srcu, &con->rcu, connection_release);
  1235. if (con->othercon) {
  1236. clean_one_writequeue(con->othercon);
  1237. call_srcu(&connections_srcu, &con->othercon->rcu, connection_release);
  1238. }
  1239. srcu_read_unlock(&connections_srcu, idx);
  1240. /* for debugging we print when we are done to compare with other
  1241. * messages in between. This function need to be correctly synchronized
  1242. * with io handling
  1243. */
  1244. log_print("closing connection to node %d done", nodeid);
  1245. return 0;
  1246. }
  1247. /* Receive worker function */
  1248. static void process_recv_sockets(struct work_struct *work)
  1249. {
  1250. struct connection *con = container_of(work, struct connection, rwork);
  1251. int ret, buflen;
  1252. down_read(&con->sock_lock);
  1253. if (!con->sock) {
  1254. up_read(&con->sock_lock);
  1255. return;
  1256. }
  1257. buflen = READ_ONCE(dlm_config.ci_buffer_size);
  1258. do {
  1259. ret = receive_from_sock(con, buflen);
  1260. } while (ret == DLM_IO_SUCCESS);
  1261. up_read(&con->sock_lock);
  1262. switch (ret) {
  1263. case DLM_IO_END:
  1264. /* CF_RECV_PENDING cleared */
  1265. break;
  1266. case DLM_IO_EOF:
  1267. close_connection(con, false);
  1268. wake_up(&con->shutdown_wait);
  1269. /* CF_RECV_PENDING cleared */
  1270. break;
  1271. case DLM_IO_FLUSH:
  1272. /* we can't flush the process_workqueue here because a
  1273. * WQ_MEM_RECLAIM workequeue can occurr a deadlock for a non
  1274. * WQ_MEM_RECLAIM workqueue such as process_workqueue. Instead
  1275. * we have a waitqueue to wait until all messages are
  1276. * processed.
  1277. *
  1278. * This handling is only necessary to backoff the sender and
  1279. * not queue all messages from the socket layer into DLM
  1280. * processqueue. When DLM is capable to parse multiple messages
  1281. * on an e.g. per socket basis this handling can might be
  1282. * removed. Especially in a message burst we are too slow to
  1283. * process messages and the queue will fill up memory.
  1284. */
  1285. wait_event(processqueue_wq, !atomic_read(&processqueue_count));
  1286. fallthrough;
  1287. case DLM_IO_RESCHED:
  1288. cond_resched();
  1289. queue_work(io_workqueue, &con->rwork);
  1290. /* CF_RECV_PENDING not cleared */
  1291. break;
  1292. default:
  1293. if (ret < 0) {
  1294. if (test_bit(CF_IS_OTHERCON, &con->flags)) {
  1295. close_connection(con, false);
  1296. } else {
  1297. spin_lock_bh(&con->writequeue_lock);
  1298. lowcomms_queue_swork(con);
  1299. spin_unlock_bh(&con->writequeue_lock);
  1300. }
  1301. /* CF_RECV_PENDING cleared for othercon
  1302. * we trigger send queue if not already done
  1303. * and process_send_sockets will handle it
  1304. */
  1305. break;
  1306. }
  1307. WARN_ON_ONCE(1);
  1308. break;
  1309. }
  1310. }
  1311. static void process_listen_recv_socket(struct work_struct *work)
  1312. {
  1313. int ret;
  1314. if (WARN_ON_ONCE(!listen_con.sock))
  1315. return;
  1316. do {
  1317. ret = accept_from_sock();
  1318. } while (ret == DLM_IO_SUCCESS);
  1319. if (ret < 0)
  1320. log_print("critical error accepting connection: %d", ret);
  1321. }
  1322. static int dlm_connect(struct connection *con)
  1323. {
  1324. struct sockaddr_storage addr;
  1325. int result, addr_len;
  1326. struct socket *sock;
  1327. unsigned int mark;
  1328. memset(&addr, 0, sizeof(addr));
  1329. result = nodeid_to_addr(con->nodeid, &addr, NULL,
  1330. dlm_proto_ops->try_new_addr, &mark);
  1331. if (result < 0) {
  1332. log_print("no address for nodeid %d", con->nodeid);
  1333. return result;
  1334. }
  1335. /* Create a socket to communicate with */
  1336. result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
  1337. SOCK_STREAM, dlm_proto_ops->proto, &sock);
  1338. if (result < 0)
  1339. return result;
  1340. sock_set_mark(sock->sk, mark);
  1341. dlm_proto_ops->sockopts(sock);
  1342. result = dlm_proto_ops->bind(sock);
  1343. if (result < 0) {
  1344. sock_release(sock);
  1345. return result;
  1346. }
  1347. add_sock(sock, con);
  1348. log_print_ratelimited("connecting to %d", con->nodeid);
  1349. make_sockaddr(&addr, dlm_config.ci_tcp_port, &addr_len);
  1350. result = kernel_connect(sock, (struct sockaddr *)&addr, addr_len, 0);
  1351. switch (result) {
  1352. case -EINPROGRESS:
  1353. /* not an error */
  1354. fallthrough;
  1355. case 0:
  1356. break;
  1357. default:
  1358. if (result < 0)
  1359. dlm_close_sock(&con->sock);
  1360. break;
  1361. }
  1362. return result;
  1363. }
  1364. /* Send worker function */
  1365. static void process_send_sockets(struct work_struct *work)
  1366. {
  1367. struct connection *con = container_of(work, struct connection, swork);
  1368. int ret;
  1369. WARN_ON_ONCE(test_bit(CF_IS_OTHERCON, &con->flags));
  1370. down_read(&con->sock_lock);
  1371. if (!con->sock) {
  1372. up_read(&con->sock_lock);
  1373. down_write(&con->sock_lock);
  1374. if (!con->sock) {
  1375. ret = dlm_connect(con);
  1376. switch (ret) {
  1377. case 0:
  1378. break;
  1379. default:
  1380. /* CF_SEND_PENDING not cleared */
  1381. up_write(&con->sock_lock);
  1382. log_print("connect to node %d try %d error %d",
  1383. con->nodeid, con->retries++, ret);
  1384. msleep(1000);
  1385. /* For now we try forever to reconnect. In
  1386. * future we should send a event to cluster
  1387. * manager to fence itself after certain amount
  1388. * of retries.
  1389. */
  1390. queue_work(io_workqueue, &con->swork);
  1391. return;
  1392. }
  1393. }
  1394. downgrade_write(&con->sock_lock);
  1395. }
  1396. do {
  1397. ret = send_to_sock(con);
  1398. } while (ret == DLM_IO_SUCCESS);
  1399. up_read(&con->sock_lock);
  1400. switch (ret) {
  1401. case DLM_IO_END:
  1402. /* CF_SEND_PENDING cleared */
  1403. break;
  1404. case DLM_IO_RESCHED:
  1405. /* CF_SEND_PENDING not cleared */
  1406. cond_resched();
  1407. queue_work(io_workqueue, &con->swork);
  1408. break;
  1409. default:
  1410. if (ret < 0) {
  1411. close_connection(con, false);
  1412. /* CF_SEND_PENDING cleared */
  1413. spin_lock_bh(&con->writequeue_lock);
  1414. lowcomms_queue_swork(con);
  1415. spin_unlock_bh(&con->writequeue_lock);
  1416. break;
  1417. }
  1418. WARN_ON_ONCE(1);
  1419. break;
  1420. }
  1421. }
  1422. static void work_stop(void)
  1423. {
  1424. if (io_workqueue) {
  1425. destroy_workqueue(io_workqueue);
  1426. io_workqueue = NULL;
  1427. }
  1428. if (process_workqueue) {
  1429. destroy_workqueue(process_workqueue);
  1430. process_workqueue = NULL;
  1431. }
  1432. }
  1433. static int work_start(void)
  1434. {
  1435. io_workqueue = alloc_workqueue("dlm_io", WQ_HIGHPRI | WQ_MEM_RECLAIM |
  1436. WQ_UNBOUND, 0);
  1437. if (!io_workqueue) {
  1438. log_print("can't start dlm_io");
  1439. return -ENOMEM;
  1440. }
  1441. process_workqueue = alloc_workqueue("dlm_process", WQ_HIGHPRI | WQ_BH, 0);
  1442. if (!process_workqueue) {
  1443. log_print("can't start dlm_process");
  1444. destroy_workqueue(io_workqueue);
  1445. io_workqueue = NULL;
  1446. return -ENOMEM;
  1447. }
  1448. return 0;
  1449. }
  1450. void dlm_lowcomms_shutdown(void)
  1451. {
  1452. struct connection *con;
  1453. int i, idx;
  1454. /* stop lowcomms_listen_data_ready calls */
  1455. lock_sock(listen_con.sock->sk);
  1456. listen_con.sock->sk->sk_data_ready = listen_sock.sk_data_ready;
  1457. release_sock(listen_con.sock->sk);
  1458. cancel_work_sync(&listen_con.rwork);
  1459. dlm_close_sock(&listen_con.sock);
  1460. idx = srcu_read_lock(&connections_srcu);
  1461. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1462. hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
  1463. shutdown_connection(con, true);
  1464. stop_connection_io(con);
  1465. flush_workqueue(process_workqueue);
  1466. close_connection(con, true);
  1467. clean_one_writequeue(con);
  1468. if (con->othercon)
  1469. clean_one_writequeue(con->othercon);
  1470. allow_connection_io(con);
  1471. }
  1472. }
  1473. srcu_read_unlock(&connections_srcu, idx);
  1474. }
  1475. void dlm_lowcomms_stop(void)
  1476. {
  1477. work_stop();
  1478. dlm_proto_ops = NULL;
  1479. }
  1480. static int dlm_listen_for_all(void)
  1481. {
  1482. struct socket *sock;
  1483. int result;
  1484. log_print("Using %s for communications",
  1485. dlm_proto_ops->name);
  1486. result = dlm_proto_ops->listen_validate();
  1487. if (result < 0)
  1488. return result;
  1489. result = sock_create_kern(&init_net, dlm_local_addr[0].ss_family,
  1490. SOCK_STREAM, dlm_proto_ops->proto, &sock);
  1491. if (result < 0) {
  1492. log_print("Can't create comms socket: %d", result);
  1493. return result;
  1494. }
  1495. sock_set_mark(sock->sk, dlm_config.ci_mark);
  1496. dlm_proto_ops->listen_sockopts(sock);
  1497. result = dlm_proto_ops->listen_bind(sock);
  1498. if (result < 0)
  1499. goto out;
  1500. lock_sock(sock->sk);
  1501. listen_sock.sk_data_ready = sock->sk->sk_data_ready;
  1502. listen_sock.sk_write_space = sock->sk->sk_write_space;
  1503. listen_sock.sk_error_report = sock->sk->sk_error_report;
  1504. listen_sock.sk_state_change = sock->sk->sk_state_change;
  1505. listen_con.sock = sock;
  1506. sock->sk->sk_allocation = GFP_NOFS;
  1507. sock->sk->sk_use_task_frag = false;
  1508. sock->sk->sk_data_ready = lowcomms_listen_data_ready;
  1509. release_sock(sock->sk);
  1510. result = sock->ops->listen(sock, 128);
  1511. if (result < 0) {
  1512. dlm_close_sock(&listen_con.sock);
  1513. return result;
  1514. }
  1515. return 0;
  1516. out:
  1517. sock_release(sock);
  1518. return result;
  1519. }
  1520. static int dlm_tcp_bind(struct socket *sock)
  1521. {
  1522. struct sockaddr_storage src_addr;
  1523. int result, addr_len;
  1524. /* Bind to our cluster-known address connecting to avoid
  1525. * routing problems.
  1526. */
  1527. memcpy(&src_addr, &dlm_local_addr[0], sizeof(src_addr));
  1528. make_sockaddr(&src_addr, 0, &addr_len);
  1529. result = kernel_bind(sock, (struct sockaddr *)&src_addr,
  1530. addr_len);
  1531. if (result < 0) {
  1532. /* This *may* not indicate a critical error */
  1533. log_print("could not bind for connect: %d", result);
  1534. }
  1535. return 0;
  1536. }
  1537. static int dlm_tcp_listen_validate(void)
  1538. {
  1539. /* We don't support multi-homed hosts */
  1540. if (dlm_local_count > 1) {
  1541. log_print("Detect multi-homed hosts but use only the first IP address.");
  1542. log_print("Try SCTP, if you want to enable multi-link.");
  1543. }
  1544. return 0;
  1545. }
  1546. static void dlm_tcp_sockopts(struct socket *sock)
  1547. {
  1548. /* Turn off Nagle's algorithm */
  1549. tcp_sock_set_nodelay(sock->sk);
  1550. }
  1551. static void dlm_tcp_listen_sockopts(struct socket *sock)
  1552. {
  1553. dlm_tcp_sockopts(sock);
  1554. sock_set_reuseaddr(sock->sk);
  1555. }
  1556. static int dlm_tcp_listen_bind(struct socket *sock)
  1557. {
  1558. int addr_len;
  1559. /* Bind to our port */
  1560. make_sockaddr(&dlm_local_addr[0], dlm_config.ci_tcp_port, &addr_len);
  1561. return kernel_bind(sock, (struct sockaddr *)&dlm_local_addr[0],
  1562. addr_len);
  1563. }
  1564. static const struct dlm_proto_ops dlm_tcp_ops = {
  1565. .name = "TCP",
  1566. .proto = IPPROTO_TCP,
  1567. .sockopts = dlm_tcp_sockopts,
  1568. .bind = dlm_tcp_bind,
  1569. .listen_validate = dlm_tcp_listen_validate,
  1570. .listen_sockopts = dlm_tcp_listen_sockopts,
  1571. .listen_bind = dlm_tcp_listen_bind,
  1572. };
  1573. static int dlm_sctp_bind(struct socket *sock)
  1574. {
  1575. return sctp_bind_addrs(sock, 0);
  1576. }
  1577. static int dlm_sctp_listen_validate(void)
  1578. {
  1579. if (!IS_ENABLED(CONFIG_IP_SCTP)) {
  1580. log_print("SCTP is not enabled by this kernel");
  1581. return -EOPNOTSUPP;
  1582. }
  1583. request_module("sctp");
  1584. return 0;
  1585. }
  1586. static int dlm_sctp_bind_listen(struct socket *sock)
  1587. {
  1588. return sctp_bind_addrs(sock, dlm_config.ci_tcp_port);
  1589. }
  1590. static void dlm_sctp_sockopts(struct socket *sock)
  1591. {
  1592. /* Turn off Nagle's algorithm */
  1593. sctp_sock_set_nodelay(sock->sk);
  1594. sock_set_rcvbuf(sock->sk, NEEDED_RMEM);
  1595. }
  1596. static const struct dlm_proto_ops dlm_sctp_ops = {
  1597. .name = "SCTP",
  1598. .proto = IPPROTO_SCTP,
  1599. .try_new_addr = true,
  1600. .sockopts = dlm_sctp_sockopts,
  1601. .bind = dlm_sctp_bind,
  1602. .listen_validate = dlm_sctp_listen_validate,
  1603. .listen_sockopts = dlm_sctp_sockopts,
  1604. .listen_bind = dlm_sctp_bind_listen,
  1605. };
  1606. int dlm_lowcomms_start(void)
  1607. {
  1608. int error;
  1609. init_local();
  1610. if (!dlm_local_count) {
  1611. error = -ENOTCONN;
  1612. log_print("no local IP address has been set");
  1613. goto fail;
  1614. }
  1615. error = work_start();
  1616. if (error)
  1617. goto fail;
  1618. /* Start listening */
  1619. switch (dlm_config.ci_protocol) {
  1620. case DLM_PROTO_TCP:
  1621. dlm_proto_ops = &dlm_tcp_ops;
  1622. break;
  1623. case DLM_PROTO_SCTP:
  1624. dlm_proto_ops = &dlm_sctp_ops;
  1625. break;
  1626. default:
  1627. log_print("Invalid protocol identifier %d set",
  1628. dlm_config.ci_protocol);
  1629. error = -EINVAL;
  1630. goto fail_proto_ops;
  1631. }
  1632. error = dlm_listen_for_all();
  1633. if (error)
  1634. goto fail_listen;
  1635. return 0;
  1636. fail_listen:
  1637. dlm_proto_ops = NULL;
  1638. fail_proto_ops:
  1639. work_stop();
  1640. fail:
  1641. return error;
  1642. }
  1643. void dlm_lowcomms_init(void)
  1644. {
  1645. int i;
  1646. for (i = 0; i < CONN_HASH_SIZE; i++)
  1647. INIT_HLIST_HEAD(&connection_hash[i]);
  1648. INIT_WORK(&listen_con.rwork, process_listen_recv_socket);
  1649. }
  1650. void dlm_lowcomms_exit(void)
  1651. {
  1652. struct connection *con;
  1653. int i, idx;
  1654. idx = srcu_read_lock(&connections_srcu);
  1655. for (i = 0; i < CONN_HASH_SIZE; i++) {
  1656. hlist_for_each_entry_rcu(con, &connection_hash[i], list) {
  1657. spin_lock(&connections_lock);
  1658. hlist_del_rcu(&con->list);
  1659. spin_unlock(&connections_lock);
  1660. if (con->othercon)
  1661. call_srcu(&connections_srcu, &con->othercon->rcu,
  1662. connection_release);
  1663. call_srcu(&connections_srcu, &con->rcu, connection_release);
  1664. }
  1665. }
  1666. srcu_read_unlock(&connections_srcu, idx);
  1667. }