totemknet.c 52 KB

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