totemknet.c 54 KB

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