totemknet.c 54 KB

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