totemknet.c 55 KB

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