totemknet.c 64 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816817818819820821822823824825826827828829830831832833834835836837838839840841842843844845846847848849850851852853854855856857858859860861862863864865866867868869870871872873874875876877878879880881882883884885886887888889890891892893894895896897898899900901902903904905906907908909910911912913914915916917918919920921922923924925926927928929930931932933934935936937938939940941942943944945946947948949950951952953954955956957958959960961962963964965966967968969970971972973974975976977978979980981982983984985986987988989990991992993994995996997998999100010011002100310041005100610071008100910101011101210131014101510161017101810191020102110221023102410251026102710281029103010311032103310341035103610371038103910401041104210431044104510461047104810491050105110521053105410551056105710581059106010611062106310641065106610671068106910701071107210731074107510761077107810791080108110821083108410851086108710881089109010911092109310941095109610971098109911001101110211031104110511061107110811091110111111121113111411151116111711181119112011211122112311241125112611271128112911301131113211331134113511361137113811391140114111421143114411451146114711481149115011511152115311541155115611571158115911601161116211631164116511661167116811691170117111721173117411751176117711781179118011811182118311841185118611871188118911901191119211931194119511961197119811991200120112021203120412051206120712081209121012111212121312141215121612171218121912201221122212231224122512261227122812291230123112321233123412351236123712381239124012411242124312441245124612471248124912501251125212531254125512561257125812591260126112621263126412651266126712681269127012711272127312741275127612771278127912801281128212831284128512861287128812891290129112921293129412951296129712981299130013011302130313041305130613071308130913101311131213131314131513161317131813191320132113221323132413251326132713281329133013311332133313341335133613371338133913401341134213431344134513461347134813491350135113521353135413551356135713581359136013611362136313641365136613671368136913701371137213731374137513761377137813791380138113821383138413851386138713881389139013911392139313941395139613971398139914001401140214031404140514061407140814091410141114121413141414151416141714181419142014211422142314241425142614271428142914301431143214331434143514361437143814391440144114421443144414451446144714481449145014511452145314541455145614571458145914601461146214631464146514661467146814691470147114721473147414751476147714781479148014811482148314841485148614871488148914901491149214931494149514961497149814991500150115021503150415051506150715081509151015111512151315141515151615171518151915201521152215231524152515261527152815291530153115321533153415351536153715381539154015411542154315441545154615471548154915501551155215531554155515561557155815591560156115621563156415651566156715681569157015711572157315741575157615771578157915801581158215831584158515861587158815891590159115921593159415951596159715981599160016011602160316041605160616071608160916101611161216131614161516161617161816191620162116221623162416251626162716281629163016311632163316341635163616371638163916401641164216431644164516461647164816491650165116521653165416551656165716581659166016611662166316641665166616671668166916701671167216731674167516761677167816791680168116821683168416851686168716881689169016911692169316941695169616971698169917001701170217031704170517061707170817091710171117121713171417151716171717181719172017211722172317241725172617271728172917301731173217331734173517361737173817391740174117421743174417451746174717481749175017511752175317541755175617571758175917601761176217631764176517661767176817691770177117721773177417751776177717781779178017811782178317841785178617871788178917901791179217931794179517961797179817991800180118021803180418051806180718081809181018111812181318141815181618171818181918201821182218231824182518261827182818291830183118321833183418351836183718381839184018411842184318441845184618471848184918501851185218531854185518561857185818591860186118621863186418651866186718681869187018711872187318741875187618771878187918801881188218831884188518861887188818891890189118921893189418951896189718981899190019011902190319041905190619071908190919101911191219131914191519161917191819191920192119221923192419251926192719281929193019311932193319341935193619371938193919401941194219431944194519461947194819491950195119521953195419551956195719581959196019611962196319641965196619671968196919701971197219731974197519761977197819791980198119821983198419851986198719881989199019911992199319941995199619971998199920002001200220032004200520062007200820092010201120122013201420152016201720182019202020212022202320242025202620272028202920302031203220332034203520362037203820392040204120422043204420452046204720482049205020512052205320542055205620572058205920602061206220632064206520662067206820692070207120722073207420752076207720782079208020812082208320842085208620872088208920902091209220932094209520962097209820992100210121022103210421052106210721082109211021112112211321142115211621172118211921202121212221232124212521262127212821292130213121322133213421352136213721382139214021412142214321442145214621472148214921502151215221532154215521562157215821592160216121622163216421652166216721682169217021712172217321742175217621772178217921802181218221832184218521862187218821892190219121922193219421952196219721982199220022012202220322042205220622072208220922102211221222132214221522162217221822192220222122222223222422252226222722282229223022312232223322342235223622372238223922402241224222432244224522462247224822492250225122522253225422552256
  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. struct totemknet_instance *instance,
  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_nodestatus_get (
  399. void *knet_context,
  400. unsigned int nodeid,
  401. struct totem_node_status *node_status)
  402. {
  403. int i;
  404. int res = 0;
  405. struct knet_link_status link_status;
  406. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  407. struct knet_host_status knet_host_status;
  408. uint8_t link_list[KNET_MAX_LINK];
  409. size_t num_links;
  410. if (!instance->knet_handle) {
  411. return CS_ERR_NOT_EXIST; /* Not using knet */
  412. }
  413. if (!node_status) {
  414. return CS_ERR_INVALID_PARAM;
  415. }
  416. res = knet_host_get_status(instance->knet_handle,
  417. nodeid,
  418. &knet_host_status);
  419. if (res) {
  420. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_handle_get_host_status(%d) failed: %d", nodeid, res);
  421. return (-1);
  422. }
  423. node_status->nodeid = nodeid;
  424. node_status->reachable = knet_host_status.reachable;
  425. node_status->remote = knet_host_status.remote;
  426. node_status->external = knet_host_status.external;
  427. #ifdef HAVE_KNET_ONWIRE_VER
  428. res = knet_handle_get_onwire_ver(instance->knet_handle,
  429. nodeid,
  430. &node_status->onwire_min,
  431. &node_status->onwire_max,
  432. &node_status->onwire_ver);
  433. if (res) {
  434. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_handle_get_onwire_ver(%d) failed: %d", nodeid, res);
  435. return (-1);
  436. }
  437. #endif
  438. /* Get link info */
  439. res = knet_link_get_link_list(instance->knet_handle,
  440. nodeid, link_list, &num_links);
  441. if (res) {
  442. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_link_get_link_list(%d) failed: %d", nodeid, res);
  443. return (-1);
  444. }
  445. /* node_status[] has been zeroed for us in totempg.c */
  446. for (i=0; i < num_links; i++) {
  447. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  448. continue;
  449. }
  450. res = knet_link_get_status(instance->knet_handle,
  451. nodeid,
  452. link_list[i],
  453. &link_status,
  454. sizeof(link_status));
  455. if (res == 0) {
  456. node_status->link_status[link_list[i]].enabled = link_status.enabled;
  457. node_status->link_status[link_list[i]].connected = link_status.connected;
  458. node_status->link_status[link_list[i]].dynconnected = link_status.dynconnected;
  459. node_status->link_status[link_list[i]].mtu = link_status.mtu;
  460. memcpy(node_status->link_status[link_list[i]].src_ipaddr, link_status.src_ipaddr, KNET_MAX_HOST_LEN);
  461. memcpy(node_status->link_status[link_list[i]].dst_ipaddr, link_status.dst_ipaddr, KNET_MAX_HOST_LEN);
  462. } else {
  463. knet_log_printf (LOGSYS_LEVEL_WARNING, "knet_link_get_link_status(%d, %d) failed: %d", nodeid, link_list[i], res);
  464. }
  465. }
  466. return res;
  467. }
  468. int totemknet_ifaces_get (void *knet_context,
  469. char ***status,
  470. unsigned int *iface_count)
  471. {
  472. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  473. struct knet_link_status link_status;
  474. knet_node_id_t host_list[KNET_MAX_HOST];
  475. uint8_t link_list[KNET_MAX_LINK];
  476. size_t num_hosts;
  477. size_t num_links;
  478. size_t link_idx;
  479. int i,j;
  480. char *ptr;
  481. int res = 0;
  482. /*
  483. * Don't do the whole 'link_info' bit if the caller just wants
  484. * a count of interfaces.
  485. */
  486. if (status) {
  487. int own_idx = OWN_INDEX_NONE;
  488. res = knet_host_get_host_list(instance->knet_handle,
  489. host_list, &num_hosts);
  490. if (res) {
  491. return (-1);
  492. }
  493. qsort(host_list, num_hosts, sizeof(uint16_t), node_compare);
  494. for (j=0; j<num_hosts; j++) {
  495. if (host_list[j] == instance->our_nodeid) {
  496. own_idx = j;
  497. break;
  498. }
  499. }
  500. for (i=0; i<INTERFACE_MAX; i++) {
  501. memset(instance->link_status[i], 'd', CFG_INTERFACE_STATUS_MAX_LEN-1);
  502. if (own_idx != OWN_INDEX_NONE) {
  503. instance->link_status[i][own_idx] = 'n';
  504. }
  505. instance->link_status[i][num_hosts] = '\0';
  506. }
  507. /* This is all a bit "inside-out" because "status" is a set of strings per link
  508. * and knet orders things by host
  509. */
  510. for (j=0; j<num_hosts; j++) {
  511. if (own_idx != OWN_INDEX_NONE && j == own_idx) {
  512. continue ;
  513. }
  514. res = knet_link_get_link_list(instance->knet_handle,
  515. host_list[j], link_list, &num_links);
  516. if (res) {
  517. return (-1);
  518. }
  519. link_idx = 0;
  520. for (i=0; i < num_links; i++) {
  521. /*
  522. * Skip over links that are unconfigured to corosync. This is basically
  523. * link0 if corosync isn't using it for comms, as we will still
  524. * have it set up for loopback.
  525. */
  526. if (!instance->totem_config->interfaces[link_list[i]].configured) {
  527. continue;
  528. }
  529. ptr = instance->link_status[link_idx++];
  530. res = knet_link_get_status(instance->knet_handle,
  531. host_list[j],
  532. link_list[i],
  533. &link_status,
  534. sizeof(link_status));
  535. if (res == 0) {
  536. ptr[j] = '0' + (link_status.enabled |
  537. link_status.connected<<1 |
  538. link_status.dynconnected<<2);
  539. }
  540. else {
  541. knet_log_printf (LOGSYS_LEVEL_ERROR,
  542. "totemknet_ifaces_get: Cannot get link status: %s", strerror(errno));
  543. ptr[j] = '?';
  544. }
  545. }
  546. }
  547. *status = instance->link_status;
  548. }
  549. *iface_count = INTERFACE_MAX;
  550. return (res);
  551. }
  552. int totemknet_finalize (
  553. void *knet_context)
  554. {
  555. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  556. int res = 0;
  557. int i,j;
  558. static knet_node_id_t nodes[KNET_MAX_HOST]; /* static to save stack */
  559. uint8_t links[KNET_MAX_LINK];
  560. size_t num_nodes;
  561. size_t num_links;
  562. knet_log_printf(LOG_DEBUG, "totemknet: finalize");
  563. qb_loop_poll_del (instance->poll_handle, instance->logpipes[0]);
  564. qb_loop_poll_del (instance->poll_handle, instance->knet_fd);
  565. /*
  566. * Disable forwarding to make knet flush send queue. This ensures that the LEAVE message will be sent.
  567. */
  568. res = knet_handle_setfwd(instance->knet_handle, 0);
  569. if (res) {
  570. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_setfwd failed: %s", strerror(errno));
  571. }
  572. res = knet_host_get_host_list(instance->knet_handle, nodes, &num_nodes);
  573. if (res) {
  574. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet node list for shutdown: %s", strerror(errno));
  575. /* Crash out anyway */
  576. goto finalise_error;
  577. }
  578. /* Tidily shut down all nodes & links. */
  579. for (i=0; i<num_nodes; i++) {
  580. res = knet_link_get_link_list(instance->knet_handle, nodes[i], links, &num_links);
  581. if (res) {
  582. knet_log_printf (LOGSYS_LEVEL_ERROR, "Cannot get knet link list for node " CS_PRI_NODE_ID ": %s", nodes[i], strerror(errno));
  583. goto finalise_error;
  584. }
  585. for (j=0; j<num_links; j++) {
  586. res = knet_link_set_enable(instance->knet_handle, nodes[i], links[j], 0);
  587. if (res) {
  588. 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));
  589. }
  590. res = knet_link_clear_config(instance->knet_handle, nodes[i], links[j]);
  591. if (res) {
  592. 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));
  593. }
  594. }
  595. res = knet_host_remove(instance->knet_handle, nodes[i]);
  596. if (res) {
  597. knet_log_printf (LOGSYS_LEVEL_ERROR, "totemknet: knet_host_remove(node " CS_PRI_NODE_ID ") failed: %s", nodes[i], strerror(errno));
  598. }
  599. }
  600. finalise_error:
  601. res = knet_handle_free(instance->knet_handle);
  602. if (res) {
  603. knet_log_printf (LOGSYS_LEVEL_CRIT, "totemknet: knet_handle_free failed: %s", strerror(errno));
  604. }
  605. totemknet_stop_merge_detect_timeout(instance);
  606. log_flush_messages(instance);
  607. /*
  608. * Error is deliberately ignored
  609. */
  610. (void)pthread_mutex_destroy(&instance->log_mutex);
  611. return (res);
  612. }
  613. static int log_deliver_fn (
  614. int fd,
  615. int revents,
  616. void *data)
  617. {
  618. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  619. char buffer[sizeof(struct knet_log_msg)*4];
  620. char *bufptr = buffer;
  621. int done = 0;
  622. int len;
  623. len = read(fd, buffer, sizeof(buffer));
  624. while (done < len) {
  625. struct knet_log_msg *msg = (struct knet_log_msg *)bufptr;
  626. switch (msg->msglevel) {
  627. case KNET_LOG_ERR:
  628. libknet_log_printf (LOGSYS_LEVEL_ERROR, "%s: %s",
  629. knet_log_get_subsystem_name(msg->subsystem),
  630. msg->msg);
  631. break;
  632. case KNET_LOG_WARN:
  633. libknet_log_printf (LOGSYS_LEVEL_WARNING, "%s: %s",
  634. knet_log_get_subsystem_name(msg->subsystem),
  635. msg->msg);
  636. break;
  637. case KNET_LOG_INFO:
  638. libknet_log_printf (LOGSYS_LEVEL_INFO, "%s: %s",
  639. knet_log_get_subsystem_name(msg->subsystem),
  640. msg->msg);
  641. break;
  642. case KNET_LOG_DEBUG:
  643. libknet_log_printf (LOGSYS_LEVEL_DEBUG, "%s: %s",
  644. knet_log_get_subsystem_name(msg->subsystem),
  645. msg->msg);
  646. break;
  647. }
  648. bufptr += sizeof(struct knet_log_msg);
  649. done += sizeof(struct knet_log_msg);
  650. }
  651. return 0;
  652. }
  653. static int data_deliver_fn (
  654. int fd,
  655. int revents,
  656. void *data)
  657. {
  658. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  659. struct msghdr msg_hdr;
  660. struct iovec iov_recv;
  661. struct sockaddr_storage system_from;
  662. ssize_t msg_len;
  663. int truncated_packet;
  664. iov_recv.iov_base = instance->iov_buffer;
  665. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  666. msg_hdr.msg_name = &system_from;
  667. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  668. msg_hdr.msg_iov = &iov_recv;
  669. msg_hdr.msg_iovlen = 1;
  670. #ifdef HAVE_MSGHDR_CONTROL
  671. msg_hdr.msg_control = 0;
  672. #endif
  673. #ifdef HAVE_MSGHDR_CONTROLLEN
  674. msg_hdr.msg_controllen = 0;
  675. #endif
  676. #ifdef HAVE_MSGHDR_FLAGS
  677. msg_hdr.msg_flags = 0;
  678. #endif
  679. #ifdef HAVE_MSGHDR_ACCRIGHTS
  680. msg_hdr.msg_accrights = NULL;
  681. #endif
  682. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  683. msg_hdr.msg_accrightslen = 0;
  684. #endif
  685. msg_len = recvmsg (fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  686. if (msg_len <= 0) {
  687. return (0);
  688. }
  689. truncated_packet = 0;
  690. #ifdef HAVE_MSGHDR_FLAGS
  691. if (msg_hdr.msg_flags & MSG_TRUNC) {
  692. truncated_packet = 1;
  693. }
  694. #else
  695. /*
  696. * We don't have MSGHDR_FLAGS, but we can (hopefully) safely make assumption that
  697. * if bytes_received == KNET_MAX_PACKET_SIZE then packet is truncated
  698. */
  699. if (bytes_received == KNET_MAX_PACKET_SIZE) {
  700. truncated_packet = 1;
  701. }
  702. #endif
  703. if (truncated_packet) {
  704. knet_log_printf(instance->totemknet_log_level_error,
  705. "Received too big message. This may be because something bad is happening"
  706. "on the network (attack?), or you tried join more nodes than corosync is"
  707. "compiled with (%u) or bug in the code (bad estimation of "
  708. "the KNET_MAX_PACKET_SIZE). Dropping packet.", PROCESSOR_COUNT_MAX);
  709. return (0);
  710. }
  711. /*
  712. * Handle incoming message
  713. */
  714. instance->totemknet_deliver_fn (
  715. instance->context,
  716. instance->iov_buffer,
  717. msg_len,
  718. &system_from);
  719. return (0);
  720. }
  721. static void timer_function_netif_check_timeout (
  722. void *data)
  723. {
  724. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  725. int i;
  726. for (i=0; i < INTERFACE_MAX; i++) {
  727. if (!instance->totem_config->interfaces[i].configured) {
  728. continue;
  729. }
  730. instance->totemknet_iface_change_fn (instance->context,
  731. &instance->my_ids[i],
  732. i);
  733. }
  734. }
  735. static void knet_set_access_list_config(struct totemknet_instance *instance)
  736. {
  737. #ifdef HAVE_KNET_ACCESS_LIST
  738. uint32_t value;
  739. cs_error_t err;
  740. value = instance->totem_config->block_unlisted_ips;
  741. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_enable access list: %d", value);
  742. err = knet_handle_enable_access_lists(instance->knet_handle, value);
  743. if (err) {
  744. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_access_lists failed");
  745. }
  746. #endif
  747. }
  748. void totemknet_configure_log_level()
  749. {
  750. int logsys_log_mode;
  751. int knet_log_mode = KNET_LOG_INFO;
  752. uint8_t s;
  753. if (!global_instance || !global_instance->knet_handle) {
  754. return;
  755. }
  756. /* Reconfigure logging level */
  757. logsys_log_mode = logsys_config_debug_get("KNET");
  758. switch (logsys_log_mode) {
  759. case LOGSYS_DEBUG_OFF:
  760. knet_log_mode = KNET_LOG_INFO;
  761. break;
  762. case LOGSYS_DEBUG_ON:
  763. knet_log_mode = KNET_LOG_DEBUG;
  764. break;
  765. case LOGSYS_DEBUG_TRACE:
  766. knet_log_mode = KNET_LOG_DEBUG;
  767. break;
  768. }
  769. log_printf (LOGSYS_LEVEL_DEBUG, "totemknet setting log level %s", knet_log_get_loglevel_name(knet_log_mode));
  770. for (s = 0; s<KNET_MAX_SUBSYSTEMS; s++) {
  771. knet_log_set_loglevel(global_instance->knet_handle, s, knet_log_mode);
  772. }
  773. }
  774. /* NOTE: this relies on the fact that totem_reload_notify() is called first */
  775. static void totemknet_refresh_config(
  776. int32_t event,
  777. const char *key_name,
  778. struct icmap_notify_value new_val,
  779. struct icmap_notify_value old_val,
  780. void *user_data)
  781. {
  782. uint8_t reloading;
  783. uint32_t value;
  784. uint32_t link_no;
  785. size_t num_nodes;
  786. knet_node_id_t host_ids[KNET_MAX_HOST];
  787. int i;
  788. int err;
  789. struct totemknet_instance *instance = (struct totemknet_instance *)user_data;
  790. ENTER();
  791. /*
  792. * If a full reload is in progress then don't do anything until it's done and
  793. * can reconfigure it all atomically
  794. */
  795. if (icmap_get_uint8("config.totemconfig_reload_in_progress", &reloading) == CS_OK && reloading) {
  796. return;
  797. }
  798. knet_set_access_list_config(instance);
  799. if (icmap_get_uint32("totem.knet_pmtud_interval", &value) == CS_OK) {
  800. instance->totem_config->knet_pmtud_interval = value;
  801. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet_pmtud_interval now %d", value);
  802. err = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  803. if (err) {
  804. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  805. }
  806. }
  807. /* Configure link parameters for each node */
  808. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_nodes);
  809. if (err != 0) {
  810. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list failed");
  811. }
  812. for (i=0; i<num_nodes; i++) {
  813. for (link_no = 0; link_no < INTERFACE_MAX; link_no++) {
  814. if (host_ids[i] == instance->our_nodeid || !instance->totem_config->interfaces[link_no].configured) {
  815. continue;
  816. }
  817. err = knet_link_set_ping_timers(instance->knet_handle, host_ids[i], link_no,
  818. instance->totem_config->interfaces[link_no].knet_ping_interval,
  819. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  820. instance->totem_config->interfaces[link_no].knet_ping_precision);
  821. if (err) {
  822. 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);
  823. }
  824. err = knet_link_set_pong_count(instance->knet_handle, host_ids[i], link_no,
  825. instance->totem_config->interfaces[link_no].knet_pong_count);
  826. if (err) {
  827. 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);
  828. }
  829. err = knet_link_set_priority(instance->knet_handle, host_ids[i], link_no,
  830. instance->totem_config->interfaces[link_no].knet_link_priority);
  831. if (err) {
  832. 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);
  833. }
  834. }
  835. }
  836. /* Log levels get reconfigured from logconfig.c as that happens last in the reload */
  837. LEAVE();
  838. }
  839. static void totemknet_add_config_notifications(struct totemknet_instance *instance)
  840. {
  841. icmap_track_t icmap_track_totem = NULL;
  842. icmap_track_t icmap_track_reload = NULL;
  843. ENTER();
  844. icmap_track_add("totem.",
  845. ICMAP_TRACK_ADD | ICMAP_TRACK_DELETE | ICMAP_TRACK_MODIFY | ICMAP_TRACK_PREFIX,
  846. totemknet_refresh_config,
  847. instance,
  848. &icmap_track_totem);
  849. icmap_track_add("config.totemconfig_reload_in_progress",
  850. ICMAP_TRACK_ADD | ICMAP_TRACK_MODIFY,
  851. totemknet_refresh_config,
  852. instance,
  853. &icmap_track_reload);
  854. LEAVE();
  855. }
  856. static int totemknet_is_crypto_enabled(const struct totemknet_instance *instance)
  857. {
  858. return (!(strcmp(instance->totem_config->crypto_cipher_type, "none") == 0 &&
  859. strcmp(instance->totem_config->crypto_hash_type, "none") == 0));
  860. }
  861. static int totemknet_set_knet_crypto(struct totemknet_instance *instance)
  862. {
  863. struct knet_handle_crypto_cfg crypto_cfg;
  864. int res;
  865. /* These have already been validated */
  866. memcpy(crypto_cfg.crypto_model, instance->totem_config->crypto_model, sizeof(crypto_cfg.crypto_model));
  867. memcpy(crypto_cfg.crypto_cipher_type, instance->totem_config->crypto_cipher_type, sizeof(crypto_cfg.crypto_model));
  868. memcpy(crypto_cfg.crypto_hash_type, instance->totem_config->crypto_hash_type, sizeof(crypto_cfg.crypto_model));
  869. memcpy(crypto_cfg.private_key, instance->totem_config->private_key, instance->totem_config->private_key_len);
  870. crypto_cfg.private_key_len = instance->totem_config->private_key_len;
  871. #ifdef HAVE_KNET_CRYPTO_RECONF
  872. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s on index %d",
  873. crypto_cfg.crypto_model,
  874. crypto_cfg.crypto_cipher_type,
  875. crypto_cfg.crypto_hash_type,
  876. instance->totem_config->crypto_index
  877. );
  878. /* If crypto is being disabled we need to explicitly allow cleartext traffic in knet */
  879. if (!totemknet_is_crypto_enabled(instance)) {
  880. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  881. if (res) {
  882. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(ALLOW) failed %s", strerror(errno));
  883. }
  884. }
  885. /* use_config will be called later when all nodes are synced */
  886. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, instance->totem_config->crypto_index);
  887. if (res == -1) {
  888. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: %s", instance->totem_config->crypto_index, strerror(errno));
  889. goto exit_error;
  890. }
  891. if (res == -2) {
  892. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config (index %d) failed: -2", instance->totem_config->crypto_index);
  893. goto exit_error;
  894. }
  895. #else
  896. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Configuring crypto %s/%s/%s",
  897. crypto_cfg.crypto_model,
  898. crypto_cfg.crypto_cipher_type,
  899. crypto_cfg.crypto_hash_type
  900. );
  901. res = knet_handle_crypto(instance->knet_handle, &crypto_cfg);
  902. if (res == -1) {
  903. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: %s", strerror(errno));
  904. goto exit_error;
  905. }
  906. if (res == -2) {
  907. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto failed: -2");
  908. goto exit_error;
  909. }
  910. #endif
  911. exit_error:
  912. return res;
  913. }
  914. /*
  915. * Create an instance
  916. */
  917. int totemknet_initialize (
  918. qb_loop_t *poll_handle,
  919. void **knet_context,
  920. struct totem_config *totem_config,
  921. totemsrp_stats_t *stats,
  922. void *context,
  923. void (*deliver_fn) (
  924. void *context,
  925. const void *msg,
  926. unsigned int msg_len,
  927. const struct sockaddr_storage *system_from),
  928. void (*iface_change_fn) (
  929. void *context,
  930. const struct totem_ip_address *iface_address,
  931. unsigned int link_no),
  932. void (*mtu_changed) (
  933. void *context,
  934. int net_mtu),
  935. void (*target_set_completed) (
  936. void *context))
  937. {
  938. struct totemknet_instance *instance;
  939. char *tmp_str;
  940. int8_t channel=0;
  941. int allow_knet_handle_fallback=0;
  942. int res;
  943. int i;
  944. instance = malloc (sizeof (struct totemknet_instance));
  945. if (instance == NULL) {
  946. return (-1);
  947. }
  948. totemknet_instance_initialize (instance);
  949. instance->totem_config = totem_config;
  950. /*
  951. * Configure logging
  952. */
  953. instance->totemknet_log_level_security = 1; //totem_config->totem_logging_configuration.log_level_security;
  954. instance->totemknet_log_level_error = totem_config->totem_logging_configuration.log_level_error;
  955. instance->totemknet_log_level_warning = totem_config->totem_logging_configuration.log_level_warning;
  956. instance->totemknet_log_level_notice = totem_config->totem_logging_configuration.log_level_notice;
  957. instance->totemknet_log_level_debug = totem_config->totem_logging_configuration.log_level_debug;
  958. instance->totemknet_subsys_id = totem_config->totem_logging_configuration.log_subsys_id;
  959. instance->totemknet_log_printf = totem_config->totem_logging_configuration.log_printf;
  960. instance->knet_subsys_id = _logsys_subsys_create("KNET", "libknet.h");
  961. /*
  962. * Initialize local variables for totemknet
  963. */
  964. instance->our_nodeid = instance->totem_config->node_id;
  965. for (i=0; i< INTERFACE_MAX; i++) {
  966. totemip_copy(&instance->my_ids[i], &totem_config->interfaces[i].bindnet);
  967. instance->my_ids[i].nodeid = instance->our_nodeid;
  968. instance->ip_port[i] = totem_config->interfaces[i].ip_port;
  969. /* Needed for totemsrp */
  970. totem_config->interfaces[i].boundto.nodeid = instance->our_nodeid;
  971. }
  972. instance->poll_handle = poll_handle;
  973. instance->context = context;
  974. instance->totemknet_deliver_fn = deliver_fn;
  975. instance->totemknet_iface_change_fn = iface_change_fn;
  976. instance->totemknet_mtu_changed = mtu_changed;
  977. instance->totemknet_target_set_completed = target_set_completed;
  978. instance->loopback_link = 0;
  979. res = pipe(instance->logpipes);
  980. if (res == -1) {
  981. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to create pipe for instance->logpipes");
  982. goto exit_error;
  983. }
  984. if (fcntl(instance->logpipes[0], F_SETFL, O_NONBLOCK) == -1 ||
  985. fcntl(instance->logpipes[1], F_SETFL, O_NONBLOCK) == -1) {
  986. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "failed to set O_NONBLOCK flag for instance->logpipes");
  987. goto exit_error;
  988. }
  989. if (icmap_get_string("system.allow_knet_handle_fallback", &tmp_str) == CS_OK) {
  990. if (strcmp(tmp_str, "yes") == 0) {
  991. allow_knet_handle_fallback = 1;
  992. }
  993. free(tmp_str);
  994. }
  995. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  996. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, KNET_HANDLE_FLAG_PRIVILEGED);
  997. #else
  998. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG);
  999. #endif
  1000. if (allow_knet_handle_fallback && !instance->knet_handle && errno == ENAMETOOLONG) {
  1001. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_new failed, trying unprivileged");
  1002. #if defined(KNET_API_VER) && (KNET_API_VER == 2)
  1003. instance->knet_handle = knet_handle_new(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1004. #else
  1005. instance->knet_handle = knet_handle_new_ex(instance->totem_config->node_id, instance->logpipes[1], KNET_LOG_DEBUG, 0);
  1006. #endif
  1007. }
  1008. if (!instance->knet_handle) {
  1009. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_CRIT, "knet_handle_new failed");
  1010. goto exit_error;
  1011. }
  1012. knet_set_access_list_config(instance);
  1013. res = knet_handle_pmtud_setfreq(instance->knet_handle, instance->totem_config->knet_pmtud_interval);
  1014. if (res) {
  1015. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_pmtud_setfreq failed");
  1016. }
  1017. res = knet_handle_enable_filter(instance->knet_handle, instance, dst_host_filter_callback_fn);
  1018. if (res) {
  1019. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_filter failed");
  1020. }
  1021. res = knet_handle_enable_sock_notify(instance->knet_handle, instance, socket_error_callback_fn);
  1022. if (res) {
  1023. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_sock_notify failed");
  1024. }
  1025. res = knet_host_enable_status_change_notify(instance->knet_handle, instance, host_change_callback_fn);
  1026. if (res) {
  1027. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_host_enable_status_change_notify failed");
  1028. }
  1029. res = knet_handle_enable_pmtud_notify(instance->knet_handle, instance, pmtu_change_callback_fn);
  1030. if (res) {
  1031. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_WARNING, "knet_handle_enable_pmtud_notify failed");
  1032. }
  1033. global_instance = instance;
  1034. /* Setup knet logging level */
  1035. totemknet_configure_log_level();
  1036. /* Get an fd into knet */
  1037. instance->knet_fd = 0;
  1038. res = knet_handle_add_datafd(instance->knet_handle, &instance->knet_fd, &channel);
  1039. if (res) {
  1040. knet_log_printf(LOG_DEBUG, "knet_handle_add_datafd failed: %s", strerror(errno));
  1041. goto exit_error;
  1042. }
  1043. /* Enable crypto if requested */
  1044. #ifdef HAVE_KNET_CRYPTO_RECONF
  1045. if (totemknet_is_crypto_enabled(instance)) {
  1046. res = totemknet_set_knet_crypto(instance);
  1047. if (res == 0) {
  1048. res = knet_handle_crypto_use_config(instance->knet_handle, totem_config->crypto_index);
  1049. if (res) {
  1050. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_use_config failed: %s", strerror(errno));
  1051. goto exit_error;
  1052. }
  1053. } else {
  1054. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1055. goto exit_error;
  1056. }
  1057. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1058. if (res) {
  1059. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (DISALLOW) failed: %s", strerror(errno));
  1060. goto exit_error;
  1061. }
  1062. } else {
  1063. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_ALLOW_CLEAR_TRAFFIC);
  1064. if (res) {
  1065. knet_log_printf(LOG_DEBUG, "knet_handle_crypto_rx_clear_traffic (ALLOW) failed: %s", strerror(errno));
  1066. goto exit_error;
  1067. }
  1068. }
  1069. #else
  1070. if (totemknet_is_crypto_enabled(instance)) {
  1071. res = totemknet_set_knet_crypto(instance);
  1072. if (res) {
  1073. knet_log_printf(LOG_DEBUG, "Failed to set up knet crypto");
  1074. goto exit_error;
  1075. }
  1076. }
  1077. #endif
  1078. /* Set up compression */
  1079. if (strcmp(totem_config->knet_compression_model, "none") != 0) {
  1080. /* Not fatal, but will log */
  1081. (void)totemknet_configure_compression(instance, totem_config);
  1082. }
  1083. knet_handle_setfwd(instance->knet_handle, 1);
  1084. instance->link_mode = KNET_LINK_POLICY_PASSIVE;
  1085. if (strcmp(instance->totem_config->link_mode, "active")==0) {
  1086. instance->link_mode = KNET_LINK_POLICY_ACTIVE;
  1087. }
  1088. if (strcmp(instance->totem_config->link_mode, "rr")==0) {
  1089. instance->link_mode = KNET_LINK_POLICY_RR;
  1090. }
  1091. for (i=0; i<INTERFACE_MAX; i++) {
  1092. instance->link_status[i] = malloc(CFG_INTERFACE_STATUS_MAX_LEN);
  1093. if (!instance->link_status[i]) {
  1094. goto exit_error;
  1095. }
  1096. }
  1097. qb_loop_poll_add (instance->poll_handle,
  1098. QB_LOOP_MED,
  1099. instance->logpipes[0],
  1100. POLLIN, instance, log_deliver_fn);
  1101. qb_loop_poll_add (instance->poll_handle,
  1102. QB_LOOP_HIGH,
  1103. instance->knet_fd,
  1104. POLLIN, instance, data_deliver_fn);
  1105. /*
  1106. * Upper layer isn't ready to receive message because it hasn't
  1107. * initialized yet. Add short timer to check the interfaces.
  1108. */
  1109. qb_loop_timer_add (instance->poll_handle,
  1110. QB_LOOP_MED,
  1111. 100*QB_TIME_NS_IN_MSEC,
  1112. (void *)instance,
  1113. timer_function_netif_check_timeout,
  1114. &instance->timer_netif_check_timeout);
  1115. totemknet_start_merge_detect_timeout(instance);
  1116. /* Start listening for config changes */
  1117. totemknet_add_config_notifications(instance);
  1118. /* Add stats keys to icmap */
  1119. stats_knet_add_handle();
  1120. knet_log_printf (LOGSYS_LEVEL_INFO, "totemknet initialized");
  1121. *knet_context = instance;
  1122. return (0);
  1123. exit_error:
  1124. log_flush_messages(instance);
  1125. free(instance);
  1126. return (-1);
  1127. }
  1128. void *totemknet_buffer_alloc (void)
  1129. {
  1130. /* Need to have space for a message AND a struct mcast in case of encapsulated messages */
  1131. return malloc(KNET_MAX_PACKET_SIZE + 512);
  1132. }
  1133. void totemknet_buffer_release (void *ptr)
  1134. {
  1135. return free (ptr);
  1136. }
  1137. int totemknet_processor_count_set (
  1138. void *knet_context,
  1139. int processor_count)
  1140. {
  1141. return (0);
  1142. }
  1143. int totemknet_recv_flush (void *knet_context)
  1144. {
  1145. return (0);
  1146. }
  1147. int totemknet_send_flush (void *knet_context)
  1148. {
  1149. return (0);
  1150. }
  1151. int totemknet_token_send (
  1152. void *knet_context,
  1153. const void *msg,
  1154. unsigned int msg_len)
  1155. {
  1156. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1157. int res = 0;
  1158. ucast_sendmsg (instance, &instance->token_target, msg, msg_len);
  1159. return (res);
  1160. }
  1161. int totemknet_mcast_flush_send (
  1162. void *knet_context,
  1163. const void *msg,
  1164. unsigned int msg_len)
  1165. {
  1166. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1167. int res = 0;
  1168. mcast_sendmsg (instance, msg, msg_len, 0);
  1169. return (res);
  1170. }
  1171. int totemknet_mcast_noflush_send (
  1172. void *knet_context,
  1173. const void *msg,
  1174. unsigned int msg_len)
  1175. {
  1176. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1177. int res = 0;
  1178. mcast_sendmsg (instance, msg, msg_len, 1);
  1179. return (res);
  1180. }
  1181. extern int totemknet_iface_check (void *knet_context)
  1182. {
  1183. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1184. int res = 0;
  1185. knet_log_printf(LOG_DEBUG, "totemknet: iface_check");
  1186. return (res);
  1187. }
  1188. extern void totemknet_net_mtu_adjust (void *knet_context, struct totem_config *totem_config)
  1189. {
  1190. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1191. knet_log_printf(LOG_DEBUG, "totemknet: Returning MTU of %d", totem_config->net_mtu);
  1192. }
  1193. int totemknet_token_target_set (
  1194. void *knet_context,
  1195. unsigned int nodeid)
  1196. {
  1197. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1198. int res = 0;
  1199. instance->token_target.nodeid = nodeid;
  1200. instance->totemknet_target_set_completed (instance->context);
  1201. return (res);
  1202. }
  1203. extern int totemknet_recv_mcast_empty (
  1204. void *knet_context)
  1205. {
  1206. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1207. unsigned int res;
  1208. struct sockaddr_storage system_from;
  1209. struct msghdr msg_hdr;
  1210. struct iovec iov_recv;
  1211. struct pollfd ufd;
  1212. int nfds;
  1213. int msg_processed = 0;
  1214. iov_recv.iov_base = instance->iov_buffer;
  1215. iov_recv.iov_len = KNET_MAX_PACKET_SIZE;
  1216. msg_hdr.msg_name = &system_from;
  1217. msg_hdr.msg_namelen = sizeof (struct sockaddr_storage);
  1218. msg_hdr.msg_iov = &iov_recv;
  1219. msg_hdr.msg_iovlen = 1;
  1220. #ifdef HAVE_MSGHDR_CONTROL
  1221. msg_hdr.msg_control = 0;
  1222. #endif
  1223. #ifdef HAVE_MSGHDR_CONTROLLEN
  1224. msg_hdr.msg_controllen = 0;
  1225. #endif
  1226. #ifdef HAVE_MSGHDR_FLAGS
  1227. msg_hdr.msg_flags = 0;
  1228. #endif
  1229. #ifdef HAVE_MSGHDR_ACCRIGHTS
  1230. msg_msg_hdr.msg_accrights = NULL;
  1231. #endif
  1232. #ifdef HAVE_MSGHDR_ACCRIGHTSLEN
  1233. msg_msg_hdr.msg_accrightslen = 0;
  1234. #endif
  1235. do {
  1236. ufd.fd = instance->knet_fd;
  1237. ufd.events = POLLIN;
  1238. nfds = poll (&ufd, 1, 0);
  1239. if (nfds == 1 && ufd.revents & POLLIN) {
  1240. res = recvmsg (instance->knet_fd, &msg_hdr, MSG_NOSIGNAL | MSG_DONTWAIT);
  1241. if (res != -1) {
  1242. msg_processed = 1;
  1243. } else {
  1244. msg_processed = -1;
  1245. }
  1246. }
  1247. } while (nfds == 1);
  1248. return (msg_processed);
  1249. }
  1250. int totemknet_iface_set (void *knet_context,
  1251. const struct totem_ip_address *local_addr,
  1252. unsigned short ip_port,
  1253. unsigned int iface_no)
  1254. {
  1255. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1256. totemip_copy(&instance->my_ids[iface_no], local_addr);
  1257. knet_log_printf(LOG_INFO, "Configured link number %d: local addr: %s, port=%d", iface_no, totemip_print(local_addr), ip_port);
  1258. instance->ip_port[iface_no] = ip_port;
  1259. return 0;
  1260. }
  1261. int totemknet_member_add (
  1262. void *knet_context,
  1263. const struct totem_ip_address *local,
  1264. const struct totem_ip_address *member,
  1265. int link_no)
  1266. {
  1267. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1268. int err;
  1269. int port = instance->ip_port[link_no];
  1270. struct sockaddr_storage remote_ss;
  1271. struct sockaddr_storage local_ss;
  1272. int addrlen;
  1273. int i;
  1274. int host_found = 0;
  1275. knet_node_id_t host_ids[KNET_MAX_HOST];
  1276. size_t num_host_ids;
  1277. /* Only create 1 loopback link and use link 0 */
  1278. if (member->nodeid == instance->our_nodeid) {
  1279. if (!instance->loopback_link) {
  1280. link_no = 0;
  1281. instance->loopback_link = 1;
  1282. } else {
  1283. /* Already done */
  1284. return 0;
  1285. }
  1286. }
  1287. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: " CS_PRI_NODE_ID " (%s), link=%d", member->nodeid, totemip_print(member), link_no);
  1288. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: local: " CS_PRI_NODE_ID " (%s)", local->nodeid, totemip_print(local));
  1289. /* Only add the host if it doesn't already exist in knet */
  1290. err = knet_host_get_host_list(instance->knet_handle, host_ids, &num_host_ids);
  1291. if (err) {
  1292. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_get_host_list");
  1293. return -1;
  1294. }
  1295. for (i=0; i<num_host_ids; i++) {
  1296. if (host_ids[i] == member->nodeid) {
  1297. host_found = 1;
  1298. }
  1299. }
  1300. if (!host_found) {
  1301. err = knet_host_add(instance->knet_handle, member->nodeid);
  1302. if (err != 0 && errno != EEXIST) {
  1303. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_host_add");
  1304. return -1;
  1305. }
  1306. } else {
  1307. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nodeid " CS_PRI_NODE_ID " already added", member->nodeid);
  1308. }
  1309. if (err == 0) {
  1310. if (knet_host_set_policy(instance->knet_handle, member->nodeid, instance->link_mode)) {
  1311. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_set_policy failed");
  1312. return -1;
  1313. }
  1314. }
  1315. memset(&local_ss, 0, sizeof(local_ss));
  1316. memset(&remote_ss, 0, sizeof(remote_ss));
  1317. /* Casts to remove const */
  1318. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)member, port, &remote_ss, &addrlen);
  1319. totemip_totemip_to_sockaddr_convert((struct totem_ip_address *)local, port, &local_ss, &addrlen);
  1320. if (member->nodeid == instance->our_nodeid) {
  1321. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: loopback link is %d\n", link_no);
  1322. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1323. KNET_TRANSPORT_LOOPBACK,
  1324. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1325. }
  1326. else {
  1327. err = knet_link_set_config(instance->knet_handle, member->nodeid, link_no,
  1328. instance->totem_config->interfaces[link_no].knet_transport,
  1329. &local_ss, &remote_ss, KNET_LINK_FLAG_TRAFFICHIPRIO);
  1330. }
  1331. if (err) {
  1332. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_config failed");
  1333. return -1;
  1334. }
  1335. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_add: Setting link prio to %d",
  1336. instance->totem_config->interfaces[link_no].knet_link_priority);
  1337. err = knet_link_set_priority(instance->knet_handle, member->nodeid, link_no,
  1338. instance->totem_config->interfaces[link_no].knet_link_priority);
  1339. if (err) {
  1340. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_priority for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1341. }
  1342. /* ping timeouts maybe 0 here for a newly added interface so we leave this till later, it will
  1343. get done in totemknet_refresh_config */
  1344. if (instance->totem_config->interfaces[link_no].knet_ping_interval != 0) {
  1345. err = knet_link_set_ping_timers(instance->knet_handle, member->nodeid, link_no,
  1346. instance->totem_config->interfaces[link_no].knet_ping_interval,
  1347. instance->totem_config->interfaces[link_no].knet_ping_timeout,
  1348. instance->totem_config->interfaces[link_no].knet_ping_precision);
  1349. if (err) {
  1350. 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);
  1351. }
  1352. err = knet_link_set_pong_count(instance->knet_handle, member->nodeid, link_no,
  1353. instance->totem_config->interfaces[link_no].knet_pong_count);
  1354. if (err) {
  1355. 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);
  1356. }
  1357. }
  1358. err = knet_link_set_enable(instance->knet_handle, member->nodeid, link_no, 1);
  1359. if (err) {
  1360. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_link_set_enable for nodeid " CS_PRI_NODE_ID ", link %d failed", member->nodeid, link_no);
  1361. return -1;
  1362. }
  1363. /* register stats */
  1364. stats_knet_add_member(member->nodeid, link_no);
  1365. return (0);
  1366. }
  1367. int totemknet_member_remove (
  1368. void *knet_context,
  1369. const struct totem_ip_address *token_target,
  1370. int link_no)
  1371. {
  1372. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1373. int res;
  1374. uint8_t link_list[KNET_MAX_LINK];
  1375. size_t num_links;
  1376. knet_log_printf (LOGSYS_LEVEL_DEBUG, "knet: member_remove: " CS_PRI_NODE_ID ", link=%d", token_target->nodeid, link_no);
  1377. /* Don't remove the link with the loopback on it until we shut down */
  1378. if (token_target->nodeid == instance->our_nodeid) {
  1379. return 0;
  1380. }
  1381. /* Tidy stats */
  1382. stats_knet_del_member(token_target->nodeid, link_no);
  1383. /* Remove the link first */
  1384. res = knet_link_set_enable(instance->knet_handle, token_target->nodeid, link_no, 0);
  1385. if (res != 0) {
  1386. 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);
  1387. return res;
  1388. }
  1389. res = knet_link_clear_config(instance->knet_handle, token_target->nodeid, link_no);
  1390. if (res != 0) {
  1391. 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);
  1392. return res;
  1393. }
  1394. /* If this is the last link, then remove the node */
  1395. res = knet_link_get_link_list(instance->knet_handle,
  1396. token_target->nodeid, link_list, &num_links);
  1397. if (res) {
  1398. return (0); /* not really failure */
  1399. }
  1400. if (num_links == 0) {
  1401. res = knet_host_remove(instance->knet_handle, token_target->nodeid);
  1402. }
  1403. return res;
  1404. }
  1405. int totemknet_member_list_rebind_ip (
  1406. void *knet_context)
  1407. {
  1408. return (0);
  1409. }
  1410. static int totemknet_configure_compression (
  1411. struct totemknet_instance *instance,
  1412. struct totem_config *totem_config)
  1413. {
  1414. struct knet_handle_compress_cfg compress_cfg;
  1415. int res = 0;
  1416. assert(strlen(totem_config->knet_compression_model) < sizeof(compress_cfg.compress_model));
  1417. strcpy(compress_cfg.compress_model, totem_config->knet_compression_model);
  1418. compress_cfg.compress_threshold = totem_config->knet_compression_threshold;
  1419. compress_cfg.compress_level = totem_config->knet_compression_level;
  1420. res = knet_handle_compress(instance->knet_handle, &compress_cfg);
  1421. if (res) {
  1422. KNET_LOGSYS_PERROR(errno, LOGSYS_LEVEL_ERROR, "knet_handle_compress failed");
  1423. }
  1424. return res;
  1425. }
  1426. int totemknet_reconfigure (
  1427. void *knet_context,
  1428. struct totem_config *totem_config)
  1429. {
  1430. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1431. int res = 0;
  1432. (void)totemknet_configure_compression(instance, totem_config);
  1433. #ifdef HAVE_LIBNOZZLE
  1434. /* Set up nozzle device(s). Return code is ignored, because inability
  1435. * configure nozzle is not fatal problem, errors are logged and
  1436. * there is not much else we can do */
  1437. (void)setup_nozzle(instance);
  1438. #endif
  1439. if (totem_config->crypto_changed) {
  1440. /* Flip crypto_index */
  1441. totem_config->crypto_index = 3-totem_config->crypto_index;
  1442. res = totemknet_set_knet_crypto(instance);
  1443. knet_log_printf(LOG_INFO, "kronosnet crypto reconfigured on index %d: %s/%s/%s", totem_config->crypto_index,
  1444. totem_config->crypto_model,
  1445. totem_config->crypto_cipher_type,
  1446. totem_config->crypto_hash_type);
  1447. }
  1448. return (res);
  1449. }
  1450. int totemknet_crypto_reconfigure_phase (
  1451. void *knet_context,
  1452. struct totem_config *totem_config,
  1453. cfg_message_crypto_reconfig_phase_t phase)
  1454. {
  1455. #ifdef HAVE_KNET_CRYPTO_RECONF
  1456. int res;
  1457. int config_to_use;
  1458. int config_to_clear;
  1459. struct knet_handle_crypto_cfg crypto_cfg;
  1460. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1461. knet_log_printf(LOGSYS_LEVEL_DEBUG, "totemknet_crypto_reconfigure_phase %d, index=%d\n", phase, totem_config->crypto_index);
  1462. switch (phase) {
  1463. case CRYPTO_RECONFIG_PHASE_ACTIVATE:
  1464. config_to_use = totem_config->crypto_index;
  1465. if (!totemknet_is_crypto_enabled(instance)) {
  1466. config_to_use = 0; /* we are clearing it */
  1467. }
  1468. /* Enable the new config on this node */
  1469. res = knet_handle_crypto_use_config(instance->knet_handle, config_to_use);
  1470. if (res == -1) {
  1471. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_use_config %d failed: %s", config_to_use, strerror(errno));
  1472. }
  1473. break;
  1474. case CRYPTO_RECONFIG_PHASE_CLEANUP:
  1475. /*
  1476. * All nodes should now have the new config. clear the old one out
  1477. * OR disable crypto entirely if that's what the new config insists on.
  1478. */
  1479. config_to_clear = 3-totem_config->crypto_index;
  1480. knet_log_printf(LOGSYS_LEVEL_DEBUG, "Clearing old knet crypto config %d\n", config_to_clear);
  1481. strcpy(crypto_cfg.crypto_model, "none");
  1482. strcpy(crypto_cfg.crypto_cipher_type, "none");
  1483. strcpy(crypto_cfg.crypto_hash_type, "none");
  1484. res = knet_handle_crypto_set_config(instance->knet_handle, &crypto_cfg, config_to_clear);
  1485. if (res == -1) {
  1486. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: %s", config_to_clear, strerror(errno));
  1487. }
  1488. if (res == -2) {
  1489. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_set_config to clear index %d failed: -2", config_to_clear);
  1490. }
  1491. /* If crypto is enabled then disable all cleartext reception */
  1492. if (totemknet_is_crypto_enabled(instance)) {
  1493. res = knet_handle_crypto_rx_clear_traffic(instance->knet_handle, KNET_CRYPTO_RX_DISALLOW_CLEAR_TRAFFIC);
  1494. if (res) {
  1495. knet_log_printf(LOGSYS_LEVEL_ERROR, "knet_handle_crypto_rx_clear_traffic(DISALLOW) failed %s", strerror(errno));
  1496. }
  1497. }
  1498. }
  1499. #endif
  1500. return 0;
  1501. }
  1502. void totemknet_stats_clear (
  1503. void *knet_context)
  1504. {
  1505. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1506. (void) knet_handle_clear_stats(instance->knet_handle, KNET_CLEARSTATS_HANDLE_AND_LINK);
  1507. }
  1508. /* For the stats module */
  1509. int totemknet_link_get_status (
  1510. knet_node_id_t node, uint8_t link_no,
  1511. struct knet_link_status *status)
  1512. {
  1513. int res;
  1514. int ret = CS_OK;
  1515. /* We are probably not using knet */
  1516. if (!global_instance) {
  1517. return CS_ERR_NOT_EXIST;
  1518. }
  1519. if (link_no >= INTERFACE_MAX) {
  1520. return CS_ERR_NOT_EXIST; /* Invalid link number */
  1521. }
  1522. res = knet_link_get_status(global_instance->knet_handle, node, link_no, status, sizeof(struct knet_link_status));
  1523. if (res) {
  1524. switch (errno) {
  1525. case EINVAL:
  1526. ret = CS_ERR_INVALID_PARAM;
  1527. break;
  1528. case EBUSY:
  1529. ret = CS_ERR_BUSY;
  1530. break;
  1531. case EDEADLK:
  1532. ret = CS_ERR_TRY_AGAIN;
  1533. break;
  1534. default:
  1535. ret = CS_ERR_LIBRARY;
  1536. break;
  1537. }
  1538. }
  1539. return (ret);
  1540. }
  1541. int totemknet_handle_get_stats (
  1542. struct knet_handle_stats *stats)
  1543. {
  1544. int res;
  1545. /* We are probably not using knet */
  1546. if (!global_instance) {
  1547. return CS_ERR_NOT_EXIST;
  1548. }
  1549. res = knet_handle_get_stats(global_instance->knet_handle, stats, sizeof(struct knet_handle_stats));
  1550. if (res != 0) {
  1551. return (qb_to_cs_error(-errno));
  1552. }
  1553. return CS_OK;
  1554. }
  1555. static void timer_function_merge_detect_timeout (
  1556. void *data)
  1557. {
  1558. struct totemknet_instance *instance = (struct totemknet_instance *)data;
  1559. if (instance->merge_detect_messages_sent_before_timeout == 0) {
  1560. instance->send_merge_detect_message = 1;
  1561. }
  1562. instance->merge_detect_messages_sent_before_timeout = 0;
  1563. totemknet_start_merge_detect_timeout(instance);
  1564. }
  1565. static void totemknet_start_merge_detect_timeout(
  1566. void *knet_context)
  1567. {
  1568. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1569. qb_loop_timer_add(instance->poll_handle,
  1570. QB_LOOP_MED,
  1571. instance->totem_config->merge_timeout * 2 * QB_TIME_NS_IN_MSEC,
  1572. (void *)instance,
  1573. timer_function_merge_detect_timeout,
  1574. &instance->timer_merge_detect_timeout);
  1575. }
  1576. static void totemknet_stop_merge_detect_timeout(
  1577. void *knet_context)
  1578. {
  1579. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1580. qb_loop_timer_del(instance->poll_handle,
  1581. instance->timer_merge_detect_timeout);
  1582. }
  1583. static void log_flush_messages (void *knet_context)
  1584. {
  1585. struct pollfd pfd;
  1586. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1587. int cont;
  1588. cont = 1;
  1589. while (cont) {
  1590. pfd.fd = instance->logpipes[0];
  1591. pfd.events = POLLIN;
  1592. pfd.revents = 0;
  1593. if ((poll(&pfd, 1, 0) > 0) &&
  1594. (pfd.revents & POLLIN) &&
  1595. (log_deliver_fn(instance->logpipes[0], POLLIN, instance) == 0)) {
  1596. cont = 1;
  1597. } else {
  1598. cont = 0;
  1599. }
  1600. }
  1601. }
  1602. #ifdef HAVE_LIBNOZZLE
  1603. #define NOZZLE_NAME "nozzle.name"
  1604. #define NOZZLE_IPADDR "nozzle.ipaddr"
  1605. #define NOZZLE_PREFIX "nozzle.ipprefix"
  1606. #define NOZZLE_MACADDR "nozzle.macaddr"
  1607. #define NOZZLE_CHANNEL 1
  1608. static char *get_nozzle_script_dir(void *knet_context)
  1609. {
  1610. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1611. char filename[PATH_MAX + FILENAME_MAX + 1];
  1612. static char updown_dirname[PATH_MAX + FILENAME_MAX + 1];
  1613. int res;
  1614. const char *dirname_res;
  1615. /*
  1616. * Build script directory based on corosync.conf file location
  1617. */
  1618. res = snprintf(filename, sizeof(filename), "%s",
  1619. corosync_get_config_file());
  1620. if (res >= sizeof(filename)) {
  1621. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1622. return NULL;
  1623. }
  1624. dirname_res = dirname(filename);
  1625. res = snprintf(updown_dirname, sizeof(updown_dirname), "%s/%s",
  1626. dirname_res, "updown.d");
  1627. if (res >= sizeof(updown_dirname)) {
  1628. knet_log_printf (LOGSYS_LEVEL_DEBUG, "nozzle up/down path too long");
  1629. return NULL;
  1630. }
  1631. return updown_dirname;
  1632. }
  1633. /*
  1634. * Deliberately doesn't return the status as caller doesn't care.
  1635. * The result will be logged though
  1636. */
  1637. static void run_nozzle_script(struct totemknet_instance *instance, int type, const char *typename)
  1638. {
  1639. int res;
  1640. char *exec_string;
  1641. res = nozzle_run_updown(instance->nozzle_handle, type, &exec_string);
  1642. if (res == -1 && errno != ENOENT) {
  1643. knet_log_printf (LOGSYS_LEVEL_INFO, "exec nozzle %s script failed: %s", typename, strerror(errno));
  1644. } else if (res == -2) {
  1645. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle %s script failed", typename);
  1646. knet_log_printf (LOGSYS_LEVEL_INFO, "%s", exec_string);
  1647. }
  1648. }
  1649. /*
  1650. * Reparse IP address to add in our node ID
  1651. * IPv6 addresses must end in '::'
  1652. * IPv4 addresses must just be valid
  1653. * '/xx' lengths are optional for IPv6, mandatory for IPv4
  1654. *
  1655. * Returns the modified IP address as a string to pass into libnozzle
  1656. */
  1657. static int reparse_nozzle_ip_address(struct totemknet_instance *instance,
  1658. const char *input_addr,
  1659. const char *prefix, int nodeid,
  1660. char *output_addr, size_t output_len)
  1661. {
  1662. char *coloncolon;
  1663. int bits;
  1664. int max_prefix = 64;
  1665. uint32_t nodeid_mask;
  1666. uint32_t addr_mask;
  1667. uint32_t masked_nodeid;
  1668. struct in_addr *addr;
  1669. struct totem_ip_address totemip;
  1670. coloncolon = strstr(input_addr, "::");
  1671. if (!coloncolon) {
  1672. max_prefix = 30;
  1673. }
  1674. bits = atoi(prefix);
  1675. if (bits < 8 || bits > max_prefix) {
  1676. knet_log_printf(LOGSYS_LEVEL_ERROR, "nozzle IP address prefix must be >= 8 and <= %d (got %d)", max_prefix, bits);
  1677. return -1;
  1678. }
  1679. /* IPv6 is easy */
  1680. if (coloncolon) {
  1681. memcpy(output_addr, input_addr, coloncolon-input_addr);
  1682. sprintf(output_addr + (coloncolon-input_addr), "::%x", nodeid);
  1683. return 0;
  1684. }
  1685. /* For IPv4 we need to parse the address into binary, mask off the required bits,
  1686. * add in the masked_nodeid and 'print' it out again
  1687. */
  1688. nodeid_mask = UINT32_MAX & ((1<<(32 - bits)) - 1);
  1689. addr_mask = UINT32_MAX ^ nodeid_mask;
  1690. masked_nodeid = nodeid & nodeid_mask;
  1691. if (totemip_parse(&totemip, input_addr, AF_INET)) {
  1692. knet_log_printf(LOGSYS_LEVEL_ERROR, "Failed to parse IPv4 nozzle IP address");
  1693. return -1;
  1694. }
  1695. addr = (struct in_addr *)&totemip.addr;
  1696. addr->s_addr &= htonl(addr_mask);
  1697. addr->s_addr |= htonl(masked_nodeid);
  1698. inet_ntop(AF_INET, addr, output_addr, output_len);
  1699. return 0;
  1700. }
  1701. static int create_nozzle_device(void *knet_context, const char *name,
  1702. const char *ipaddr, const char *prefix,
  1703. const char *macaddr)
  1704. {
  1705. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1706. char device_name[IFNAMSIZ+1];
  1707. size_t size = IFNAMSIZ;
  1708. int8_t channel = NOZZLE_CHANNEL;
  1709. nozzle_t nozzle_dev;
  1710. int nozzle_fd;
  1711. int res;
  1712. char *updown_dir;
  1713. char parsed_ipaddr[INET6_ADDRSTRLEN];
  1714. char mac[19];
  1715. memset(device_name, 0, size);
  1716. memset(&mac, 0, sizeof(mac));
  1717. strncpy(device_name, name, size);
  1718. updown_dir = get_nozzle_script_dir(knet_context);
  1719. knet_log_printf (LOGSYS_LEVEL_INFO, "nozzle script dir is %s", updown_dir);
  1720. nozzle_dev = nozzle_open(device_name, size, updown_dir);
  1721. if (!nozzle_dev) {
  1722. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to init nozzle device %s: %s", device_name, strerror(errno));
  1723. return -1;
  1724. }
  1725. instance->nozzle_handle = nozzle_dev;
  1726. if (nozzle_set_mac(nozzle_dev, macaddr) < 0) {
  1727. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle MAC to %s: %s", mac, strerror(errno));
  1728. goto out_clean;
  1729. }
  1730. if (reparse_nozzle_ip_address(instance, ipaddr, prefix, instance->our_nodeid, parsed_ipaddr, sizeof(parsed_ipaddr))) {
  1731. /* Prints its own errors */
  1732. goto out_clean;
  1733. }
  1734. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle IP address is %s / %d", parsed_ipaddr, atoi(prefix));
  1735. if (nozzle_add_ip(nozzle_dev, parsed_ipaddr, prefix) < 0) {
  1736. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add set nozzle IP addr to %s/%s: %s", parsed_ipaddr, prefix, strerror(errno));
  1737. goto out_clean;
  1738. }
  1739. nozzle_fd = nozzle_get_fd(nozzle_dev);
  1740. knet_log_printf (LOGSYS_LEVEL_INFO, "Opened '%s' on fd %d", device_name, nozzle_fd);
  1741. res = knet_handle_add_datafd(instance->knet_handle, &nozzle_fd, &channel);
  1742. if (res != 0) {
  1743. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to add nozzle FD to knet: %s", strerror(errno));
  1744. goto out_clean;
  1745. }
  1746. run_nozzle_script(instance, NOZZLE_PREUP, "pre-up");
  1747. res = nozzle_set_up(nozzle_dev);
  1748. if (res != 0) {
  1749. knet_log_printf (LOGSYS_LEVEL_ERROR, "Unable to set nozzle interface UP: %s", strerror(errno));
  1750. goto out_clean;
  1751. }
  1752. run_nozzle_script(instance, NOZZLE_UP, "up");
  1753. return 0;
  1754. out_clean:
  1755. nozzle_close(nozzle_dev);
  1756. return -1;
  1757. }
  1758. static int remove_nozzle_device(void *knet_context)
  1759. {
  1760. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1761. int res;
  1762. int datafd;
  1763. res = knet_handle_get_datafd(instance->knet_handle, NOZZLE_CHANNEL, &datafd);
  1764. if (res != 0) {
  1765. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't find datafd for channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1766. return -1;
  1767. }
  1768. res = knet_handle_remove_datafd(instance->knet_handle, datafd);
  1769. if (res != 0) {
  1770. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't remove datafd for nozzle channel %d: %s", NOZZLE_CHANNEL, strerror(errno));
  1771. return -1;
  1772. }
  1773. run_nozzle_script(instance, NOZZLE_DOWN, "pre-down");
  1774. res = nozzle_set_down(instance->nozzle_handle);
  1775. if (res != 0) {
  1776. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't set nozzle device down: %s", strerror(errno));
  1777. return -1;
  1778. }
  1779. run_nozzle_script(instance, NOZZLE_POSTDOWN, "post-down");
  1780. res = nozzle_close(instance->nozzle_handle);
  1781. if (res != 0) {
  1782. knet_log_printf (LOGSYS_LEVEL_ERROR, "Can't close nozzle device: %s", strerror(errno));
  1783. return -1;
  1784. }
  1785. knet_log_printf (LOGSYS_LEVEL_INFO, "Removed nozzle device");
  1786. return 0;
  1787. }
  1788. static void free_nozzle(struct totemknet_instance *instance)
  1789. {
  1790. free(instance->nozzle_name);
  1791. free(instance->nozzle_ipaddr);
  1792. free(instance->nozzle_prefix);
  1793. free(instance->nozzle_macaddr);
  1794. instance->nozzle_name = instance->nozzle_ipaddr = instance->nozzle_prefix =
  1795. instance->nozzle_macaddr = NULL;
  1796. }
  1797. static int setup_nozzle(void *knet_context)
  1798. {
  1799. struct totemknet_instance *instance = (struct totemknet_instance *)knet_context;
  1800. char *ipaddr_str = NULL;
  1801. char *name_str = NULL;
  1802. char *prefix_str = NULL;
  1803. char *macaddr_str = NULL;
  1804. char mac[32];
  1805. int name_res;
  1806. int macaddr_res;
  1807. int res = -1;
  1808. /*
  1809. * Return value ignored on purpose. icmap_get_string changes
  1810. * ipaddr_str/prefix_str only on success.
  1811. */
  1812. (void)icmap_get_string(NOZZLE_IPADDR, &ipaddr_str);
  1813. (void)icmap_get_string(NOZZLE_PREFIX, &prefix_str);
  1814. macaddr_res = icmap_get_string(NOZZLE_MACADDR, &macaddr_str);
  1815. name_res = icmap_get_string(NOZZLE_NAME, &name_str);
  1816. /* Is is being removed? */
  1817. if (name_res == CS_ERR_NOT_EXIST && instance->nozzle_handle) {
  1818. remove_nozzle_device(instance);
  1819. free_nozzle(instance);
  1820. goto out_free;
  1821. }
  1822. if (!name_str) {
  1823. /* no nozzle */
  1824. goto out_free;
  1825. }
  1826. if (!ipaddr_str) {
  1827. knet_log_printf (LOGSYS_LEVEL_ERROR, "No IP address supplied for Nozzle device");
  1828. goto out_free;
  1829. }
  1830. if (!prefix_str) {
  1831. knet_log_printf (LOGSYS_LEVEL_ERROR, "No prefix supplied for Nozzle IP address");
  1832. goto out_free;
  1833. }
  1834. if (macaddr_str && strlen(macaddr_str) != 17) {
  1835. knet_log_printf (LOGSYS_LEVEL_ERROR, "macaddr for nozzle device is not in the correct format '%s'", macaddr_str);
  1836. goto out_free;
  1837. }
  1838. if (!macaddr_str) {
  1839. macaddr_str = (char*)"54:54:01:00:00:00";
  1840. }
  1841. if (instance->nozzle_name &&
  1842. (strcmp(name_str, instance->nozzle_name) == 0) &&
  1843. (strcmp(ipaddr_str, instance->nozzle_ipaddr) == 0) &&
  1844. (strcmp(prefix_str, instance->nozzle_prefix) == 0) &&
  1845. (instance->nozzle_macaddr == NULL ||
  1846. strcmp(macaddr_str, instance->nozzle_macaddr) == 0)) {
  1847. /* Nothing has changed */
  1848. knet_log_printf (LOGSYS_LEVEL_DEBUG, "Nozzle device info not changed");
  1849. goto out_free;
  1850. }
  1851. /* Add nodeid into MAC address */
  1852. memcpy(mac, macaddr_str, 12);
  1853. snprintf(mac+12, sizeof(mac) - 13, "%02x:%02x",
  1854. instance->our_nodeid >> 8,
  1855. instance->our_nodeid & 0xFF);
  1856. knet_log_printf (LOGSYS_LEVEL_INFO, "Local nozzle MAC address is %s", mac);
  1857. if (name_res == CS_OK && name_str) {
  1858. /* Reconfigure */
  1859. if (instance->nozzle_name) {
  1860. remove_nozzle_device(instance);
  1861. free_nozzle(instance);
  1862. }
  1863. res = create_nozzle_device(knet_context, name_str, ipaddr_str, prefix_str,
  1864. mac);
  1865. instance->nozzle_name = strdup(name_str);
  1866. instance->nozzle_ipaddr = strdup(ipaddr_str);
  1867. instance->nozzle_prefix = strdup(prefix_str);
  1868. instance->nozzle_macaddr = strdup(macaddr_str);
  1869. if (!instance->nozzle_name || !instance->nozzle_ipaddr ||
  1870. !instance->nozzle_prefix) {
  1871. knet_log_printf (LOGSYS_LEVEL_ERROR, "strdup failed in nozzle allocation");
  1872. /*
  1873. * This 'free' will cause a complete reconfigure of the device next time we reload
  1874. * but will also let the the current device keep working until then.
  1875. * remove_nozzle() only needs the, statically-allocated, nozzle_handle
  1876. */
  1877. free_nozzle(instance);
  1878. }
  1879. }
  1880. out_free:
  1881. free(name_str);
  1882. free(ipaddr_str);
  1883. free(prefix_str);
  1884. if (macaddr_res == CS_OK) {
  1885. free(macaddr_str);
  1886. }
  1887. return res;
  1888. }
  1889. #endif // HAVE_LIBNOZZLE