totemknet.c 60 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121
  1. /*
  2. * Copyright (c) 2016-2020 Red Hat, Inc.
  3. *
  4. * All rights reserved.
  5. *
  6. * Author: Christine Caulfield (ccaulfie@redhat.com)
  7. * This software licensed under BSD license, the text of which follows:
  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. * - Redistributions of source code must retain the above copyright notice,
  13. * this list of conditions and the following disclaimer.
  14. * - Redistributions in binary form must reproduce the above copyright notice,
  15. * this list of conditions and the following disclaimer in the documentation
  16. * and/or other materials provided with the distribution.
  17. * - Neither the name of the MontaVista Software, Inc. nor the names of its
  18. * contributors may be used to endorse or promote products derived from this
  19. * software without specific prior written permission.
  20. *
  21. * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  22. * AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  23. * IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  24. * ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  25. * LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  26. * CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  27. * SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  28. * INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  29. * CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  30. * ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF
  31. * THE POSSIBILITY OF SUCH DAMAGE.
  32. */
  33. #include <config.h>
  34. #include <assert.h>
  35. #include <sys/mman.h>
  36. #include <sys/types.h>
  37. #include <sys/stat.h>
  38. #include <sys/socket.h>
  39. #include <netdb.h>
  40. #include <sys/un.h>
  41. #include <sys/ioctl.h>
  42. #include <sys/param.h>
  43. #include <netinet/in.h>
  44. #include <net/ethernet.h>
  45. #include <arpa/inet.h>
  46. #include <unistd.h>
  47. #include <fcntl.h>
  48. #include <stdlib.h>
  49. #include <stdio.h>
  50. #include <errno.h>
  51. #include <pthread.h>
  52. #include <sched.h>
  53. #include <time.h>
  54. #include <sys/time.h>
  55. #include <sys/poll.h>
  56. #include <sys/uio.h>
  57. #include <limits.h>
  58. #include <qb/qbdefs.h>
  59. #include <qb/qbloop.h>
  60. #ifdef HAVE_LIBNOZZLE
  61. #include <libgen.h>
  62. #include <libnozzle.h>
  63. #endif
  64. #include <corosync/sq.h>
  65. #include <corosync/swab.h>
  66. #include <corosync/logsys.h>
  67. #include <corosync/icmap.h>
  68. #include <corosync/totem/totemip.h>
  69. #include "totemknet.h"
  70. #include "main.h"
  71. #include "util.h"
  72. #include <libknet.h>
  73. #include <corosync/totem/totemstats.h>
  74. #ifndef MSG_NOSIGNAL
  75. #define MSG_NOSIGNAL 0
  76. #endif
  77. #ifdef HAVE_LIBNOZZLE
  78. static int setup_nozzle(void *knet_context);
  79. #endif
  80. /* Should match that used by cfg */
  81. #define CFG_INTERFACE_STATUS_MAX_LEN 512
  82. struct totemknet_instance {
  83. struct crypto_instance *crypto_inst;
  84. qb_loop_t *poll_handle;
  85. knet_handle_t knet_handle;
  86. int link_mode;
  87. void *context;
  88. void (*totemknet_deliver_fn) (
  89. void *context,
  90. const void *msg,
  91. unsigned int msg_len,
  92. const struct sockaddr_storage *system_from);
  93. void (*totemknet_iface_change_fn) (
  94. void *context,
  95. const struct totem_ip_address *iface_address,
  96. unsigned int link_no);
  97. void (*totemknet_mtu_changed) (
  98. void *context,
  99. int net_mtu);
  100. void (*totemknet_target_set_completed) (void *context);
  101. /*
  102. * Function and data used to log messages
  103. */
  104. int totemknet_log_level_security;
  105. int totemknet_log_level_error;
  106. int totemknet_log_level_warning;
  107. int totemknet_log_level_notice;
  108. int totemknet_log_level_debug;
  109. int totemknet_subsys_id;
  110. int knet_subsys_id;
  111. void (*totemknet_log_printf) (
  112. int level,
  113. int subsys,
  114. const char *function,
  115. const char *file,
  116. int line,
  117. const char *format,
  118. ...)__attribute__((format(printf, 6, 7)));
  119. void *knet_context;
  120. char iov_buffer[KNET_MAX_PACKET_SIZE];
  121. char *link_status[INTERFACE_MAX];
  122. struct totem_ip_address my_ids[INTERFACE_MAX];
  123. uint16_t ip_port[INTERFACE_MAX];
  124. int our_nodeid;
  125. int loopback_link;
  126. struct totem_config *totem_config;
  127. struct totem_ip_address token_target;
  128. qb_loop_timer_handle timer_netif_check_timeout;
  129. qb_loop_timer_handle timer_merge_detect_timeout;
  130. int send_merge_detect_message;
  131. unsigned int merge_detect_messages_sent_before_timeout;
  132. int logpipes[2];
  133. int knet_fd;
  134. pthread_mutex_t log_mutex;
  135. #ifdef HAVE_LIBNOZZLE
  136. char *nozzle_name;
  137. char *nozzle_ipaddr;
  138. char *nozzle_prefix;
  139. char *nozzle_macaddr;
  140. nozzle_t nozzle_handle;
  141. #endif
  142. };
  143. /* Awkward. But needed to get stats from knet */
  144. struct totemknet_instance *global_instance;
  145. struct work_item {
  146. const void *msg;
  147. unsigned int msg_len;
  148. struct totemknet_instance *instance;
  149. };
  150. int totemknet_member_list_rebind_ip (
  151. void *knet_context);
  152. static int totemknet_configure_compression (
  153. void *knet_context,
  154. struct totem_config *totem_config);
  155. static void totemknet_start_merge_detect_timeout(
  156. void *knet_context);
  157. static void totemknet_stop_merge_detect_timeout(
  158. void *knet_context);
  159. static void log_flush_messages (
  160. void *knet_context);
  161. static void totemknet_instance_initialize (struct totemknet_instance *instance)
  162. {
  163. int res;
  164. memset (instance, 0, sizeof (struct totemknet_instance));
  165. res = pthread_mutex_init(&instance->log_mutex, NULL);
  166. /*
  167. * There is not too much else what can be done.
  168. */
  169. assert(res == 0);
  170. }
  171. #define knet_log_printf_lock(level, subsys, function, file, line, format, args...) \
  172. do { \
  173. (void)pthread_mutex_lock(&instance->log_mutex); \
  174. instance->totemknet_log_printf ( \
  175. level, subsys, function, file, line, \
  176. (const char *)format, ##args); \
  177. (void)pthread_mutex_unlock(&instance->log_mutex); \
  178. } while (0);
  179. #define knet_log_printf(level, format, args...) \
  180. do { \
  181. knet_log_printf_lock ( \
  182. level, instance->totemknet_subsys_id, \
  183. __FUNCTION__, __FILE__, __LINE__, \
  184. (const char *)format, ##args); \
  185. } while (0);
  186. #define libknet_log_printf(level, format, args...) \
  187. do { \
  188. knet_log_printf_lock ( \
  189. level, instance->knet_subsys_id, \
  190. __FUNCTION__, "libknet.h", __LINE__, \
  191. (const char *)format, ##args); \
  192. } while (0);
  193. #define KNET_LOGSYS_PERROR(err_num, level, fmt, args...) \
  194. do { \
  195. char _error_str[LOGSYS_MAX_PERROR_MSG_LEN]; \
  196. const char *_error_ptr = qb_strerror_r(err_num, _error_str, sizeof(_error_str)); \
  197. instance->totemknet_log_printf ( \
  198. level, instance->totemknet_subsys_id, \
  199. __FUNCTION__, __FILE__, __LINE__, \
  200. fmt ": %s (%d)", ##args, _error_ptr, err_num); \
  201. } while(0)
  202. #ifdef HAVE_LIBNOZZLE
  203. static inline int is_ether_addr_multicast(const uint8_t *addr)
  204. {
  205. return (addr[0] & 0x01);
  206. }
  207. static inline int is_ether_addr_zero(const uint8_t *addr)
  208. {
  209. return (!addr[0] && !addr[1] && !addr[2] && !addr[3] && !addr[4] && !addr[5]);
  210. }
  211. static int ether_host_filter_fn(void *private_data,
  212. const unsigned char *outdata,
  213. ssize_t outdata_len,
  214. uint8_t tx_rx,
  215. knet_node_id_t this_host_id,
  216. knet_node_id_t src_host_id,
  217. int8_t *channel,
  218. knet_node_id_t *dst_host_ids,
  219. size_t *dst_host_ids_entries)
  220. {
  221. struct ether_header *eth_h = (struct ether_header *)outdata;
  222. uint8_t *dst_mac = (uint8_t *)eth_h->ether_dhost;
  223. uint16_t dst_host_id;
  224. if (is_ether_addr_zero(dst_mac))
  225. return -1;
  226. if (is_ether_addr_multicast(dst_mac)) {
  227. return 1;
  228. }
  229. memmove(&dst_host_id, &dst_mac[4], 2);
  230. dst_host_ids[0] = ntohs(dst_host_id);
  231. *dst_host_ids_entries = 1;
  232. return 0;
  233. }
  234. #endif
  235. static int dst_host_filter_callback_fn(void *private_data,
  236. const unsigned char *outdata,
  237. ssize_t outdata_len,
  238. uint8_t tx_rx,
  239. knet_node_id_t this_host_id,
  240. knet_node_id_t src_host_id,
  241. int8_t *channel,
  242. knet_node_id_t *dst_host_ids,
  243. size_t *dst_host_ids_entries)
  244. {
  245. struct totem_message_header *header = (struct totem_message_header *)outdata;
  246. int res;
  247. #ifdef HAVE_LIBNOZZLE
  248. if (*channel != 0) {
  249. return ether_host_filter_fn(private_data,
  250. outdata, outdata_len,
  251. tx_rx,
  252. this_host_id, src_host_id,
  253. channel,
  254. dst_host_ids,
  255. dst_host_ids_entries);
  256. }
  257. #endif
  258. if (header->target_nodeid) {
  259. dst_host_ids[0] = header->target_nodeid;
  260. *dst_host_ids_entries = 1;
  261. res = 0; /* unicast message */
  262. }
  263. else {
  264. *dst_host_ids_entries = 0;
  265. res = 1; /* multicast message */
  266. }
  267. return res;
  268. }
  269. static void socket_error_callback_fn(void *private_data, int datafd, int8_t channel, uint8_t tx_rx, int error, int errorno)
  270. {
  271. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  272. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet socket ERROR notification called: txrx=%d, error=%d, errorno=%d", tx_rx, error, errorno);
  273. if ((error == -1 && errorno != EAGAIN) || (error == 0)) {
  274. knet_handle_remove_datafd(instance->knet_handle, datafd);
  275. }
  276. }
  277. static void host_change_callback_fn(void *private_data, knet_node_id_t host_id, uint8_t reachable, uint8_t remote, uint8_t external)
  278. {
  279. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  280. // TODO: what? if anything.
  281. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet host change callback. nodeid: " CS_PRI_NODE_ID " reachable: %d", host_id, reachable);
  282. }
  283. static void pmtu_change_callback_fn(void *private_data, unsigned int data_mtu)
  284. {
  285. struct totemknet_instance *instance = (struct totemknet_instance *)private_data;
  286. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Knet pMTU change: %d", data_mtu);
  287. /* We don't need to tell corosync the actual knet MTU */
  288. // instance->totemknet_mtu_changed(instance->context, data_mtu);
  289. }
  290. int totemknet_crypto_set (
  291. void *knet_context,
  292. const char *cipher_type,
  293. const char *hash_type)
  294. {
  295. return (0);
  296. }
  297. static inline void ucast_sendmsg (
  298. struct totemknet_instance *instance,
  299. struct totem_ip_address *system_to,
  300. const void *msg,
  301. unsigned int msg_len)
  302. {
  303. int res = 0;
  304. struct totem_message_header *header = (struct totem_message_header *)msg;
  305. struct msghdr msg_ucast;
  306. struct iovec iovec;
  307. header->target_nodeid = system_to->nodeid;
  308. iovec.iov_base = (void *)msg;
  309. iovec.iov_len = msg_len;
  310. /*
  311. * Build unicast message
  312. */
  313. memset(&msg_ucast, 0, sizeof(msg_ucast));
  314. msg_ucast.msg_iov = (void *)&iovec;
  315. msg_ucast.msg_iovlen = 1;
  316. #ifdef HAVE_MSGHDR_CONTROL
  317. msg_ucast.msg_control = 0;
  318. #endif
  319. #ifdef HAVE_MSGHDR_CONTROLLEN
  320. msg_ucast.msg_controllen = 0;
  321. #endif
  322. #ifdef HAVE_MSGHDR_FLAGS
  323. msg_ucast.msg_flags = 0;
  324. #endif
  325. #ifdef HAVE_MSGHDR_ACCRIGHTS
  326. msg_ucast.msg_accrights = NULL;
  327. #endif
  328. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  329. msg_ucast.msg_accrightslen = 0;
  330. #endif
  331. /*
  332. * Transmit unicast message
  333. * An error here is recovered by totemsrp
  334. */
  335. res = sendmsg (instance->knet_fd, &msg_ucast, MSG_NOSIGNAL);
  336. if (res < 0) {
  337. KNET_LOGSYS_PERROR (errno, instance->totemknet_log_level_debug,
  338. "sendmsg(ucast) failed (non-critical)");
  339. }
  340. }
  341. static inline void mcast_sendmsg (
  342. struct totemknet_instance *instance,
  343. const void *msg,
  344. unsigned int msg_len,
  345. int only_active)
  346. {
  347. int res;
  348. struct totem_message_header *header = (struct totem_message_header *)msg;
  349. struct msghdr msg_mcast;
  350. struct iovec iovec;
  351. iovec.iov_base = (void *)msg;
  352. iovec.iov_len = msg_len;
  353. header->target_nodeid = 0;
  354. /*
  355. * Build multicast message
  356. */
  357. memset(&msg_mcast, 0, sizeof(msg_mcast));
  358. msg_mcast.msg_iov = (void *)&iovec;
  359. msg_mcast.msg_iovlen = 1;
  360. #ifdef HAVE_MSGHDR_CONTROL
  361. msg_mcast.msg_control = 0;
  362. #endif
  363. #ifdef HAVE_MSGHDR_CONTROLLEN
  364. msg_mcast.msg_controllen = 0;
  365. #endif
  366. #ifdef HAVE_MSGHDR_FLAGS
  367. msg_mcast.msg_flags = 0;
  368. #endif
  369. #ifdef HAVE_MSGHDR_ACCRIGHTS
  370. msg_mcast.msg_accrights = NULL;
  371. #endif
  372. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  373. msg_mcast.msg_accrightslen = 0;
  374. #endif
  375. // log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_sendmsg. only_active=%d, len=%d", only_active, msg_len);
  376. res = sendmsg (instance->knet_fd, &msg_mcast, MSG_NOSIGNAL);
  377. if (res < msg_len) {
  378. knet_log_printf (LOGSYS_LEVEL_DEBUG, "totemknet: mcast_send sendmsg returned %d", res);
  379. }
  380. if (!only_active || instance->send_merge_detect_message) {
  381. /*
  382. * Current message was sent to all nodes
  383. */
  384. instance->merge_detect_messages_sent_before_timeout++;
  385. instance->send_merge_detect_message = 0;
  386. }
  387. }
  388. static int node_compare(const void *aptr, const void *bptr)
  389. {
  390. uint16_t a,b;
  391. a = *(uint16_t *)aptr;
  392. b = *(uint16_t *)bptr;
  393. return a > b;
  394. }
  395. #ifndef OWN_INDEX_NONE
  396. #define OWN_INDEX_NONE -1
  397. #endif
  398. int totemknet_ifaces_get (void *knet_context,
  399. char ***status,
  400. unsigned int *iface_count)
  401. {
  402. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  403. struct knet_link_status link_status;
  404. knet_node_id_t host_list[KNET_MAX_HOST];
  405. uint8_t link_list[KNET_MAX_LINK];
  406. size_t num_hosts;
  407. size_t num_links;
  408. size_t link_idx;
  409. int i,j;
  410. char *ptr;
  411. int res = 0;
  412. /*
  413. * Don't do the whole 'link_info' bit if the caller just wants
  414. * a count of interfaces.
  415. */
  416. if (status) {
  417. int own_idx = OWN_INDEX_NONE;
  418. res = knet_host_get_host_list(instance->knet_handle,
  419. host_list, &num_hosts);
  420. if (res) {
  421. return (-1);
  422. }
  423. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  424. for (j=0; j<num_hosts; j++) {
  425. if (host_list[j] == instance->our_nodeid) {
  426. own_idx = j;
  427. break;
  428. }
  429. }
  430. for (i=0; i<INTERFACE_MAX; i++) {
  431. memset(instance->link_status[i], 'd', CFG_INTERFACE_STATUS_MAX_LEN-1);
  432. if (own_idx != OWN_INDEX_NONE) {
  433. instance->link_status[i][own_idx] = 'n';
  434. }
  435. instance->link_status[i][num_hosts] = '\0';
  436. }
  437. /* This is all a bit "inside-out" because "status" is a set of strings per link
  438. * and knet orders things by host
  439. */
  440. for (j=0; j<num_hosts; j++) {
  441. if (own_idx != OWN_INDEX_NONE && j == own_idx) {
  442. continue ;
  443. }
  444. res = knet_link_get_link_list(instance->knet_handle,
  445. host_list[j], link_list, &num_links);
  446. if (res) {
  447. return (-1);
  448. }
  449. link_idx = 0;
  450. for (i=0; i < num_links; i++) {
  451. /*
  452. * Skip over links that are unconfigured to corosync. This is basically
  453. * link0 if corosync isn't using it for comms, as we will still
  454. * have it set up for loopback.
  455. */
  456. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  457. continue;
  458. }
  459. ptr = instance->link_status[link_idx++];
  460. res = knet_link_get_status(instance->knet_handle,
  461. host_list[j],
  462. link_list[i],
  463. &link_status,
  464. sizeof(link_status));
  465. if (res == 0) {
  466. ptr[j] = '0' + (link_status.enabled |
  467. link_status.connected<<1 |
  468. link_status.dynconnected<<2);
  469. }
  470. else {
  471. knet_log_printf (LOGSYS_LEVEL_ERROR,
  472. "totemknet_ifaces_get: Cannot get link status: %s", strerror(errno));
  473. ptr[j] = '?';
  474. }
  475. }
  476. }
  477. *status = instance->link_status;
  478. }
  479. *iface_count = INTERFACE_MAX;
  480. return (res);
  481. }
  482. int totemknet_finalize (
  483. void *knet_context)
  484. {
  485. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  486. int res = 0;
  487. int i,j;
  488. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  489. uint8_t links[KNET_MAX_LINK];
  490. size_t num_nodes;
  491. size_t num_links;
  492. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  493. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  494. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  495. /*
  496. * Disable forwarding to make knet flush send queue. This ensures that the LEAVE message will be sent.
  497. */
  498. res = knet_handle_setfwd(instance->knet_handle, 0);
  499. if (res) {
  500. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  501. }
  502. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  503. if (res) {
  504. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  505. /* Crash out anyway */
  506. goto finalise_error;
  507. }
  508. /* Tidily shut down all nodes & links. */
  509. for (i=0; i<num_nodes; i++) {
  510. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  511. if (res) {
  512. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node " CS_PRI_NODE_ID ": %s", nodes[i], strerror(errno));
  513. goto finalise_error;
  514. }
  515. for (j=0; j<num_links; j++) {
  516. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  517. if (res) {
  518. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_set_enable(node " CS_PRI_NODE_ID ", link %d) failed: %s", nodes[i], links[j], strerror(errno));
  519. }
  520. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  521. if (res) {
  522. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_link_clear_config(node " CS_PRI_NODE_ID ", link %d) failed: %s", nodes[i], links[j], strerror(errno));
  523. }
  524. }
  525. res = knet_host_remove(instance->knet_handle, nodes[i]);
  526. if (res) {
  527. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node " CS_PRI_NODE_ID ") failed: %s", nodes[i], strerror(errno));
  528. }
  529. }
  530. finalise_error:
  531. res = knet_handle_free(instance->knet_handle);
  532. if (res) {
  533. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  534. }
  535. totemknet_stop_merge_detect_timeout(instance);
  536. log_flush_messages(instance);
  537. /*
  538. * Error is deliberately ignored
  539. */
  540. (void)pthread_mutex_destroy(&instance->log_mutex);
  541. return (res);
  542. }
  543. static int log_deliver_fn (
  544. int fd,
  545. int revents,
  546. void *data)
  547. {
  548. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  549. char buffer[sizeof(struct knet_log_msg)*4];
  550. char *bufptr = buffer;
  551. int done = 0;
  552. int len;
  553. len = read(fd, buffer, sizeof(buffer));
  554. while (done < len) {
  555. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  556. switch (msg->msglevel) {
  557. case KNET_LOG_ERR:
  558. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  559. knet_log_get_subsystem_name(msg->subsystem),
  560. msg->msg);
  561. break;
  562. case KNET_LOG_WARN:
  563. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  564. knet_log_get_subsystem_name(msg->subsystem),
  565. msg->msg);
  566. break;
  567. case KNET_LOG_INFO:
  568. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  569. knet_log_get_subsystem_name(msg->subsystem),
  570. msg->msg);
  571. break;
  572. case KNET_LOG_DEBUG:
  573. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  574. knet_log_get_subsystem_name(msg->subsystem),
  575. msg->msg);
  576. break;
  577. }
  578. bufptr += sizeof(struct knet_log_msg);
  579. done += sizeof(struct knet_log_msg);
  580. }
  581. return 0;
  582. }
  583. static int data_deliver_fn (
  584. int fd,
  585. int revents,
  586. void *data)
  587. {
  588. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  589. struct msghdr msg_hdr;
  590. struct iovec iov_recv;
  591. struct sockaddr_storage system_from;
  592. ssize_t msg_len;
  593. int truncated_packet;
  594. iov_recv.iov_base = instance->iov_buffer;
  595. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  596. msg_hdr.msg_name = &system_from;
  597. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  598. msg_hdr.msg_iov = &iov_recv;
  599. msg_hdr.msg_iovlen = 1;
  600. #ifdef HAVE_MSGHDR_CONTROL
  601. msg_hdr.msg_control = 0;
  602. #endif
  603. #ifdef HAVE_MSGHDR_CONTROLLEN
  604. msg_hdr.msg_controllen = 0;
  605. #endif
  606. #ifdef HAVE_MSGHDR_FLAGS
  607. msg_hdr.msg_flags = 0;
  608. #endif
  609. #ifdef HAVE_MSGHDR_ACCRIGHTS
  610. msg_hdr.msg_accrights = NULL;
  611. #endif
  612. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  613. msg_hdr.msg_accrightslen = 0;
  614. #endif
  615. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  616. if (msg_len <= 0) {
  617. return (0);
  618. }
  619. truncated_packet = 0;
  620. #ifdef HAVE_MSGHDR_FLAGS
  621. if (msg_hdr.msg_flags & MSG_TRUNC) {
  622. truncated_packet = 1;
  623. }
  624. #else
  625. /*
  626. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  627. * if bytes_received == KNET_MAX_PACKET_SIZE then packet is truncated
  628. */
  629. if (bytes_received == KNET_MAX_PACKET_SIZE) {
  630. truncated_packet = 1;
  631. }
  632. #endif
  633. if (truncated_packet) {
  634. knet_log_printf(instance->totemknet_log_level_error,
  635. "Received too big message. This may be because something bad is happening"
  636. "on the network (attack?), or you tried join more nodes than corosync is"
  637. "compiled with (%u) or bug in the code (bad estimation of "
  638. "the KNET_MAX_PACKET_SIZE). Dropping packet.", PROCESSOR_COUNT_MAX);
  639. return (0);
  640. }
  641. /*
  642. * Handle incoming message
  643. */
  644. instance->totemknet_deliver_fn (
  645. instance->context,
  646. instance->iov_buffer,
  647. msg_len,
  648. &system_from);
  649. return (0);
  650. }
  651. static void timer_function_netif_check_timeout (
  652. void *data)
  653. {
  654. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  655. int i;
  656. for (i=0; i < INTERFACE_MAX; i++) {
  657. if (!instance->totem_config->interfaces[i].configured) {
  658. continue;
  659. }
  660. instance->totemknet_iface_change_fn (instance->context,
  661. &instance->my_ids[i],
  662. i);
  663. }
  664. }
  665. static void knet_set_access_list_config(struct totemknet_instance *instance)
  666. {
  667. #ifdef HAVE_KNET_ACCESS_LIST
  668. uint32_t value;
  669. cs_error_t err;
  670. value = instance->totem_config->block_unlisted_ips;
  671. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_enable access list: %d", value);
  672. err = knet_handle_enable_access_lists(instance->knet_handle, value);
  673. if (err) {
  674. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_access_lists failed");
  675. }
  676. #endif
  677. }
  678. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  679. static void totemknet_refresh_config(
  680. int32_t event,
  681. const char *key_name,
  682. struct icmap_notify_value new_val,
  683. struct icmap_notify_value old_val,
  684. void *user_data)
  685. {
  686. uint8_t reloading;
  687. uint32_t value;
  688. uint32_t link_no;
  689. size_t num_nodes;
  690. knet_node_id_t host_ids[KNET_MAX_HOST];
  691. int i;
  692. int err;
  693. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  694. ENTER();
  695. /*
  696. * If a full reload is in progress then don't do anything until it's done and
  697. * can reconfigure it all atomically
  698. */
  699. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  700. return;
  701. }
  702. knet_set_access_list_config(instance);
  703. if (icmap_get_uint32("totem.knet_pmtud_interval", &value) == CS_OK) {
  704. instance->totem_config->knet_pmtud_interval = value;
  705. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %d", value);
  706. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  707. if (err) {
  708. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  709. }
  710. }
  711. /* Configure link parameters for each node */
  712. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  713. if (err != 0) {
  714. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  715. }
  716. for (i=0; i<num_nodes; i++) {
  717. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  718. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  719. continue;
  720. }
  721. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  722. instance->totem_config->interfaces[link_no].knet_ping_interval,
  723. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  724. instance->totem_config->interfaces[link_no].knet_ping_precision);
  725. if (err) {
  726. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for node " CS_PRI_NODE_ID " link %d failed", host_ids[i], link_no);
  727. }
  728. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  729. instance->totem_config->interfaces[link_no].knet_pong_count);
  730. if (err) {
  731. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for node " CS_PRI_NODE_ID " link %d failed",host_ids[i], link_no);
  732. }
  733. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  734. instance->totem_config->interfaces[link_no].knet_link_priority);
  735. if (err) {
  736. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for node " CS_PRI_NODE_ID " link %d failed", host_ids[i], link_no);
  737. }
  738. }
  739. }
  740. LEAVE();
  741. }
  742. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  743. {
  744. icmap_track_t icmap_track_totem = NULL;
  745. icmap_track_t icmap_track_reload = NULL;
  746. ENTER();
  747. icmap_track_add("totem.",
  748. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  749. totemknet_refresh_config,
  750. instance,
  751. &icmap_track_totem);
  752. icmap_track_add("config.totemconfig_reload_in_progress",
  753. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  754. totemknet_refresh_config,
  755. instance,
  756. &icmap_track_reload);
  757. LEAVE();
  758. }
  759. static int totemknet_set_knet_crypto(struct totemknet_instance *instance)
  760. {
  761. struct knet_handle_crypto_cfg crypto_cfg;
  762. int res;
  763. /* These have already been validated */
  764. memcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model, sizeof(crypto_cfg.crypto_model));
  765. memcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type, sizeof(crypto_cfg.crypto_model));
  766. memcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type, sizeof(crypto_cfg.crypto_model));
  767. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  768. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  769. #ifdef HAVE_KNET_CRYPTO_RECONF
  770. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s on index %d",
  771. crypto_cfg.crypto_model,
  772. crypto_cfg.crypto_cipher_type,
  773. crypto_cfg.crypto_hash_type,
  774. instance->totem_config->crypto_index
  775. );
  776. /* If crypto is being disabled we need to explicitly allow cleartext traffic in knet */
  777. if (strcmp(instance->totem_config->crypto_cipher_type, "none") == 0) {
  778. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  779. if (res) {
  780. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(ALLOW) failed %s", strerror(errno));
  781. }
  782. }
  783. /* use_config will be called later when all nodes are synced */
  784. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, instance->totem_config->crypto_index);
  785. if (res == -1) {
  786. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: %s", instance->totem_config->crypto_index, strerror(errno));
  787. goto exit_error;
  788. }
  789. if (res == -2) {
  790. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: -2", instance->totem_config->crypto_index);
  791. goto exit_error;
  792. }
  793. #else
  794. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s",
  795. crypto_cfg.crypto_model,
  796. crypto_cfg.crypto_cipher_type,
  797. crypto_cfg.crypto_hash_type
  798. );
  799. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  800. if (res == -1) {
  801. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  802. goto exit_error;
  803. }
  804. if (res == -2) {
  805. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  806. goto exit_error;
  807. }
  808. #endif
  809. exit_error:
  810. return res;
  811. }
  812. /*
  813. * Create an instance
  814. */
  815. int totemknet_initialize (
  816. qb_loop_t *poll_handle,
  817. void **knet_context,
  818. struct totem_config *totem_config,
  819. totemsrp_stats_t *stats,
  820. void *context,
  821. void (*deliver_fn) (
  822. void *context,
  823. const void *msg,
  824. unsigned int msg_len,
  825. const struct sockaddr_storage *system_from),
  826. void (*iface_change_fn) (
  827. void *context,
  828. const struct totem_ip_address *iface_address,
  829. unsigned int link_no),
  830. void (*mtu_changed) (
  831. void *context,
  832. int net_mtu),
  833. void (*target_set_completed) (
  834. void *context))
  835. {
  836. struct totemknet_instance *instance;
  837. int8_t channel=0;
  838. int res;
  839. int i;
  840. instance = malloc (sizeof (struct totemknet_instance));
  841. if (instance == NULL) {
  842. return (-1);
  843. }
  844. totemknet_instance_initialize (instance);
  845. instance->totem_config = totem_config;
  846. /*
  847. * Configure logging
  848. */
  849. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  850. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  851. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  852. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  853. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  854. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  855. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  856. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  857. /*
  858. * Initialize local variables for totemknet
  859. */
  860. instance->our_nodeid = instance->totem_config->node_id;
  861. for (i=0; i< INTERFACE_MAX; i++) {
  862. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  863. instance->my_ids[i].nodeid = instance->our_nodeid;
  864. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  865. /* Needed for totemsrp */
  866. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  867. }
  868. instance->poll_handle = poll_handle;
  869. instance->context = context;
  870. instance->totemknet_deliver_fn = deliver_fn;
  871. instance->totemknet_iface_change_fn = iface_change_fn;
  872. instance->totemknet_mtu_changed = mtu_changed;
  873. instance->totemknet_target_set_completed = target_set_completed;
  874. instance->loopback_link = 0;
  875. res = pipe(instance->logpipes);
  876. if (res == -1) {
  877. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  878. goto exit_error;
  879. }
  880. if (fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK) == -1 ||
  881. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK) == -1) {
  882. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to set O_NONBLOCK flag for instance->logpipes");
  883. goto exit_error;
  884. }
  885. #if !defined(KNET_API_VER) || (KNET_API_VER == 1)
  886. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  887. #endif
  888. #if KNET_API_VER == 2
  889. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, KNET_HANDLE_FLAG_PRIVILEGED);
  890. #endif
  891. if (!instance->knet_handle) {
  892. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  893. goto exit_error;
  894. }
  895. knet_set_access_list_config(instance);
  896. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  897. if (res) {
  898. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  899. }
  900. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  901. if (res) {
  902. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  903. }
  904. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  905. if (res) {
  906. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  907. }
  908. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  909. if (res) {
  910. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  911. }
  912. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  913. if (res) {
  914. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  915. }
  916. global_instance = instance;
  917. /* Get an fd into knet */
  918. instance->knet_fd = 0;
  919. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  920. if (res) {
  921. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  922. goto exit_error;
  923. }
  924. /* Enable crypto if requested */
  925. #ifdef HAVE_KNET_CRYPTO_RECONF
  926. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  927. res = totemknet_set_knet_crypto(instance);
  928. if (res == 0) {
  929. res = knet_handle_crypto_use_config(instance->knet_handle, totem_config->crypto_index);
  930. if (res) {
  931. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_use_config failed: %s", strerror(errno));
  932. goto exit_error;
  933. }
  934. } else {
  935. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  936. goto exit_error;
  937. }
  938. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  939. if (res) {
  940. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (DISALLOW) failed: %s", strerror(errno));
  941. goto exit_error;
  942. }
  943. } else {
  944. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  945. if (res) {
  946. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (ALLOW) failed: %s", strerror(errno));
  947. goto exit_error;
  948. }
  949. }
  950. #else
  951. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  952. res = totemknet_set_knet_crypto(instance);
  953. if (res) {
  954. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  955. goto exit_error;
  956. }
  957. }
  958. #endif
  959. /* Set up compression */
  960. if (strcmp(totem_config->knet_compression_model, "none") != 0) {
  961. /* Not fatal, but will log */
  962. (void)totemknet_configure_compression(knet_context, totem_config);
  963. }
  964. knet_handle_setfwd(instance->knet_handle, 1);
  965. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  966. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  967. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  968. }
  969. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  970. instance->link_mode = KNET_LINK_POLICY_RR;
  971. }
  972. for (i=0; i<INTERFACE_MAX; i++) {
  973. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  974. if (!instance->link_status[i]) {
  975. goto exit_error;
  976. }
  977. }
  978. qb_loop_poll_add (instance->poll_handle,
  979. QB_LOOP_MED,
  980. instance->logpipes[0],
  981. POLLIN, instance, log_deliver_fn);
  982. qb_loop_poll_add (instance->poll_handle,
  983. QB_LOOP_HIGH,
  984. instance->knet_fd,
  985. POLLIN, instance, data_deliver_fn);
  986. /*
  987. * Upper layer isn't ready to receive message because it hasn't
  988. * initialized yet. Add short timer to check the interfaces.
  989. */
  990. qb_loop_timer_add (instance->poll_handle,
  991. QB_LOOP_MED,
  992. 100*QB_TIME_NS_IN_MSEC,
  993. (void *)instance,
  994. timer_function_netif_check_timeout,
  995. &instance->timer_netif_check_timeout);
  996. totemknet_start_merge_detect_timeout(instance);
  997. /* Start listening for config changes */
  998. totemknet_add_config_notifications(instance);
  999. /* Add stats keys to icmap */
  1000. stats_knet_add_handle();
  1001. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  1002. *knet_context = instance;
  1003. return (0);
  1004. exit_error:
  1005. log_flush_messages(instance);
  1006. free(instance);
  1007. return (-1);
  1008. }
  1009. void *totemknet_buffer_alloc (void)
  1010. {
  1011. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  1012. return malloc(KNET_MAX_PACKET_SIZE + 512);
  1013. }
  1014. void totemknet_buffer_release (void *ptr)
  1015. {
  1016. return free (ptr);
  1017. }
  1018. int totemknet_processor_count_set (
  1019. void *knet_context,
  1020. int processor_count)
  1021. {
  1022. return (0);
  1023. }
  1024. int totemknet_recv_flush (void *knet_context)
  1025. {
  1026. return (0);
  1027. }
  1028. int totemknet_send_flush (void *knet_context)
  1029. {
  1030. return (0);
  1031. }
  1032. int totemknet_token_send (
  1033. void *knet_context,
  1034. const void *msg,
  1035. unsigned int msg_len)
  1036. {
  1037. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1038. int res = 0;
  1039. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1040. return (res);
  1041. }
  1042. int totemknet_mcast_flush_send (
  1043. void *knet_context,
  1044. const void *msg,
  1045. unsigned int msg_len)
  1046. {
  1047. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1048. int res = 0;
  1049. mcast_sendmsg (instance, msg, msg_len, 0);
  1050. return (res);
  1051. }
  1052. int totemknet_mcast_noflush_send (
  1053. void *knet_context,
  1054. const void *msg,
  1055. unsigned int msg_len)
  1056. {
  1057. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1058. int res = 0;
  1059. mcast_sendmsg (instance, msg, msg_len, 1);
  1060. return (res);
  1061. }
  1062. extern int totemknet_iface_check (void *knet_context)
  1063. {
  1064. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1065. int res = 0;
  1066. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  1067. return (res);
  1068. }
  1069. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  1070. {
  1071. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1072. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  1073. }
  1074. int totemknet_token_target_set (
  1075. void *knet_context,
  1076. unsigned int nodeid)
  1077. {
  1078. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1079. int res = 0;
  1080. instance->token_target.nodeid = nodeid;
  1081. instance->totemknet_target_set_completed (instance->context);
  1082. return (res);
  1083. }
  1084. extern int totemknet_recv_mcast_empty (
  1085. void *knet_context)
  1086. {
  1087. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1088. unsigned int res;
  1089. struct sockaddr_storage system_from;
  1090. struct msghdr msg_hdr;
  1091. struct iovec iov_recv;
  1092. struct pollfd ufd;
  1093. int nfds;
  1094. int msg_processed = 0;
  1095. iov_recv.iov_base = instance->iov_buffer;
  1096. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  1097. msg_hdr.msg_name = &system_from;
  1098. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  1099. msg_hdr.msg_iov = &iov_recv;
  1100. msg_hdr.msg_iovlen = 1;
  1101. #ifdef HAVE_MSGHDR_CONTROL
  1102. msg_hdr.msg_control = 0;
  1103. #endif
  1104. #ifdef HAVE_MSGHDR_CONTROLLEN
  1105. msg_hdr.msg_controllen = 0;
  1106. #endif
  1107. #ifdef HAVE_MSGHDR_FLAGS
  1108. msg_hdr.msg_flags = 0;
  1109. #endif
  1110. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1111. msg_msg_hdr.msg_accrights = NULL;
  1112. #endif
  1113. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1114. msg_msg_hdr.msg_accrightslen = 0;
  1115. #endif
  1116. do {
  1117. ufd.fd = instance->knet_fd;
  1118. ufd.events = POLLIN;
  1119. nfds = poll (&ufd, 1, 0);
  1120. if (nfds == 1 && ufd.revents & POLLIN) {
  1121. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  1122. if (res != -1) {
  1123. msg_processed = 1;
  1124. } else {
  1125. msg_processed = -1;
  1126. }
  1127. }
  1128. } while (nfds == 1);
  1129. return (msg_processed);
  1130. }
  1131. int totemknet_iface_set (void *knet_context,
  1132. const struct totem_ip_address *local_addr,
  1133. unsigned short ip_port,
  1134. unsigned int iface_no)
  1135. {
  1136. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1137. totemip_copy(&instance->my_ids[iface_no], local_addr);
  1138. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  1139. instance->ip_port[iface_no] = ip_port;
  1140. return 0;
  1141. }
  1142. int totemknet_member_add (
  1143. void *knet_context,
  1144. const struct totem_ip_address *local,
  1145. const struct totem_ip_address *member,
  1146. int link_no)
  1147. {
  1148. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1149. int err;
  1150. int port = instance->ip_port[link_no];
  1151. struct sockaddr_storage remote_ss;
  1152. struct sockaddr_storage local_ss;
  1153. int addrlen;
  1154. int i;
  1155. int host_found = 0;
  1156. knet_node_id_t host_ids[KNET_MAX_HOST];
  1157. size_t num_host_ids;
  1158. /* Only create 1 loopback link and use link 0 */
  1159. if (member->nodeid == instance->our_nodeid) {
  1160. if (!instance->loopback_link) {
  1161. link_no = 0;
  1162. instance->loopback_link = 1;
  1163. } else {
  1164. /* Already done */
  1165. return 0;
  1166. }
  1167. }
  1168. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: " CS_PRI_NODE_ID " (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  1169. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: " CS_PRI_NODE_ID " (%s)", local->nodeid, totemip_print(local));
  1170. /* Only add the host if it doesn't already exist in knet */
  1171. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_host_ids);
  1172. if (err) {
  1173. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list");
  1174. return -1;
  1175. }
  1176. for (i=0; i<num_host_ids; i++) {
  1177. if (host_ids[i] == member->nodeid) {
  1178. host_found = 1;
  1179. }
  1180. }
  1181. if (!host_found) {
  1182. err = knet_host_add(instance->knet_handle, member->nodeid);
  1183. if (err != 0 && errno != EEXIST) {
  1184. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  1185. return -1;
  1186. }
  1187. } else {
  1188. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nodeid " CS_PRI_NODE_ID " already added", member->nodeid);
  1189. }
  1190. if (err == 0) {
  1191. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  1192. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  1193. return -1;
  1194. }
  1195. }
  1196. memset(&local_ss, 0, sizeof(local_ss));
  1197. memset(&remote_ss, 0, sizeof(remote_ss));
  1198. /* Casts to remove const */
  1199. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  1200. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  1201. if (member->nodeid == instance->our_nodeid) {
  1202. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  1203. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1204. KNET_TRANSPORT_LOOPBACK,
  1205. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1206. }
  1207. else {
  1208. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1209. instance->totem_config->interfaces[link_no].knet_transport,
  1210. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1211. }
  1212. if (err) {
  1213. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  1214. return -1;
  1215. }
  1216. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  1217. instance->totem_config->interfaces[link_no].knet_link_priority);
  1218. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  1219. instance->totem_config->interfaces[link_no].knet_link_priority);
  1220. if (err) {
  1221. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1222. }
  1223. /* ping timeouts maybe 0 here for a newly added interface so we leave this till later, it will
  1224. get done in totemknet_refresh_config */
  1225. if (instance->totem_config->interfaces[link_no].knet_ping_interval != 0) {
  1226. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  1227. instance->totem_config->interfaces[link_no].knet_ping_interval,
  1228. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  1229. instance->totem_config->interfaces[link_no].knet_ping_precision);
  1230. if (err) {
  1231. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_ping_timers for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1232. }
  1233. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1234. instance->totem_config->interfaces[link_no].knet_pong_count);
  1235. if (err) {
  1236. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_pong_count for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1237. }
  1238. }
  1239. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1240. if (err) {
  1241. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1242. return -1;
  1243. }
  1244. /* register stats */
  1245. stats_knet_add_member(member->nodeid, link_no);
  1246. return (0);
  1247. }
  1248. int totemknet_member_remove (
  1249. void *knet_context,
  1250. const struct totem_ip_address *token_target,
  1251. int link_no)
  1252. {
  1253. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1254. int res;
  1255. uint8_t link_list[KNET_MAX_LINK];
  1256. size_t num_links;
  1257. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: " CS_PRI_NODE_ID ", link=%d", token_target->nodeid, link_no);
  1258. /* Don't remove the link with the loopback on it until we shut down */
  1259. if (token_target->nodeid == instance->our_nodeid) {
  1260. return 0;
  1261. }
  1262. /* Tidy stats */
  1263. stats_knet_del_member(token_target->nodeid, link_no);
  1264. /* Remove the link first */
  1265. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1266. if (res != 0) {
  1267. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set enable(off) for nodeid " CS_PRI_NODE_ID ", link %d failed", token_target->nodeid, link_no);
  1268. return res;
  1269. }
  1270. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1271. if (res != 0) {
  1272. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_clear_config for nodeid " CS_PRI_NODE_ID ", link %d failed", token_target->nodeid, link_no);
  1273. return res;
  1274. }
  1275. /* If this is the last link, then remove the node */
  1276. res = knet_link_get_link_list(instance->knet_handle,
  1277. token_target->nodeid, link_list, &num_links);
  1278. if (res) {
  1279. return (0); /* not really failure */
  1280. }
  1281. if (num_links == 0) {
  1282. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1283. }
  1284. return res;
  1285. }
  1286. int totemknet_member_list_rebind_ip (
  1287. void *knet_context)
  1288. {
  1289. return (0);
  1290. }
  1291. static int totemknet_configure_compression (
  1292. void *knet_context,
  1293. struct totem_config *totem_config)
  1294. {
  1295. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1296. struct knet_handle_compress_cfg compress_cfg;
  1297. int res = 0;
  1298. assert(strlen(totem_config->knet_compression_model) < sizeof(compress_cfg.compress_model));
  1299. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1300. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1301. compress_cfg.compress_level = totem_config->knet_compression_level;
  1302. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1303. if (res) {
  1304. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1305. }
  1306. return res;
  1307. }
  1308. int totemknet_reconfigure (
  1309. void *knet_context,
  1310. struct totem_config *totem_config)
  1311. {
  1312. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1313. int res = 0;
  1314. (void)totemknet_configure_compression(knet_context, totem_config);
  1315. #ifdef HAVE_LIBNOZZLE
  1316. /* Set up nozzle device(s). Return code is ignored, because inability
  1317. * configure nozzle is not fatal problem, errors are logged and
  1318. * there is not much else we can do */
  1319. (void)setup_nozzle(instance);
  1320. #endif
  1321. if (totem_config->crypto_changed) {
  1322. /* Flip crypto_index */
  1323. totem_config->crypto_index = 3-totem_config->crypto_index;
  1324. res = totemknet_set_knet_crypto(instance);
  1325. knet_log_printf(LOG_INFO, "kronosnet crypto reconfigured on index %d: %s/%s/%s", totem_config->crypto_index,
  1326. totem_config->crypto_model,
  1327. totem_config->crypto_cipher_type,
  1328. totem_config->crypto_hash_type);
  1329. }
  1330. return (res);
  1331. }
  1332. int totemknet_crypto_reconfigure_phase (
  1333. void *knet_context,
  1334. struct totem_config *totem_config,
  1335. cfg_message_crypto_reconfig_phase_t phase)
  1336. {
  1337. #ifdef HAVE_KNET_CRYPTO_RECONF
  1338. int res;
  1339. int config_to_use;
  1340. int config_to_clear;
  1341. struct knet_handle_crypto_cfg crypto_cfg;
  1342. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1343. knet_log_printf(LOGSYS_LEVEL_DEBUG, "totemknet_crypto_reconfigure_phase %d, index=%d\n", phase, totem_config->crypto_index);
  1344. switch (phase) {
  1345. case CRYPTO_RECONFIG_PHASE_ACTIVATE:
  1346. config_to_use = totem_config->crypto_index;
  1347. if (strcmp(instance->totem_config->crypto_cipher_type, "none") == 0) {
  1348. config_to_use = 0; /* we are clearing it */
  1349. }
  1350. /* Enable the new config on this node */
  1351. res = knet_handle_crypto_use_config(instance->knet_handle, config_to_use);
  1352. if (res == -1) {
  1353. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_use_config %d failed: %s", config_to_use, strerror(errno));
  1354. }
  1355. break;
  1356. case CRYPTO_RECONFIG_PHASE_CLEANUP:
  1357. /*
  1358. * All nodes should now have the new config. clear the old one out
  1359. * OR disable crypto entirely if that's what the new config insists on.
  1360. */
  1361. config_to_clear = 3-totem_config->crypto_index;
  1362. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Clearing old knet crypto config %d\n", config_to_clear);
  1363. strcpy(crypto_cfg.crypto_model, "none");
  1364. strcpy(crypto_cfg.crypto_cipher_type, "none");
  1365. strcpy(crypto_cfg.crypto_hash_type, "none");
  1366. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, config_to_clear);
  1367. if (res == -1) {
  1368. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: %s", config_to_clear, strerror(errno));
  1369. }
  1370. if (res == -2) {
  1371. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: -2", config_to_clear);
  1372. }
  1373. /* If crypto is enabled then disable all cleartext reception */
  1374. if (strcmp(instance->totem_config->crypto_cipher_type, "none") != 0) {
  1375. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1376. if (res) {
  1377. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(DISALLOW) failed %s", strerror(errno));
  1378. }
  1379. }
  1380. }
  1381. #endif
  1382. return 0;
  1383. }
  1384. void totemknet_stats_clear (
  1385. void *knet_context)
  1386. {
  1387. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1388. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1389. }
  1390. /* For the stats module */
  1391. int totemknet_link_get_status (
  1392. knet_node_id_t node, uint8_t link_no,
  1393. struct knet_link_status *status)
  1394. {
  1395. int res;
  1396. int ret = CS_OK;
  1397. /* We are probably not using knet */
  1398. if (!global_instance) {
  1399. return CS_ERR_NOT_EXIST;
  1400. }
  1401. if (link_no >= INTERFACE_MAX) {
  1402. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1403. }
  1404. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1405. if (res) {
  1406. switch (errno) {
  1407. case EINVAL:
  1408. ret = CS_ERR_INVALID_PARAM;
  1409. break;
  1410. case EBUSY:
  1411. ret = CS_ERR_BUSY;
  1412. break;
  1413. case EDEADLK:
  1414. ret = CS_ERR_TRY_AGAIN;
  1415. break;
  1416. default:
  1417. ret = CS_ERR_LIBRARY;
  1418. break;
  1419. }
  1420. }
  1421. return (ret);
  1422. }
  1423. int totemknet_handle_get_stats (
  1424. struct knet_handle_stats *stats)
  1425. {
  1426. int res;
  1427. /* We are probably not using knet */
  1428. if (!global_instance) {
  1429. return CS_ERR_NOT_EXIST;
  1430. }
  1431. res = knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1432. if (res != 0) {
  1433. return (qb_to_cs_error(-errno));
  1434. }
  1435. return CS_OK;
  1436. }
  1437. static void timer_function_merge_detect_timeout (
  1438. void *data)
  1439. {
  1440. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1441. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1442. instance->send_merge_detect_message = 1;
  1443. }
  1444. instance->merge_detect_messages_sent_before_timeout = 0;
  1445. totemknet_start_merge_detect_timeout(instance);
  1446. }
  1447. static void totemknet_start_merge_detect_timeout(
  1448. void *knet_context)
  1449. {
  1450. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1451. qb_loop_timer_add(instance->poll_handle,
  1452. QB_LOOP_MED,
  1453. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1454. (void *)instance,
  1455. timer_function_merge_detect_timeout,
  1456. &instance->timer_merge_detect_timeout);
  1457. }
  1458. static void totemknet_stop_merge_detect_timeout(
  1459. void *knet_context)
  1460. {
  1461. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1462. qb_loop_timer_del(instance->poll_handle,
  1463. instance->timer_merge_detect_timeout);
  1464. }
  1465. static void log_flush_messages (void *knet_context)
  1466. {
  1467. struct pollfd pfd;
  1468. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1469. int cont;
  1470. cont = 1;
  1471. while (cont) {
  1472. pfd.fd = instance->logpipes[0];
  1473. pfd.events = POLLIN;
  1474. pfd.revents = 0;
  1475. if ((poll(&pfd, 1, 0) > 0) &&
  1476. (pfd.revents & POLLIN) &&
  1477. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1478. cont = 1;
  1479. } else {
  1480. cont = 0;
  1481. }
  1482. }
  1483. }
  1484. #ifdef HAVE_LIBNOZZLE
  1485. #define NOZZLE_NAME "nozzle.name"
  1486. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1487. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1488. #define NOZZLE_MACADDR "nozzle.macaddr"
  1489. #define NOZZLE_CHANNEL 1
  1490. static char *get_nozzle_script_dir(void *knet_context)
  1491. {
  1492. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1493. char filename[PATH_MAX + FILENAME_MAX + 1];
  1494. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1495. int res;
  1496. const char *dirname_res;
  1497. /*
  1498. * Build script directory based on corosync.conf file location
  1499. */
  1500. res = snprintf(filename, sizeof(filename), "%s",
  1501. corosync_get_config_file());
  1502. if (res >= sizeof(filename)) {
  1503. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1504. return NULL;
  1505. }
  1506. dirname_res = dirname(filename);
  1507. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1508. dirname_res, "updown.d");
  1509. if (res >= sizeof(updown_dirname)) {
  1510. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1511. return NULL;
  1512. }
  1513. return updown_dirname;
  1514. }
  1515. /*
  1516. * Deliberately doesn't return the status as caller doesn't care.
  1517. * The result will be logged though
  1518. */
  1519. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1520. {
  1521. int res;
  1522. char *exec_string;
  1523. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1524. if (res == -1 && errno != ENOENT) {
  1525. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1526. } else if (res == -2) {
  1527. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1528. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1529. }
  1530. }
  1531. /*
  1532. * Reparse IP address to add in our node ID
  1533. * IPv6 addresses must end in '::'
  1534. * IPv4 addresses must just be valid
  1535. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1536. *
  1537. * Returns the modified IP address as a string to pass into libnozzle
  1538. */
  1539. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1540. const char *input_addr,
  1541. const char *prefix, int nodeid,
  1542. char *output_addr, size_t output_len)
  1543. {
  1544. char *coloncolon;
  1545. int bits;
  1546. int max_prefix = 64;
  1547. uint32_t nodeid_mask;
  1548. uint32_t addr_mask;
  1549. uint32_t masked_nodeid;
  1550. struct in_addr *addr;
  1551. struct totem_ip_address totemip;
  1552. coloncolon = strstr(input_addr, "::");
  1553. if (!coloncolon) {
  1554. max_prefix = 30;
  1555. }
  1556. bits = atoi(prefix);
  1557. if (bits < 8 || bits > max_prefix) {
  1558. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1559. return -1;
  1560. }
  1561. /* IPv6 is easy */
  1562. if (coloncolon) {
  1563. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1564. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1565. return 0;
  1566. }
  1567. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1568. * add in the masked_nodeid and 'print' it out again
  1569. */
  1570. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1571. addr_mask = UINT32_MAX ^ nodeid_mask;
  1572. masked_nodeid = nodeid & nodeid_mask;
  1573. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1574. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1575. return -1;
  1576. }
  1577. addr = (struct in_addr *)&totemip.addr;
  1578. addr->s_addr &= htonl(addr_mask);
  1579. addr->s_addr |= htonl(masked_nodeid);
  1580. inet_ntop(AF_INET, addr, output_addr, output_len);
  1581. return 0;
  1582. }
  1583. static int create_nozzle_device(void *knet_context, const char *name,
  1584. const char *ipaddr, const char *prefix,
  1585. const char *macaddr)
  1586. {
  1587. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1588. char device_name[IFNAMSIZ+1];
  1589. size_t size = IFNAMSIZ;
  1590. int8_t channel = NOZZLE_CHANNEL;
  1591. nozzle_t nozzle_dev;
  1592. int nozzle_fd;
  1593. int res;
  1594. char *updown_dir;
  1595. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1596. char mac[19];
  1597. memset(device_name, 0, size);
  1598. memset(&mac, 0, sizeof(mac));
  1599. strncpy(device_name, name, size);
  1600. updown_dir = get_nozzle_script_dir(knet_context);
  1601. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1602. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1603. if (!nozzle_dev) {
  1604. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1605. return -1;
  1606. }
  1607. instance->nozzle_handle = nozzle_dev;
  1608. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1609. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1610. goto out_clean;
  1611. }
  1612. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1613. /* Prints its own errors */
  1614. goto out_clean;
  1615. }
  1616. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1617. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1618. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1619. goto out_clean;
  1620. }
  1621. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1622. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1623. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1624. if (res != 0) {
  1625. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1626. goto out_clean;
  1627. }
  1628. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1629. res = nozzle_set_up(nozzle_dev);
  1630. if (res != 0) {
  1631. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1632. goto out_clean;
  1633. }
  1634. run_nozzle_script(instance, NOZZLE_UP, "up");
  1635. return 0;
  1636. out_clean:
  1637. nozzle_close(nozzle_dev);
  1638. return -1;
  1639. }
  1640. static int remove_nozzle_device(void *knet_context)
  1641. {
  1642. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1643. int res;
  1644. int datafd;
  1645. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1646. if (res != 0) {
  1647. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1648. return -1;
  1649. }
  1650. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1651. if (res != 0) {
  1652. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1653. return -1;
  1654. }
  1655. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1656. res = nozzle_set_down(instance->nozzle_handle);
  1657. if (res != 0) {
  1658. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1659. return -1;
  1660. }
  1661. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1662. res = nozzle_close(instance->nozzle_handle);
  1663. if (res != 0) {
  1664. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1665. return -1;
  1666. }
  1667. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1668. return 0;
  1669. }
  1670. static void free_nozzle(struct totemknet_instance *instance)
  1671. {
  1672. free(instance->nozzle_name);
  1673. free(instance->nozzle_ipaddr);
  1674. free(instance->nozzle_prefix);
  1675. free(instance->nozzle_macaddr);
  1676. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1677. instance->nozzle_macaddr = NULL;
  1678. }
  1679. static int setup_nozzle(void *knet_context)
  1680. {
  1681. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1682. char *ipaddr_str = NULL;
  1683. char *name_str = NULL;
  1684. char *prefix_str = NULL;
  1685. char *macaddr_str = NULL;
  1686. char mac[32];
  1687. int name_res;
  1688. int macaddr_res;
  1689. int res = -1;
  1690. /*
  1691. * Return value ignored on purpose. icmap_get_string changes
  1692. * ipaddr_str/prefix_str only on success.
  1693. */
  1694. (void)icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1695. (void)icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1696. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1697. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1698. /* Is is being removed? */
  1699. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1700. remove_nozzle_device(instance);
  1701. free_nozzle(instance);
  1702. goto out_free;
  1703. }
  1704. if (!name_str) {
  1705. /* no nozzle */
  1706. goto out_free;
  1707. }
  1708. if (!ipaddr_str) {
  1709. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1710. goto out_free;
  1711. }
  1712. if (!prefix_str) {
  1713. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1714. goto out_free;
  1715. }
  1716. if (macaddr_str && strlen(macaddr_str) != 17) {
  1717. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1718. goto out_free;
  1719. }
  1720. if (!macaddr_str) {
  1721. macaddr_str = (char*)"54:54:01:00:00:00";
  1722. }
  1723. if (instance->nozzle_name &&
  1724. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1725. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1726. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1727. (instance->nozzle_macaddr == NULL ||
  1728. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1729. /* Nothing has changed */
  1730. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1731. goto out_free;
  1732. }
  1733. /* Add nodeid into MAC address */
  1734. memcpy(mac, macaddr_str, 12);
  1735. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1736. instance->our_nodeid >> 8,
  1737. instance->our_nodeid & 0xFF);
  1738. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1739. if (name_res == CS_OK && name_str) {
  1740. /* Reconfigure */
  1741. if (instance->nozzle_name) {
  1742. remove_nozzle_device(instance);
  1743. free_nozzle(instance);
  1744. }
  1745. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1746. mac);
  1747. instance->nozzle_name = strdup(name_str);
  1748. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1749. instance->nozzle_prefix = strdup(prefix_str);
  1750. instance->nozzle_macaddr = strdup(macaddr_str);
  1751. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1752. !instance->nozzle_prefix) {
  1753. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1754. /*
  1755. * This 'free' will cause a complete reconfigure of the device next time we reload
  1756. * but will also let the the current device keep working until then.
  1757. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1758. */
  1759. free_nozzle(instance);
  1760. }
  1761. }
  1762. out_free:
  1763. free(name_str);
  1764. free(ipaddr_str);
  1765. free(prefix_str);
  1766. if (macaddr_res == CS_OK) {
  1767. free(macaddr_str);
  1768. }
  1769. return res;
  1770. }
  1771. #endif // HAVE_LIBNOZZLE