totemknet.c 53 KB

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