Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1 | /* |
| 2 | * mc_socket.c: socket based multicast for vlib mc |
| 3 | * |
| 4 | * Copyright (c) 2010 Cisco and/or its affiliates. |
| 5 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 6 | * you may not use this file except in compliance with the License. |
| 7 | * You may obtain a copy of the License at: |
| 8 | * |
| 9 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 10 | * |
| 11 | * Unless required by applicable law or agreed to in writing, software |
| 12 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 13 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 14 | * See the License for the specific language governing permissions and |
| 15 | * limitations under the License. |
| 16 | */ |
| 17 | |
| 18 | #include <vlib/vlib.h> |
| 19 | #include <vlib/unix/mc_socket.h> |
| 20 | |
| 21 | #include <sys/ioctl.h> /* for FIONBIO */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 22 | #include <netinet/tcp.h> /* for TCP_NODELAY */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 23 | #include <net/if.h> /* for struct ifreq */ |
| 24 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 25 | static u8 * |
| 26 | format_socket_peer_id (u8 * s, va_list * args) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 27 | { |
| 28 | u64 peer_id_as_u64 = va_arg (*args, u64); |
| 29 | mc_peer_id_t peer_id; |
| 30 | peer_id.as_u64 = peer_id_as_u64; |
| 31 | u32 a = mc_socket_peer_id_get_address (peer_id); |
| 32 | u32 p = mc_socket_peer_id_get_port (peer_id); |
| 33 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 34 | s = format (s, "%U:%04x", format_network_address, AF_INET, &a, ntohs (p)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 35 | |
| 36 | return s; |
| 37 | } |
| 38 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 39 | typedef void (mc_msg_handler_t) (mc_main_t * mcm, void *msg, |
| 40 | u32 buffer_index); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 41 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 42 | always_inline void |
| 43 | msg_handler (mc_main_t * mcm, |
| 44 | u32 buffer_index, u32 handler_frees_buffer, void *_h) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 45 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 46 | vlib_main_t *vm = mcm->vlib_main; |
| 47 | mc_msg_handler_t *h = _h; |
| 48 | vlib_buffer_t *b = vlib_get_buffer (vm, buffer_index); |
| 49 | void *the_msg = vlib_buffer_get_current (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 50 | |
| 51 | h (mcm, the_msg, buffer_index); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 52 | if (!handler_frees_buffer) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 53 | vlib_buffer_free_one (vm, buffer_index); |
| 54 | } |
| 55 | |
| 56 | static uword |
| 57 | append_buffer_index_to_iovec (vlib_main_t * vm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 58 | u32 buffer_index, struct iovec **iovs_return) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 59 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 60 | struct iovec *i; |
| 61 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 62 | u32 bi = buffer_index; |
| 63 | u32 l = 0; |
| 64 | |
| 65 | while (1) |
| 66 | { |
| 67 | b = vlib_get_buffer (vm, bi); |
| 68 | vec_add2 (*iovs_return, i, 1); |
| 69 | i->iov_base = vlib_buffer_get_current (b); |
| 70 | i->iov_len = b->current_length; |
| 71 | l += i->iov_len; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 72 | if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 73 | break; |
| 74 | bi = b->next_buffer; |
| 75 | } |
| 76 | |
| 77 | return l; |
| 78 | } |
| 79 | |
| 80 | static clib_error_t * |
| 81 | sendmsg_helper (mc_socket_main_t * msm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 82 | int socket, struct sockaddr_in *tx_addr, u32 buffer_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 83 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 84 | vlib_main_t *vm = msm->mc_main.vlib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 85 | struct msghdr h; |
| 86 | word n_bytes, n_bytes_tx, n_retries; |
| 87 | |
| 88 | memset (&h, 0, sizeof (h)); |
| 89 | h.msg_name = tx_addr; |
| 90 | h.msg_namelen = sizeof (tx_addr[0]); |
| 91 | |
| 92 | if (msm->iovecs) |
| 93 | _vec_len (msm->iovecs) = 0; |
| 94 | |
| 95 | n_bytes = append_buffer_index_to_iovec (vm, buffer_index, &msm->iovecs); |
| 96 | ASSERT (n_bytes <= msm->mc_main.transport.max_packet_size); |
| 97 | if (n_bytes > msm->mc_main.transport.max_packet_size) |
| 98 | clib_error ("sending packet larger than interace MTU %d bytes", n_bytes); |
| 99 | |
| 100 | h.msg_iov = msm->iovecs; |
| 101 | h.msg_iovlen = vec_len (msm->iovecs); |
| 102 | |
| 103 | n_retries = 0; |
| 104 | while ((n_bytes_tx = sendmsg (socket, &h, /* flags */ 0)) != n_bytes |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 105 | && errno == EAGAIN) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 106 | n_retries++; |
| 107 | if (n_bytes_tx != n_bytes) |
| 108 | { |
| 109 | clib_unix_warning ("sendmsg"); |
| 110 | return 0; |
| 111 | } |
| 112 | if (n_retries) |
| 113 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 114 | ELOG_TYPE_DECLARE (e) = |
| 115 | { |
| 116 | .format = "sendmsg-helper: %d retries",.format_args = "i4",}; |
| 117 | struct |
| 118 | { |
| 119 | u32 retries; |
| 120 | } *ed = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 121 | |
| 122 | ed = ELOG_DATA (&vm->elog_main, e); |
| 123 | ed->retries = n_retries; |
| 124 | } |
| 125 | return 0; |
| 126 | } |
| 127 | |
| 128 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 129 | tx_buffer (void *transport, mc_transport_type_t type, u32 buffer_index) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 130 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 131 | mc_socket_main_t *msm = (mc_socket_main_t *) transport; |
| 132 | vlib_main_t *vm = msm->mc_main.vlib_main; |
| 133 | mc_multicast_socket_t *ms = &msm->multicast_sockets[type]; |
| 134 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 135 | error = sendmsg_helper (msm, ms->socket, &ms->tx_addr, buffer_index); |
| 136 | if (type != MC_TRANSPORT_USER_REQUEST_TO_RELAY) |
| 137 | vlib_buffer_free_one (vm, buffer_index); |
| 138 | return error; |
| 139 | } |
| 140 | |
| 141 | static clib_error_t * |
| 142 | tx_ack (void *transport, mc_peer_id_t dest_peer_id, u32 buffer_index) |
| 143 | { |
| 144 | struct sockaddr_in tx_addr; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 145 | mc_socket_main_t *msm = (mc_socket_main_t *) transport; |
| 146 | vlib_main_t *vm = msm->mc_main.vlib_main; |
| 147 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 148 | |
| 149 | memset (&tx_addr, 0, sizeof (tx_addr)); |
| 150 | tx_addr.sin_family = AF_INET; |
| 151 | tx_addr.sin_addr.s_addr = mc_socket_peer_id_get_address (dest_peer_id); |
| 152 | tx_addr.sin_port = mc_socket_peer_id_get_port (dest_peer_id); |
| 153 | |
| 154 | error = sendmsg_helper (msm, msm->ack_socket, &tx_addr, buffer_index); |
| 155 | vlib_buffer_free_one (vm, buffer_index); |
| 156 | return error; |
| 157 | } |
| 158 | |
| 159 | static clib_error_t * |
| 160 | recvmsg_helper (mc_socket_main_t * msm, |
| 161 | int socket, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 162 | struct sockaddr_in *rx_addr, |
| 163 | u32 * buffer_index, u32 drop_message) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 164 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 165 | vlib_main_t *vm = msm->mc_main.vlib_main; |
| 166 | vlib_buffer_t *b; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 167 | uword n_left, n_alloc, n_mtu, i, i_rx; |
| 168 | const uword buffer_size = VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; |
| 169 | word n_bytes_left; |
| 170 | |
| 171 | /* Make sure we have at least a MTU worth of buffers. */ |
| 172 | n_mtu = msm->rx_mtu_n_buffers; |
| 173 | n_left = vec_len (msm->rx_buffers); |
| 174 | if (n_left < n_mtu) |
| 175 | { |
| 176 | uword max_alloc = 8 * n_mtu; |
| 177 | vec_validate (msm->rx_buffers, max_alloc - 1); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 178 | n_alloc = |
| 179 | vlib_buffer_alloc (vm, msm->rx_buffers + n_left, max_alloc - n_left); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 180 | _vec_len (msm->rx_buffers) = n_left + n_alloc; |
| 181 | } |
| 182 | |
| 183 | ASSERT (vec_len (msm->rx_buffers) >= n_mtu); |
| 184 | vec_validate (msm->iovecs, n_mtu - 1); |
| 185 | |
| 186 | /* Allocate RX buffers from end of rx_buffers. |
| 187 | Turn them into iovecs to pass to readv. */ |
| 188 | i_rx = vec_len (msm->rx_buffers) - 1; |
| 189 | for (i = 0; i < n_mtu; i++) |
| 190 | { |
| 191 | b = vlib_get_buffer (vm, msm->rx_buffers[i_rx - i]); |
| 192 | msm->iovecs[i].iov_base = b->data; |
| 193 | msm->iovecs[i].iov_len = buffer_size; |
| 194 | } |
| 195 | _vec_len (msm->iovecs) = n_mtu; |
| 196 | |
| 197 | { |
| 198 | struct msghdr h; |
| 199 | |
| 200 | memset (&h, 0, sizeof (h)); |
| 201 | if (rx_addr) |
| 202 | { |
| 203 | h.msg_name = rx_addr; |
| 204 | h.msg_namelen = sizeof (rx_addr[0]); |
| 205 | } |
| 206 | h.msg_iov = msm->iovecs; |
| 207 | h.msg_iovlen = vec_len (msm->iovecs); |
| 208 | |
| 209 | n_bytes_left = recvmsg (socket, &h, 0); |
| 210 | if (n_bytes_left < 0) |
| 211 | return clib_error_return_unix (0, "recvmsg"); |
| 212 | } |
| 213 | |
| 214 | if (drop_message) |
| 215 | { |
| 216 | *buffer_index = ~0; |
| 217 | return 0; |
| 218 | } |
| 219 | |
| 220 | *buffer_index = msm->rx_buffers[i_rx]; |
| 221 | while (1) |
| 222 | { |
| 223 | b = vlib_get_buffer (vm, msm->rx_buffers[i_rx]); |
| 224 | |
| 225 | b->flags = 0; |
| 226 | b->current_data = 0; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 227 | b->current_length = |
| 228 | n_bytes_left < buffer_size ? n_bytes_left : buffer_size; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 229 | |
| 230 | n_bytes_left -= buffer_size; |
| 231 | |
| 232 | if (n_bytes_left <= 0) |
| 233 | break; |
| 234 | |
| 235 | i_rx--; |
| 236 | b->flags |= VLIB_BUFFER_NEXT_PRESENT; |
| 237 | b->next_buffer = msm->rx_buffers[i_rx]; |
| 238 | } |
| 239 | |
| 240 | _vec_len (msm->rx_buffers) = i_rx; |
| 241 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 242 | return 0 /* no error */ ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 243 | } |
| 244 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 245 | static clib_error_t * |
| 246 | mastership_socket_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 247 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 248 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 249 | mc_main_t *mcm = &msm->mc_main; |
| 250 | mc_multicast_socket_t *ms = |
| 251 | &msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP]; |
| 252 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 253 | u32 bi; |
| 254 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 255 | error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ |
| 256 | 0); |
| 257 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 258 | msg_handler (mcm, bi, |
| 259 | /* handler_frees_buffer */ 0, |
| 260 | mc_msg_master_assert_handler); |
| 261 | |
| 262 | return error; |
| 263 | } |
| 264 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 265 | static clib_error_t * |
| 266 | to_relay_socket_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 267 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 268 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 269 | mc_main_t *mcm = &msm->mc_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 270 | vlib_main_t *vm = msm->mc_main.vlib_main; |
| 271 | mc_multicast_socket_t *ms_to_relay = |
| 272 | &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY]; |
| 273 | mc_multicast_socket_t *ms_from_relay = |
| 274 | &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; |
| 275 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 276 | u32 bi; |
| 277 | u32 is_master = mcm->relay_state == MC_RELAY_STATE_MASTER; |
| 278 | |
| 279 | /* Not the ordering master? Turf the msg */ |
| 280 | error = recvmsg_helper (msm, ms_to_relay->socket, /* rx_addr */ 0, &bi, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 281 | /* drop_message */ !is_master); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 282 | |
| 283 | /* If we are the master, number and rebroadcast the msg. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 284 | if (!error && is_master) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 285 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 286 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 287 | mc_msg_user_request_t *mp = vlib_buffer_get_current (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 288 | mp->global_sequence = clib_host_to_net_u32 (mcm->relay_global_sequence); |
| 289 | mcm->relay_global_sequence++; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 290 | error = |
| 291 | sendmsg_helper (msm, ms_from_relay->socket, &ms_from_relay->tx_addr, |
| 292 | bi); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 293 | vlib_buffer_free_one (vm, bi); |
| 294 | } |
| 295 | |
| 296 | return error; |
| 297 | } |
| 298 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 299 | static clib_error_t * |
| 300 | from_relay_socket_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 301 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 302 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 303 | mc_main_t *mcm = &msm->mc_main; |
| 304 | mc_multicast_socket_t *ms = |
| 305 | &msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY]; |
| 306 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 307 | u32 bi; |
| 308 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 309 | error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ |
| 310 | 0); |
| 311 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 312 | { |
| 313 | msg_handler (mcm, bi, /* handler_frees_buffer */ 1, |
| 314 | mc_msg_user_request_handler); |
| 315 | } |
| 316 | return error; |
| 317 | } |
| 318 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 319 | static clib_error_t * |
| 320 | join_socket_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 321 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 322 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 323 | mc_main_t *mcm = &msm->mc_main; |
| 324 | vlib_main_t *vm = mcm->vlib_main; |
| 325 | mc_multicast_socket_t *ms = &msm->multicast_sockets[MC_TRANSPORT_JOIN]; |
| 326 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 327 | u32 bi; |
| 328 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 329 | error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */ |
| 330 | 0); |
| 331 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 332 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 333 | vlib_buffer_t *b = vlib_get_buffer (vm, bi); |
| 334 | mc_msg_join_or_leave_request_t *mp = vlib_buffer_get_current (b); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 335 | |
| 336 | switch (clib_host_to_net_u32 (mp->type)) |
| 337 | { |
| 338 | case MC_MSG_TYPE_join_or_leave_request: |
| 339 | msg_handler (mcm, bi, /* handler_frees_buffer */ 0, |
| 340 | mc_msg_join_or_leave_request_handler); |
| 341 | break; |
| 342 | |
| 343 | case MC_MSG_TYPE_join_reply: |
| 344 | msg_handler (mcm, bi, /* handler_frees_buffer */ 0, |
| 345 | mc_msg_join_reply_handler); |
| 346 | break; |
| 347 | |
| 348 | default: |
| 349 | ASSERT (0); |
| 350 | break; |
| 351 | } |
| 352 | } |
| 353 | return error; |
| 354 | } |
| 355 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 356 | static clib_error_t * |
| 357 | ack_socket_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 358 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 359 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 360 | mc_main_t *mcm = &msm->mc_main; |
| 361 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 362 | u32 bi; |
| 363 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 364 | error = recvmsg_helper (msm, msm->ack_socket, /* rx_addr */ 0, &bi, |
| 365 | /* drop_message */ 0); |
| 366 | if (!error) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 367 | msg_handler (mcm, bi, /* handler_frees_buffer */ 0, |
| 368 | mc_msg_user_ack_handler); |
| 369 | return error; |
| 370 | } |
| 371 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 372 | static void |
| 373 | catchup_cleanup (mc_socket_main_t * msm, |
| 374 | mc_socket_catchup_t * c, unix_main_t * um, unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 375 | { |
| 376 | hash_unset (msm->catchup_index_by_file_descriptor, uf->file_descriptor); |
| 377 | unix_file_del (um, uf); |
| 378 | vec_free (c->input_vector); |
| 379 | vec_free (c->output_vector); |
| 380 | pool_put (msm->catchups, c); |
| 381 | } |
| 382 | |
| 383 | static mc_socket_catchup_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 384 | find_catchup_from_file_descriptor (mc_socket_main_t * msm, |
| 385 | int file_descriptor) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 386 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 387 | uword *p = |
| 388 | hash_get (msm->catchup_index_by_file_descriptor, file_descriptor); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 389 | return p ? pool_elt_at_index (msm->catchups, p[0]) : 0; |
| 390 | } |
| 391 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 392 | static clib_error_t * |
| 393 | catchup_socket_read_ready (unix_file_t * uf, int is_server) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 394 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 395 | unix_main_t *um = &unix_main; |
| 396 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 397 | mc_main_t *mcm = &msm->mc_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 398 | mc_socket_catchup_t *c = |
| 399 | find_catchup_from_file_descriptor (msm, uf->file_descriptor); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 400 | word l, n, is_eof; |
| 401 | |
| 402 | l = vec_len (c->input_vector); |
| 403 | vec_resize (c->input_vector, 4096); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 404 | n = |
| 405 | read (uf->file_descriptor, c->input_vector + l, |
| 406 | vec_len (c->input_vector) - l); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 407 | is_eof = n == 0; |
| 408 | |
| 409 | if (n < 0) |
| 410 | { |
| 411 | if (errno == EAGAIN) |
| 412 | n = 0; |
| 413 | else |
| 414 | { |
| 415 | catchup_cleanup (msm, c, um, uf); |
| 416 | return clib_error_return_unix (0, "read"); |
| 417 | } |
| 418 | } |
| 419 | |
| 420 | _vec_len (c->input_vector) = l + n; |
| 421 | |
| 422 | if (is_eof && vec_len (c->input_vector) > 0) |
| 423 | { |
| 424 | if (is_server) |
| 425 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 426 | mc_msg_catchup_request_handler (mcm, (void *) c->input_vector, |
| 427 | c - msm->catchups); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 428 | _vec_len (c->input_vector) = 0; |
| 429 | } |
| 430 | else |
| 431 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 432 | mc_msg_catchup_reply_handler (mcm, (void *) c->input_vector, |
| 433 | c - msm->catchups); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 434 | c->input_vector = 0; /* reply handler is responsible for freeing vector */ |
| 435 | catchup_cleanup (msm, c, um, uf); |
| 436 | } |
| 437 | } |
| 438 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 439 | return 0 /* no error */ ; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 440 | } |
| 441 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 442 | static clib_error_t * |
| 443 | catchup_server_read_ready (unix_file_t * uf) |
| 444 | { |
| 445 | return catchup_socket_read_ready (uf, /* is_server */ 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 446 | } |
| 447 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 448 | static clib_error_t * |
| 449 | catchup_client_read_ready (unix_file_t * uf) |
| 450 | { |
| 451 | if (MC_EVENT_LOGGING) |
| 452 | { |
| 453 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 454 | vlib_main_t *vm = msm->mc_main.vlib_main; |
| 455 | |
| 456 | ELOG_TYPE (e, "catchup_client_read_ready"); |
| 457 | ELOG (&vm->elog_main, e, 0); |
| 458 | } |
| 459 | return catchup_socket_read_ready (uf, /* is_server */ 0); |
| 460 | } |
| 461 | |
| 462 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 463 | catchup_socket_write_ready (unix_file_t * uf, int is_server) |
| 464 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 465 | unix_main_t *um = &unix_main; |
| 466 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 467 | mc_socket_catchup_t *c = |
| 468 | find_catchup_from_file_descriptor (msm, uf->file_descriptor); |
| 469 | clib_error_t *error = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 470 | int n; |
| 471 | |
| 472 | if (c->connect_in_progress) |
| 473 | { |
| 474 | u32 len, value; |
| 475 | |
| 476 | c->connect_in_progress = 0; |
| 477 | len = sizeof (value); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 478 | if (getsockopt (c->socket, SOL_SOCKET, SO_ERROR, &value, &len) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 479 | { |
| 480 | error = clib_error_return_unix (0, "getsockopt SO_ERROR"); |
| 481 | goto error_quit; |
| 482 | } |
| 483 | if (value != 0) |
| 484 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 485 | error = |
| 486 | clib_error_return_code (0, value, CLIB_ERROR_ERRNO_VALID, |
| 487 | "connect fails"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 488 | goto error_quit; |
| 489 | } |
| 490 | } |
| 491 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 492 | while (1) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 493 | { |
| 494 | u32 n_this_write; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 495 | |
| 496 | n_this_write = |
| 497 | clib_min (vec_len (c->output_vector) - c->output_vector_n_written, |
| 498 | msm->rx_mtu_n_bytes - |
| 499 | 64 /* ip + tcp + option allowance */ ); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 500 | |
| 501 | if (n_this_write <= 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 502 | break; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 503 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 504 | do |
| 505 | { |
| 506 | n = write (uf->file_descriptor, |
| 507 | c->output_vector + c->output_vector_n_written, |
| 508 | n_this_write); |
| 509 | } |
| 510 | while (n < 0 && errno == EAGAIN); |
| 511 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 512 | if (n < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 513 | { |
| 514 | error = clib_error_return_unix (0, "write"); |
| 515 | goto error_quit; |
| 516 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 517 | c->output_vector_n_written += n; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 518 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 519 | |
| 520 | if (c->output_vector_n_written >= vec_len (c->output_vector)) |
| 521 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 522 | if (!is_server) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 523 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 524 | uf->flags &= ~UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
| 525 | unix_main.file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 526 | /* Send EOF to other side. */ |
| 527 | shutdown (uf->file_descriptor, SHUT_WR); |
| 528 | return error; |
| 529 | } |
| 530 | else |
| 531 | { |
| 532 | error_quit: |
| 533 | catchup_cleanup (msm, c, um, uf); |
| 534 | } |
| 535 | } |
| 536 | return error; |
| 537 | } |
| 538 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 539 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 540 | catchup_server_write_ready (unix_file_t * uf) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 541 | { |
| 542 | return catchup_socket_write_ready (uf, /* is_server */ 1); |
| 543 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 544 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 545 | static clib_error_t * |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 546 | catchup_client_write_ready (unix_file_t * uf) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 547 | { |
| 548 | return catchup_socket_write_ready (uf, /* is_server */ 0); |
| 549 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 550 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 551 | static clib_error_t * |
| 552 | catchup_socket_error_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 553 | { |
| 554 | unix_main_t *um = &unix_main; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 555 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
| 556 | mc_socket_catchup_t *c = |
| 557 | find_catchup_from_file_descriptor (msm, uf->file_descriptor); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 558 | catchup_cleanup (msm, c, um, uf); |
| 559 | return clib_error_return (0, "error"); |
| 560 | } |
| 561 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 562 | static clib_error_t * |
| 563 | catchup_listen_read_ready (unix_file_t * uf) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 564 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 565 | mc_socket_main_t *msm = (mc_socket_main_t *) uf->private_data; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 566 | struct sockaddr_in client_addr; |
| 567 | int client_len; |
| 568 | mc_socket_catchup_t *c; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 569 | unix_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 570 | |
| 571 | pool_get (msm->catchups, c); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 572 | memset (c, 0, sizeof (c[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 573 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 574 | client_len = sizeof (client_addr); |
| 575 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 576 | /* Acquires the non-blocking attrib from the server socket. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 577 | c->socket = accept (uf->file_descriptor, |
| 578 | (struct sockaddr *) &client_addr, |
| 579 | (socklen_t *) & client_len); |
| 580 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 581 | if (c->socket < 0) |
| 582 | { |
| 583 | pool_put (msm->catchups, c); |
| 584 | return clib_error_return_unix (0, "accept"); |
| 585 | } |
| 586 | |
| 587 | if (MC_EVENT_LOGGING) |
| 588 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 589 | mc_main_t *mcm = &msm->mc_main; |
| 590 | vlib_main_t *vm = mcm->vlib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 591 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 592 | ELOG_TYPE_DECLARE (e) = |
| 593 | { |
| 594 | .format = "catchup accepted from 0x%lx",.format_args = "i4",}; |
| 595 | struct |
| 596 | { |
| 597 | u32 addr; |
| 598 | } *ed = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 599 | |
| 600 | ed = ELOG_DATA (&vm->elog_main, e); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 601 | ed->addr = ntohl (client_addr.sin_addr.s_addr); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 602 | } |
| 603 | |
| 604 | /* Disable the Nagle algorithm, ship catchup pkts immediately */ |
| 605 | { |
| 606 | int one = 1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 607 | if ((setsockopt (c->socket, IPPROTO_TCP, |
| 608 | TCP_NODELAY, (void *) &one, sizeof (one))) < 0) |
| 609 | { |
| 610 | clib_unix_warning ("catchup socket: set TCP_NODELAY"); |
| 611 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 612 | } |
| 613 | |
| 614 | template.read_function = catchup_server_read_ready; |
| 615 | template.write_function = catchup_server_write_ready; |
| 616 | template.error_function = catchup_socket_error_ready; |
| 617 | template.file_descriptor = c->socket; |
| 618 | template.private_data = pointer_to_uword (msm); |
| 619 | c->unix_file_index = unix_file_add (&unix_main, &template); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 620 | hash_set (msm->catchup_index_by_file_descriptor, c->socket, |
| 621 | c - msm->catchups); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 622 | |
| 623 | return 0; |
| 624 | } |
| 625 | |
| 626 | /* Return and bind to an unused port. */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 627 | static word |
| 628 | find_and_bind_to_free_port (word sock, word port) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 629 | { |
| 630 | for (; port < 1 << 16; port++) |
| 631 | { |
| 632 | struct sockaddr_in a; |
| 633 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 634 | memset (&a, 0, sizeof (a)); /* Warnings be gone */ |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 635 | |
| 636 | a.sin_family = PF_INET; |
| 637 | a.sin_addr.s_addr = INADDR_ANY; |
| 638 | a.sin_port = htons (port); |
| 639 | |
| 640 | if (bind (sock, (struct sockaddr *) &a, sizeof (a)) >= 0) |
| 641 | break; |
| 642 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 643 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 644 | return port < 1 << 16 ? port : -1; |
| 645 | } |
| 646 | |
| 647 | static clib_error_t * |
| 648 | setup_mutlicast_socket (mc_socket_main_t * msm, |
| 649 | mc_multicast_socket_t * ms, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 650 | char *type, uword udp_port) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 651 | { |
| 652 | int one = 1; |
| 653 | struct ip_mreq mcast_req; |
| 654 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 655 | if (!msm->multicast_ttl) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 656 | msm->multicast_ttl = 1; |
| 657 | |
| 658 | /* mastership (multicast) TX socket */ |
| 659 | if ((ms->socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 660 | return clib_error_return_unix (0, "%s socket", type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 661 | |
| 662 | { |
| 663 | u8 ttl = msm->multicast_ttl; |
| 664 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 665 | if ((setsockopt (ms->socket, IPPROTO_IP, |
| 666 | IP_MULTICAST_TTL, (void *) &ttl, sizeof (ttl))) < 0) |
| 667 | return clib_error_return_unix (0, "%s set multicast ttl", type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 668 | } |
| 669 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 670 | if (setsockopt (ms->socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)) < |
| 671 | 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 672 | return clib_error_return_unix (0, "%s setsockopt SO_REUSEADDR", type); |
| 673 | |
| 674 | memset (&ms->tx_addr, 0, sizeof (ms->tx_addr)); |
| 675 | ms->tx_addr.sin_family = AF_INET; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 676 | ms->tx_addr.sin_addr.s_addr = |
| 677 | htonl (msm->multicast_tx_ip4_address_host_byte_order); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 678 | ms->tx_addr.sin_port = htons (udp_port); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 679 | |
| 680 | if (bind (ms->socket, (struct sockaddr *) &ms->tx_addr, |
| 681 | sizeof (ms->tx_addr)) < 0) |
| 682 | return clib_error_return_unix (0, "%s bind", type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 683 | |
| 684 | memset (&mcast_req, 0, sizeof (mcast_req)); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 685 | mcast_req.imr_multiaddr.s_addr = |
| 686 | htonl (msm->multicast_tx_ip4_address_host_byte_order); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 687 | mcast_req.imr_interface.s_addr = msm->if_ip4_address_net_byte_order; |
| 688 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 689 | if ((setsockopt (ms->socket, IPPROTO_IP, |
| 690 | IP_ADD_MEMBERSHIP, (void *) &mcast_req, |
| 691 | sizeof (mcast_req))) < 0) |
| 692 | return clib_error_return_unix (0, "%s IP_ADD_MEMBERSHIP setsockopt", |
| 693 | type); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 694 | |
| 695 | if (ioctl (ms->socket, FIONBIO, &one) < 0) |
| 696 | return clib_error_return_unix (0, "%s set FIONBIO", type); |
| 697 | |
| 698 | /* FIXME remove this when we support tx_ready. */ |
| 699 | { |
| 700 | u32 len = 1 << 20; |
| 701 | socklen_t sl = sizeof (len); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 702 | if (setsockopt (ms->socket, SOL_SOCKET, SO_SNDBUF, &len, sl) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 703 | clib_unix_error ("setsockopt"); |
| 704 | } |
| 705 | |
| 706 | return 0; |
| 707 | } |
| 708 | |
| 709 | static clib_error_t * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 710 | socket_setup (mc_socket_main_t * msm) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 711 | { |
| 712 | int one = 1; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 713 | clib_error_t *error; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 714 | u32 port; |
| 715 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 716 | if (!msm->base_multicast_udp_port_host_byte_order) |
| 717 | msm->base_multicast_udp_port_host_byte_order = |
| 718 | 0xffff - ((MC_N_TRANSPORT_TYPE + 2 /* ack socket, catchup socket */ ) |
| 719 | - 1); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 720 | |
| 721 | port = msm->base_multicast_udp_port_host_byte_order; |
| 722 | |
| 723 | error = setup_mutlicast_socket (msm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 724 | &msm->multicast_sockets |
| 725 | [MC_TRANSPORT_MASTERSHIP], "mastership", |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 726 | port++); |
| 727 | if (error) |
| 728 | return error; |
| 729 | |
| 730 | error = setup_mutlicast_socket (msm, |
| 731 | &msm->multicast_sockets[MC_TRANSPORT_JOIN], |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 732 | "join", port++); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 733 | if (error) |
| 734 | return error; |
| 735 | |
| 736 | error = setup_mutlicast_socket (msm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 737 | &msm->multicast_sockets |
| 738 | [MC_TRANSPORT_USER_REQUEST_TO_RELAY], |
| 739 | "to relay", port++); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 740 | if (error) |
| 741 | return error; |
| 742 | |
| 743 | error = setup_mutlicast_socket (msm, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 744 | &msm->multicast_sockets |
| 745 | [MC_TRANSPORT_USER_REQUEST_FROM_RELAY], |
| 746 | "from relay", port++); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 747 | if (error) |
| 748 | return error; |
| 749 | |
| 750 | /* ACK rx socket */ |
| 751 | msm->ack_socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP); |
| 752 | if (msm->ack_socket < 0) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 753 | return clib_error_return_unix (0, "ack socket"); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 754 | |
| 755 | msm->ack_udp_port = find_and_bind_to_free_port (msm->ack_socket, port++); |
| 756 | |
| 757 | if (ioctl (msm->ack_socket, FIONBIO, &one) < 0) |
| 758 | return clib_error_return_unix (0, "ack socket FIONBIO"); |
| 759 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 760 | msm->catchup_server_socket = socket (AF_INET, SOCK_STREAM, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 761 | if (msm->catchup_server_socket < 0) |
| 762 | return clib_error_return_unix (0, "catchup server socket"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 763 | |
| 764 | msm->catchup_tcp_port = |
| 765 | find_and_bind_to_free_port (msm->catchup_server_socket, port++); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 766 | |
| 767 | if (ioctl (msm->catchup_server_socket, FIONBIO, &one) < 0) |
| 768 | return clib_error_return_unix (0, "catchup server socket FIONBIO"); |
| 769 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 770 | if (listen (msm->catchup_server_socket, 5) < 0) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 771 | return clib_error_return_unix (0, "catchup server socket listen"); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 772 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 773 | /* epoll setup for multicast mastership socket */ |
| 774 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 775 | unix_file_t template = { 0 }; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 776 | |
| 777 | template.read_function = mastership_socket_read_ready; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 778 | template.file_descriptor = |
| 779 | msm->multicast_sockets[MC_TRANSPORT_MASTERSHIP].socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 780 | template.private_data = (uword) msm; |
| 781 | unix_file_add (&unix_main, &template); |
| 782 | |
| 783 | /* epoll setup for multicast to_relay socket */ |
| 784 | template.read_function = to_relay_socket_read_ready; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 785 | template.file_descriptor = |
| 786 | msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_TO_RELAY].socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 787 | template.private_data = (uword) msm; |
| 788 | unix_file_add (&unix_main, &template); |
| 789 | |
| 790 | /* epoll setup for multicast from_relay socket */ |
| 791 | template.read_function = from_relay_socket_read_ready; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 792 | template.file_descriptor = |
| 793 | msm->multicast_sockets[MC_TRANSPORT_USER_REQUEST_FROM_RELAY].socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 794 | template.private_data = (uword) msm; |
| 795 | unix_file_add (&unix_main, &template); |
| 796 | |
| 797 | template.read_function = join_socket_read_ready; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 798 | template.file_descriptor = |
| 799 | msm->multicast_sockets[MC_TRANSPORT_JOIN].socket; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 800 | template.private_data = (uword) msm; |
| 801 | unix_file_add (&unix_main, &template); |
| 802 | |
| 803 | /* epoll setup for ack rx socket */ |
| 804 | template.read_function = ack_socket_read_ready; |
| 805 | template.file_descriptor = msm->ack_socket; |
| 806 | template.private_data = (uword) msm; |
| 807 | unix_file_add (&unix_main, &template); |
| 808 | |
| 809 | /* epoll setup for TCP catchup server */ |
| 810 | template.read_function = catchup_listen_read_ready; |
| 811 | template.file_descriptor = msm->catchup_server_socket; |
| 812 | template.private_data = (uword) msm; |
| 813 | unix_file_add (&unix_main, &template); |
| 814 | } |
| 815 | |
| 816 | return 0; |
| 817 | } |
| 818 | |
| 819 | static void * |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 820 | catchup_add_pending_output (mc_socket_catchup_t * c, uword n_bytes, |
| 821 | u8 * set_output_vector) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 822 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 823 | unix_file_t *uf = pool_elt_at_index (unix_main.file_pool, |
| 824 | c->unix_file_index); |
| 825 | u8 *result = 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 826 | |
| 827 | if (set_output_vector) |
| 828 | c->output_vector = set_output_vector; |
| 829 | else |
| 830 | vec_add2 (c->output_vector, result, n_bytes); |
| 831 | if (vec_len (c->output_vector) > 0) |
| 832 | { |
| 833 | int skip_update = 0 != (uf->flags & UNIX_FILE_DATA_AVAILABLE_TO_WRITE); |
| 834 | uf->flags |= UNIX_FILE_DATA_AVAILABLE_TO_WRITE; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 835 | if (!skip_update) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 836 | unix_main.file_update (uf, UNIX_FILE_UPDATE_MODIFY); |
| 837 | } |
| 838 | return result; |
| 839 | } |
| 840 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 841 | static uword |
| 842 | catchup_request_fun (void *transport_main, |
| 843 | u32 stream_index, mc_peer_id_t catchup_peer_id) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 844 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 845 | mc_socket_main_t *msm = (mc_socket_main_t *) transport_main; |
| 846 | mc_main_t *mcm = &msm->mc_main; |
| 847 | vlib_main_t *vm = mcm->vlib_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 848 | mc_socket_catchup_t *c; |
| 849 | struct sockaddr_in addr; |
| 850 | unix_main_t *um = &unix_main; |
| 851 | int one = 1; |
| 852 | |
| 853 | pool_get (msm->catchups, c); |
| 854 | memset (c, 0, sizeof (*c)); |
| 855 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 856 | c->socket = socket (AF_INET, SOCK_STREAM, 0); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 857 | if (c->socket < 0) |
| 858 | { |
| 859 | clib_unix_warning ("socket"); |
| 860 | return 0; |
| 861 | } |
| 862 | |
| 863 | if (ioctl (c->socket, FIONBIO, &one) < 0) |
| 864 | { |
| 865 | clib_unix_warning ("FIONBIO"); |
| 866 | return 0; |
| 867 | } |
| 868 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 869 | memset (&addr, 0, sizeof (addr)); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 870 | addr.sin_family = AF_INET; |
| 871 | addr.sin_addr.s_addr = mc_socket_peer_id_get_address (catchup_peer_id); |
| 872 | addr.sin_port = mc_socket_peer_id_get_port (catchup_peer_id); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 873 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 874 | c->connect_in_progress = 1; |
| 875 | |
| 876 | if (MC_EVENT_LOGGING) |
| 877 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 878 | ELOG_TYPE_DECLARE (e) = |
| 879 | { |
| 880 | .format = "connecting to peer 0x%Lx",.format_args = "i8",}; |
| 881 | struct |
| 882 | { |
| 883 | u64 peer; |
| 884 | } *ed; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 885 | ed = ELOG_DATA (&vm->elog_main, e); |
| 886 | ed->peer = catchup_peer_id.as_u64; |
| 887 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 888 | |
| 889 | if (connect (c->socket, (const void *) &addr, sizeof (addr)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 890 | < 0 && errno != EINPROGRESS) |
| 891 | { |
| 892 | clib_unix_warning ("connect to %U fails", |
| 893 | format_socket_peer_id, catchup_peer_id); |
| 894 | return 0; |
| 895 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 896 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 897 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 898 | unix_file_t template = { 0 }; |
| 899 | |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 900 | template.read_function = catchup_client_read_ready; |
| 901 | template.write_function = catchup_client_write_ready; |
| 902 | template.error_function = catchup_socket_error_ready; |
| 903 | template.file_descriptor = c->socket; |
| 904 | template.private_data = (uword) msm; |
| 905 | c->unix_file_index = unix_file_add (um, &template); |
| 906 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 907 | hash_set (msm->catchup_index_by_file_descriptor, c->socket, |
| 908 | c - msm->catchups); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 909 | } |
| 910 | |
| 911 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 912 | mc_msg_catchup_request_t *mp; |
| 913 | mp = catchup_add_pending_output (c, sizeof (mp[0]), /* set_output_vector */ |
| 914 | 0); |
| 915 | mp->peer_id = msm->mc_main.transport.our_catchup_peer_id; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 916 | mp->stream_index = stream_index; |
| 917 | mc_byte_swap_msg_catchup_request (mp); |
| 918 | } |
| 919 | |
| 920 | return c - msm->catchups; |
| 921 | } |
| 922 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 923 | static void |
| 924 | catchup_send_fun (void *transport_main, uword opaque, u8 * data) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 925 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 926 | mc_socket_main_t *msm = (mc_socket_main_t *) transport_main; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 927 | mc_socket_catchup_t *c = pool_elt_at_index (msm->catchups, opaque); |
| 928 | catchup_add_pending_output (c, 0, data); |
| 929 | } |
| 930 | |
| 931 | static int |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 932 | find_interface_ip4_address (char *if_name, u32 * ip4_address, u32 * mtu) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 933 | { |
| 934 | int fd; |
| 935 | struct ifreq ifr; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 936 | struct sockaddr_in *sa; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 937 | |
| 938 | /* Dig up our IP address */ |
| 939 | fd = socket (PF_INET, AF_INET, 0); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 940 | if (fd < 0) |
| 941 | { |
| 942 | clib_unix_error ("socket"); |
| 943 | return -1; |
| 944 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 945 | |
| 946 | ifr.ifr_addr.sa_family = AF_INET; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 947 | strncpy (ifr.ifr_name, if_name, sizeof (ifr.ifr_name) - 1); |
| 948 | if (ioctl (fd, SIOCGIFADDR, &ifr) < 0) |
| 949 | { |
| 950 | clib_unix_error ("ioctl(SIOCFIGADDR)"); |
Dave Barach | dd522cb | 2016-08-10 16:56:16 -0400 | [diff] [blame] | 951 | close (fd); |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 952 | return -1; |
| 953 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 954 | |
| 955 | sa = (void *) &ifr.ifr_addr; |
Damjan Marion | f1213b8 | 2016-03-13 02:22:06 +0100 | [diff] [blame] | 956 | clib_memcpy (ip4_address, &sa->sin_addr.s_addr, sizeof (ip4_address[0])); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 957 | |
| 958 | if (ioctl (fd, SIOCGIFMTU, &ifr) < 0) |
Dave Barach | b2a6e25 | 2016-07-27 10:00:58 -0400 | [diff] [blame] | 959 | { |
| 960 | close (fd); |
| 961 | return -1; |
| 962 | } |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 963 | if (mtu) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 964 | *mtu = ifr.ifr_mtu - ( /* IP4 header */ 20 + /* UDP header */ 8); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 965 | |
| 966 | close (fd); |
| 967 | |
| 968 | return 0; |
| 969 | } |
| 970 | |
| 971 | clib_error_t * |
| 972 | mc_socket_main_init (mc_socket_main_t * msm, char **intfc_probe_list, |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 973 | int n_intfcs_to_probe) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 974 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 975 | clib_error_t *error; |
| 976 | mc_main_t *mcm; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 977 | u32 mtu; |
| 978 | |
| 979 | mcm = &msm->mc_main; |
| 980 | |
| 981 | /* 239.255.0.7 */ |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 982 | if (!msm->multicast_tx_ip4_address_host_byte_order) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 983 | msm->multicast_tx_ip4_address_host_byte_order = 0xefff0007; |
| 984 | |
| 985 | { |
| 986 | u32 i, a, win; |
| 987 | |
| 988 | win = 0; |
| 989 | if (msm->multicast_interface_name) |
| 990 | { |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 991 | win = |
| 992 | !find_interface_ip4_address (msm->multicast_interface_name, &a, |
| 993 | &mtu); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 994 | } |
| 995 | else |
| 996 | { |
| 997 | for (i = 0; i < n_intfcs_to_probe; i++) |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 998 | if (!find_interface_ip4_address (intfc_probe_list[i], &a, &mtu)) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 999 | { |
| 1000 | win = 1; |
| 1001 | msm->multicast_interface_name = intfc_probe_list[i]; |
| 1002 | break; |
| 1003 | } |
| 1004 | } |
| 1005 | |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1006 | if (!win) |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1007 | return clib_error_return (0, "can't find interface ip4 address"); |
| 1008 | |
| 1009 | msm->if_ip4_address_net_byte_order = a; |
| 1010 | } |
| 1011 | |
| 1012 | msm->rx_mtu_n_bytes = mtu; |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1013 | msm->rx_mtu_n_buffers = |
| 1014 | msm->rx_mtu_n_bytes / VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES; |
| 1015 | msm->rx_mtu_n_buffers += |
| 1016 | (msm->rx_mtu_n_bytes % VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES) != 0; |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1017 | |
| 1018 | error = socket_setup (msm); |
| 1019 | if (error) |
| 1020 | return error; |
| 1021 | |
| 1022 | mcm->transport.our_ack_peer_id = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1023 | mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, |
| 1024 | msm->ack_udp_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1025 | |
| 1026 | mcm->transport.our_catchup_peer_id = |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1027 | mc_socket_set_peer_id (msm->if_ip4_address_net_byte_order, |
| 1028 | msm->catchup_tcp_port); |
Ed Warnicke | cb9cada | 2015-12-08 15:45:58 -0700 | [diff] [blame] | 1029 | |
| 1030 | mcm->transport.tx_buffer = tx_buffer; |
| 1031 | mcm->transport.tx_ack = tx_ack; |
| 1032 | mcm->transport.catchup_request_fun = catchup_request_fun; |
| 1033 | mcm->transport.catchup_send_fun = catchup_send_fun; |
| 1034 | mcm->transport.format_peer_id = format_socket_peer_id; |
| 1035 | mcm->transport.opaque = msm; |
| 1036 | mcm->transport.max_packet_size = mtu; |
| 1037 | |
| 1038 | mc_main_init (mcm, "socket"); |
| 1039 | |
| 1040 | return error; |
| 1041 | } |
Dave Barach | 9b8ffd9 | 2016-07-08 08:13:45 -0400 | [diff] [blame] | 1042 | |
| 1043 | /* |
| 1044 | * fd.io coding-style-patch-verification: ON |
| 1045 | * |
| 1046 | * Local Variables: |
| 1047 | * eval: (c-set-style "gnu") |
| 1048 | * End: |
| 1049 | */ |