totemknet.c 53 KB

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