FD.io VPP  v16.09
Vector Packet Processing
mc_socket.c
Go to the documentation of this file.
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 */
22 #include <netinet/tcp.h> /* for TCP_NODELAY */
23 #include <net/if.h> /* for struct ifreq */
24 
25 static u8 *
26 format_socket_peer_id (u8 * s, va_list * args)
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;
32  u32 p = mc_socket_peer_id_get_port (peer_id);
33 
34  s = format (s, "%U:%04x", format_network_address, AF_INET, &a, ntohs (p));
35 
36  return s;
37 }
38 
39 typedef void (mc_msg_handler_t) (mc_main_t * mcm, void *msg,
40  u32 buffer_index);
41 
42 always_inline void
44  u32 buffer_index, u32 handler_frees_buffer, void *_h)
45 {
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);
50 
51  h (mcm, the_msg, buffer_index);
52  if (!handler_frees_buffer)
53  vlib_buffer_free_one (vm, buffer_index);
54 }
55 
56 static uword
58  u32 buffer_index, struct iovec **iovs_return)
59 {
60  struct iovec *i;
61  vlib_buffer_t *b;
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;
72  if (!(b->flags & VLIB_BUFFER_NEXT_PRESENT))
73  break;
74  bi = b->next_buffer;
75  }
76 
77  return l;
78 }
79 
80 static clib_error_t *
82  int socket, struct sockaddr_in *tx_addr, u32 buffer_index)
83 {
84  vlib_main_t *vm = msm->mc_main.vlib_main;
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
105  && errno == EAGAIN)
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  {
114  ELOG_TYPE_DECLARE (e) =
115  {
116  .format = "sendmsg-helper: %d retries",.format_args = "i4",};
117  struct
118  {
119  u32 retries;
120  } *ed = 0;
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 *
129 tx_buffer (void *transport, mc_transport_type_t type, u32 buffer_index)
130 {
131  mc_socket_main_t *msm = (mc_socket_main_t *) transport;
132  vlib_main_t *vm = msm->mc_main.vlib_main;
134  clib_error_t *error;
135  error = sendmsg_helper (msm, ms->socket, &ms->tx_addr, buffer_index);
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;
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;
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 *
161  int socket,
162  struct sockaddr_in *rx_addr,
163  u32 * buffer_index, u32 drop_message)
164 {
165  vlib_main_t *vm = msm->mc_main.vlib_main;
166  vlib_buffer_t *b;
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);
178  n_alloc =
179  vlib_buffer_alloc (vm, msm->rx_buffers + n_left, max_alloc - n_left);
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;
227  b->current_length =
228  n_bytes_left < buffer_size ? n_bytes_left : buffer_size;
229 
230  n_bytes_left -= buffer_size;
231 
232  if (n_bytes_left <= 0)
233  break;
234 
235  i_rx--;
237  b->next_buffer = msm->rx_buffers[i_rx];
238  }
239 
240  _vec_len (msm->rx_buffers) = i_rx;
241 
242  return 0 /* no error */ ;
243 }
244 
245 static clib_error_t *
247 {
249  mc_main_t *mcm = &msm->mc_main;
252  clib_error_t *error;
253  u32 bi;
254 
255  error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */
256  0);
257  if (!error)
258  msg_handler (mcm, bi,
259  /* handler_frees_buffer */ 0,
261 
262  return error;
263 }
264 
265 static clib_error_t *
267 {
269  mc_main_t *mcm = &msm->mc_main;
270  vlib_main_t *vm = msm->mc_main.vlib_main;
271  mc_multicast_socket_t *ms_to_relay =
273  mc_multicast_socket_t *ms_from_relay =
275  clib_error_t *error;
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,
281  /* drop_message */ !is_master);
282 
283  /* If we are the master, number and rebroadcast the msg. */
284  if (!error && is_master)
285  {
286  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
287  mc_msg_user_request_t *mp = vlib_buffer_get_current (b);
288  mp->global_sequence = clib_host_to_net_u32 (mcm->relay_global_sequence);
289  mcm->relay_global_sequence++;
290  error =
291  sendmsg_helper (msm, ms_from_relay->socket, &ms_from_relay->tx_addr,
292  bi);
293  vlib_buffer_free_one (vm, bi);
294  }
295 
296  return error;
297 }
298 
299 static clib_error_t *
301 {
303  mc_main_t *mcm = &msm->mc_main;
306  clib_error_t *error;
307  u32 bi;
308 
309  error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */
310  0);
311  if (!error)
312  {
313  msg_handler (mcm, bi, /* handler_frees_buffer */ 1,
315  }
316  return error;
317 }
318 
319 static clib_error_t *
321 {
323  mc_main_t *mcm = &msm->mc_main;
324  vlib_main_t *vm = mcm->vlib_main;
326  clib_error_t *error;
327  u32 bi;
328 
329  error = recvmsg_helper (msm, ms->socket, /* rx_addr */ 0, &bi, /* drop_message */
330  0);
331  if (!error)
332  {
333  vlib_buffer_t *b = vlib_get_buffer (vm, bi);
334  mc_msg_join_or_leave_request_t *mp = vlib_buffer_get_current (b);
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,
341  break;
342 
343  case MC_MSG_TYPE_join_reply:
344  msg_handler (mcm, bi, /* handler_frees_buffer */ 0,
346  break;
347 
348  default:
349  ASSERT (0);
350  break;
351  }
352  }
353  return error;
354 }
355 
356 static clib_error_t *
358 {
360  mc_main_t *mcm = &msm->mc_main;
361  clib_error_t *error;
362  u32 bi;
363 
364  error = recvmsg_helper (msm, msm->ack_socket, /* rx_addr */ 0, &bi,
365  /* drop_message */ 0);
366  if (!error)
367  msg_handler (mcm, bi, /* handler_frees_buffer */ 0,
369  return error;
370 }
371 
372 static void
375 {
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 *
385  int file_descriptor)
386 {
387  uword *p =
388  hash_get (msm->catchup_index_by_file_descriptor, file_descriptor);
389  return p ? pool_elt_at_index (msm->catchups, p[0]) : 0;
390 }
391 
392 static clib_error_t *
394 {
395  unix_main_t *um = &unix_main;
397  mc_main_t *mcm = &msm->mc_main;
400  word l, n, is_eof;
401 
402  l = vec_len (c->input_vector);
403  vec_resize (c->input_vector, 4096);
404  n =
405  read (uf->file_descriptor, c->input_vector + l,
406  vec_len (c->input_vector) - l);
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  {
426  mc_msg_catchup_request_handler (mcm, (void *) c->input_vector,
427  c - msm->catchups);
428  _vec_len (c->input_vector) = 0;
429  }
430  else
431  {
432  mc_msg_catchup_reply_handler (mcm, (void *) c->input_vector,
433  c - msm->catchups);
434  c->input_vector = 0; /* reply handler is responsible for freeing vector */
435  catchup_cleanup (msm, c, um, uf);
436  }
437  }
438 
439  return 0 /* no error */ ;
440 }
441 
442 static clib_error_t *
444 {
445  return catchup_socket_read_ready (uf, /* is_server */ 1);
446 }
447 
448 static clib_error_t *
450 {
451  if (MC_EVENT_LOGGING)
452  {
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 *
464 {
465  unix_main_t *um = &unix_main;
469  clib_error_t *error = 0;
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);
478  if (getsockopt (c->socket, SOL_SOCKET, SO_ERROR, &value, &len) < 0)
479  {
480  error = clib_error_return_unix (0, "getsockopt SO_ERROR");
481  goto error_quit;
482  }
483  if (value != 0)
484  {
485  error =
487  "connect fails");
488  goto error_quit;
489  }
490  }
491 
492  while (1)
493  {
494  u32 n_this_write;
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 */ );
500 
501  if (n_this_write <= 0)
502  break;
503 
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 
512  if (n < 0)
513  {
514  error = clib_error_return_unix (0, "write");
515  goto error_quit;
516  }
517  c->output_vector_n_written += n;
518  }
519 
520  if (c->output_vector_n_written >= vec_len (c->output_vector))
521  {
522  if (!is_server)
523  {
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 
539 static clib_error_t *
541 {
542  return catchup_socket_write_ready (uf, /* is_server */ 1);
543 }
544 
545 static clib_error_t *
547 {
548  return catchup_socket_write_ready (uf, /* is_server */ 0);
549 }
550 
551 static clib_error_t *
553 {
554  unix_main_t *um = &unix_main;
558  catchup_cleanup (msm, c, um, uf);
559  return clib_error_return (0, "error");
560 }
561 
562 static clib_error_t *
564 {
566  struct sockaddr_in client_addr;
567  int client_len;
569  unix_file_t template = { 0 };
570 
571  pool_get (msm->catchups, c);
572  memset (c, 0, sizeof (c[0]));
573 
574  client_len = sizeof (client_addr);
575 
576  /* Acquires the non-blocking attrib from the server socket. */
577  c->socket = accept (uf->file_descriptor,
578  (struct sockaddr *) &client_addr,
579  (socklen_t *) & client_len);
580 
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  {
589  mc_main_t *mcm = &msm->mc_main;
590  vlib_main_t *vm = mcm->vlib_main;
591 
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;
599 
600  ed = ELOG_DATA (&vm->elog_main, e);
601  ed->addr = ntohl (client_addr.sin_addr.s_addr);
602  }
603 
604  /* Disable the Nagle algorithm, ship catchup pkts immediately */
605  {
606  int one = 1;
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  }
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);
620  hash_set (msm->catchup_index_by_file_descriptor, c->socket,
621  c - msm->catchups);
622 
623  return 0;
624 }
625 
626 /* Return and bind to an unused port. */
627 static word
629 {
630  for (; port < 1 << 16; port++)
631  {
632  struct sockaddr_in a;
633 
634  memset (&a, 0, sizeof (a)); /* Warnings be gone */
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  }
643 
644  return port < 1 << 16 ? port : -1;
645 }
646 
647 static clib_error_t *
650  char *type, uword udp_port)
651 {
652  int one = 1;
653  struct ip_mreq mcast_req;
654 
655  if (!msm->multicast_ttl)
656  msm->multicast_ttl = 1;
657 
658  /* mastership (multicast) TX socket */
659  if ((ms->socket = socket (PF_INET, SOCK_DGRAM, IPPROTO_UDP)) < 0)
660  return clib_error_return_unix (0, "%s socket", type);
661 
662  {
663  u8 ttl = msm->multicast_ttl;
664 
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);
668  }
669 
670  if (setsockopt (ms->socket, SOL_SOCKET, SO_REUSEADDR, &one, sizeof (one)) <
671  0)
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;
676  ms->tx_addr.sin_addr.s_addr =
678  ms->tx_addr.sin_port = htons (udp_port);
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);
683 
684  memset (&mcast_req, 0, sizeof (mcast_req));
685  mcast_req.imr_multiaddr.s_addr =
687  mcast_req.imr_interface.s_addr = msm->if_ip4_address_net_byte_order;
688 
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);
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);
702  if (setsockopt (ms->socket, SOL_SOCKET, SO_SNDBUF, &len, sl) < 0)
703  clib_unix_error ("setsockopt");
704  }
705 
706  return 0;
707 }
708 
709 static clib_error_t *
711 {
712  int one = 1;
713  clib_error_t *error;
714  u32 port;
715 
718  0xffff - ((MC_N_TRANSPORT_TYPE + 2 /* ack socket, catchup socket */ )
719  - 1);
720 
722 
723  error = setup_mutlicast_socket (msm,
724  &msm->multicast_sockets
725  [MC_TRANSPORT_MASTERSHIP], "mastership",
726  port++);
727  if (error)
728  return error;
729 
730  error = setup_mutlicast_socket (msm,
732  "join", port++);
733  if (error)
734  return error;
735 
736  error = setup_mutlicast_socket (msm,
737  &msm->multicast_sockets
739  "to relay", port++);
740  if (error)
741  return error;
742 
743  error = setup_mutlicast_socket (msm,
744  &msm->multicast_sockets
746  "from relay", port++);
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)
753  return clib_error_return_unix (0, "ack socket");
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 
760  msm->catchup_server_socket = socket (AF_INET, SOCK_STREAM, 0);
761  if (msm->catchup_server_socket < 0)
762  return clib_error_return_unix (0, "catchup server socket");
763 
764  msm->catchup_tcp_port =
766 
767  if (ioctl (msm->catchup_server_socket, FIONBIO, &one) < 0)
768  return clib_error_return_unix (0, "catchup server socket FIONBIO");
769 
770  if (listen (msm->catchup_server_socket, 5) < 0)
771  return clib_error_return_unix (0, "catchup server socket listen");
772 
773  /* epoll setup for multicast mastership socket */
774  {
775  unix_file_t template = { 0 };
776 
778  template.file_descriptor =
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;
785  template.file_descriptor =
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;
792  template.file_descriptor =
794  template.private_data = (uword) msm;
795  unix_file_add (&unix_main, &template);
796 
797  template.read_function = join_socket_read_ready;
798  template.file_descriptor =
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 *
821  u8 * set_output_vector)
822 {
824  c->unix_file_index);
825  u8 *result = 0;
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);
835  if (!skip_update)
837  }
838  return result;
839 }
840 
841 static uword
842 catchup_request_fun (void *transport_main,
843  u32 stream_index, mc_peer_id_t catchup_peer_id)
844 {
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;
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 
856  c->socket = socket (AF_INET, SOCK_STREAM, 0);
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 
869  memset (&addr, 0, sizeof (addr));
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);
873 
874  c->connect_in_progress = 1;
875 
876  if (MC_EVENT_LOGGING)
877  {
878  ELOG_TYPE_DECLARE (e) =
879  {
880  .format = "connecting to peer 0x%Lx",.format_args = "i8",};
881  struct
882  {
883  u64 peer;
884  } *ed;
885  ed = ELOG_DATA (&vm->elog_main, e);
886  ed->peer = catchup_peer_id.as_u64;
887  }
888 
889  if (connect (c->socket, (const void *) &addr, sizeof (addr))
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  }
896 
897  {
898  unix_file_t template = { 0 };
899 
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 
908  c - msm->catchups);
909  }
910 
911  {
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;
916  mp->stream_index = stream_index;
918  }
919 
920  return c - msm->catchups;
921 }
922 
923 static void
924 catchup_send_fun (void *transport_main, uword opaque, u8 * data)
925 {
926  mc_socket_main_t *msm = (mc_socket_main_t *) transport_main;
928  catchup_add_pending_output (c, 0, data);
929 }
930 
931 static int
932 find_interface_ip4_address (char *if_name, u32 * ip4_address, u32 * mtu)
933 {
934  int fd;
935  struct ifreq ifr;
936  struct sockaddr_in *sa;
937 
938  /* Dig up our IP address */
939  fd = socket (PF_INET, AF_INET, 0);
940  if (fd < 0)
941  {
942  clib_unix_error ("socket");
943  return -1;
944  }
945 
946  ifr.ifr_addr.sa_family = AF_INET;
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)");
951  close (fd);
952  return -1;
953  }
954 
955  sa = (void *) &ifr.ifr_addr;
956  clib_memcpy (ip4_address, &sa->sin_addr.s_addr, sizeof (ip4_address[0]));
957 
958  if (ioctl (fd, SIOCGIFMTU, &ifr) < 0)
959  {
960  close (fd);
961  return -1;
962  }
963  if (mtu)
964  *mtu = ifr.ifr_mtu - ( /* IP4 header */ 20 + /* UDP header */ 8);
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,
973  int n_intfcs_to_probe)
974 {
975  clib_error_t *error;
976  mc_main_t *mcm;
977  u32 mtu;
978 
979  mcm = &msm->mc_main;
980 
981  /* 239.255.0.7 */
984 
985  {
986  u32 i, a, win;
987 
988  win = 0;
989  if (msm->multicast_interface_name)
990  {
991  win =
993  &mtu);
994  }
995  else
996  {
997  for (i = 0; i < n_intfcs_to_probe; i++)
998  if (!find_interface_ip4_address (intfc_probe_list[i], &a, &mtu))
999  {
1000  win = 1;
1001  msm->multicast_interface_name = intfc_probe_list[i];
1002  break;
1003  }
1004  }
1005 
1006  if (!win)
1007  return clib_error_return (0, "can't find interface ip4 address");
1008 
1010  }
1011 
1012  msm->rx_mtu_n_bytes = mtu;
1013  msm->rx_mtu_n_buffers =
1015  msm->rx_mtu_n_buffers +=
1017 
1018  error = socket_setup (msm);
1019  if (error)
1020  return error;
1021 
1022  mcm->transport.our_ack_peer_id =
1024  msm->ack_udp_port);
1025 
1028  msm->catchup_tcp_port);
1029 
1030  mcm->transport.tx_buffer = tx_buffer;
1031  mcm->transport.tx_ack = tx_ack;
1035  mcm->transport.opaque = msm;
1036  mcm->transport.max_packet_size = mtu;
1037 
1038  mc_main_init (mcm, "socket");
1039 
1040  return error;
1041 }
1042 
1043 /*
1044  * fd.io coding-style-patch-verification: ON
1045  *
1046  * Local Variables:
1047  * eval: (c-set-style "gnu")
1048  * End:
1049  */
void mc_main_init(mc_main_t *mcm, char *tag)
Definition: mc.c:2417
#define vec_validate(V, I)
Make sure vector is long enough for given index (no header, unspecified alignment) ...
Definition: vec.h:396
static void msg_handler(mc_main_t *mcm, u32 buffer_index, u32 handler_frees_buffer, void *_h)
Definition: mc_socket.c:43
unix_file_t * file_pool
Definition: unix.h:89
#define clib_error_return_code(e, code, flags, args...)
Definition: error.h:105
#define hash_set(h, key, value)
Definition: hash.h:254
sll srl srl sll sra u16x4 i
Definition: vector_sse2.h:343
#define clib_min(x, y)
Definition: clib.h:326
int catchup_server_socket
Definition: mc_socket.h:54
u32 flags
Definition: unix.h:54
unix_file_function_t * read_function
Definition: unix.h:62
#define hash_unset(h, key)
Definition: hash.h:260
a
Definition: bitmap.h:516
bad routing header type(not 4)") sr_error (NO_MORE_SEGMENTS
void mc_msg_master_assert_handler(mc_main_t *mcm, mc_msg_master_assert_t *mp, u32 buffer_index)
Definition: mc.c:1891
static_always_inline mc_peer_id_t mc_socket_set_peer_id(u32 address_net_byte_order, u32 port_host_byte_order)
Definition: mc_socket.h:109
#define clib_error(format, args...)
Definition: error.h:62
#define ELOG(em, f, data)
Definition: elog.h:382
static clib_error_t * ack_socket_read_ready(unix_file_t *uf)
Definition: mc_socket.c:357
void mc_msg_user_ack_handler(mc_main_t *mcm, mc_msg_user_ack_t *mp, u32 buffer_index)
Definition: mc.c:1223
#define vec_add2(V, P, N)
Add N elements to end of vector V, return pointer to new elements in P.
Definition: vec.h:521
static clib_error_t * catchup_server_read_ready(unix_file_t *uf)
Definition: mc_socket.c:443
void mc_msg_join_reply_handler(mc_main_t *mcm, mc_msg_join_reply_t *mp, u32 buffer_index)
Definition: mc.c:971
#define pool_get(P, E)
Allocate an object E from a pool P (unspecified alignment).
Definition: pool.h:200
mc_main_t * mc_main
Definition: main.h:133
static uword unix_file_add(unix_main_t *um, unix_file_t *template)
Definition: unix.h:136
mc_multicast_socket_t multicast_sockets[MC_N_TRANSPORT_TYPE]
Definition: mc_socket.h:48
static clib_error_t * join_socket_read_ready(unix_file_t *uf)
Definition: mc_socket.c:320
static clib_error_t * catchup_socket_error_ready(unix_file_t *uf)
Definition: mc_socket.c:552
static clib_error_t * catchup_socket_read_ready(unix_file_t *uf, int is_server)
Definition: mc_socket.c:393
struct iovec * iovecs
Definition: mc_socket.h:70
i16 current_data
signed offset in data[], pre_data[] that we are currently processing.
Definition: buffer.h:78
static clib_error_t * tx_buffer(void *transport, mc_transport_type_t type, u32 buffer_index)
Definition: mc_socket.c:129
void mc_msg_catchup_reply_handler(mc_main_t *mcm, mc_msg_catchup_reply_t *mp, u32 catchup_opaque)
Definition: mc.c:1584
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:187
void * opaque
Definition: mc.h:291
unsigned long u64
Definition: types.h:89
static clib_error_t * setup_mutlicast_socket(mc_socket_main_t *msm, mc_multicast_socket_t *ms, char *type, uword udp_port)
Definition: mc_socket.c:648
mc_peer_id_t our_catchup_peer_id
Definition: mc.h:294
#define vec_resize(V, N)
Resize a vector (no header, unspecified alignment) Add N elements to end of given vector V...
Definition: vec.h:201
static clib_error_t * sendmsg_helper(mc_socket_main_t *msm, int socket, struct sockaddr_in *tx_addr, u32 buffer_index)
Definition: mc_socket.c:81
u32 * rx_buffers
Definition: mc_socket.h:67
static uword pointer_to_uword(const void *p)
Definition: types.h:131
#define VLIB_BUFFER_NEXT_PRESENT
Definition: buffer.h:95
mc_main_t mc_main
Definition: mc_socket.h:45
mc_socket_catchup_t * catchups
Definition: mc_socket.h:57
static void unix_file_del(unix_main_t *um, unix_file_t *f)
Definition: unix.h:146
#define hash_get(h, key)
Definition: hash.h:248
static u8 * format_socket_peer_id(u8 *s, va_list *args)
Definition: mc_socket.c:26
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
u32 base_multicast_udp_port_host_byte_order
Definition: mc_socket.h:91
static word find_and_bind_to_free_port(word sock, word port)
Definition: mc_socket.c:628
u32 file_descriptor
Definition: unix.h:52
#define VLIB_BUFFER_DEFAULT_FREE_LIST_BYTES
Definition: buffer.h:304
u64 as_u64
Definition: mc.h:43
u16 current_length
Nbytes between current data and the end of this buffer.
Definition: buffer.h:82
uword private_data
Definition: unix.h:59
static clib_error_t * catchup_client_write_ready(unix_file_t *uf)
Definition: mc_socket.c:546
#define clib_error_return_unix(e, args...)
Definition: error.h:114
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
static clib_error_t * from_relay_socket_read_ready(unix_file_t *uf)
Definition: mc_socket.c:300
static u32 mc_socket_peer_id_get_port(mc_peer_id_t i)
Definition: mc_socket.h:103
#define ELOG_DATA(em, f)
Definition: elog.h:392
static int find_interface_ip4_address(char *if_name, u32 *ip4_address, u32 *mtu)
Definition: mc_socket.c:932
static uword append_buffer_index_to_iovec(vlib_main_t *vm, u32 buffer_index, struct iovec **iovs_return)
Definition: mc_socket.c:57
uword * catchup_index_by_file_descriptor
Definition: mc_socket.h:59
static void * catchup_add_pending_output(mc_socket_catchup_t *c, uword n_bytes, u8 *set_output_vector)
Definition: mc_socket.c:820
mc_peer_id_t our_ack_peer_id
Definition: mc.h:293
static void catchup_send_fun(void *transport_main, uword opaque, u8 *data)
Definition: mc_socket.c:924
clib_error_t * mc_socket_main_init(mc_socket_main_t *msm, char **intfc_probe_list, int n_intfcs_to_probe)
Definition: mc_socket.c:972
static void catchup_cleanup(mc_socket_main_t *msm, mc_socket_catchup_t *c, unix_main_t *um, unix_file_t *uf)
Definition: mc_socket.c:373
static clib_error_t * to_relay_socket_read_ready(unix_file_t *uf)
Definition: mc_socket.c:266
svmdb_client_t * c
#define vec_free(V)
Free vector&#39;s memory (no header).
Definition: vec.h:300
static clib_error_t * tx_ack(void *transport, mc_peer_id_t dest_peer_id, u32 buffer_index)
Definition: mc_socket.c:142
u32 catchup_tcp_port
Definition: mc_socket.h:76
#define clib_memcpy(a, b, c)
Definition: string.h:63
#define clib_unix_warning(format, args...)
Definition: error.h:68
elog_main_t elog_main
Definition: main.h:141
#define ELOG_TYPE(f, fmt)
Definition: elog.h:370
#define ELOG_TYPE_DECLARE(f)
Definition: elog.h:350
void mc_msg_user_request_handler(mc_main_t *mcm, mc_msg_user_request_t *mp, u32 buffer_index)
Definition: mc.c:1090
static clib_error_t * catchup_socket_write_ready(unix_file_t *uf, int is_server)
Definition: mc_socket.c:463
u32 max_packet_size
Definition: mc.h:298
uword(* catchup_request_fun)(void *opaque, u32 stream_index, mc_peer_id_t catchup_peer_id)
Definition: mc.h:284
Definition: mc.h:522
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
void mc_msg_join_or_leave_request_handler(mc_main_t *mcm, mc_msg_join_or_leave_request_t *req, u32 buffer_index)
Definition: mc.c:914
static clib_error_t * catchup_client_read_ready(unix_file_t *uf)
Definition: mc_socket.c:449
static clib_error_t * catchup_server_write_ready(unix_file_t *uf)
Definition: mc_socket.c:540
u8 * format(u8 *s, char *fmt,...)
Definition: format.c:418
u32 next_buffer
Next buffer for this linked-list of buffers.
Definition: buffer.h:114
u32 vlib_buffer_alloc(vlib_main_t *vm, u32 *buffers, u32 n_buffers)
Allocate buffers into supplied array.
Definition: dpdk_buffer.c:643
unix_main_t unix_main
Definition: main.c:57
#define clib_unix_error(format, args...)
Definition: error.h:65
u64 uword
Definition: types.h:112
mc_transport_type_t
Definition: mc.h:266
void(* file_update)(unix_file_t *file, unix_file_update_type_t update_type)
Definition: unix.h:94
i64 word
Definition: types.h:111
#define UNIX_FILE_DATA_AVAILABLE_TO_WRITE
Definition: unix.h:55
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static clib_error_t * catchup_listen_read_ready(unix_file_t *uf)
Definition: mc_socket.c:563
struct sockaddr_in tx_addr
Definition: mc_socket.h:27
u32 rx_mtu_n_buffers
Definition: mc_socket.h:64
Definition: unix.h:49
void(* catchup_send_fun)(void *opaque, uword catchup_opaque, u8 *data_vector)
Definition: mc.h:287
format_function_t * format_peer_id
Definition: mc.h:300
static clib_error_t * recvmsg_helper(mc_socket_main_t *msm, int socket, struct sockaddr_in *rx_addr, u32 *buffer_index, u32 drop_message)
Definition: mc_socket.c:160
u32 if_ip4_address_net_byte_order
Definition: mc_socket.h:73
clib_error_t *(* tx_buffer)(void *opaque, mc_transport_type_t type, u32 buffer_index)
Definition: mc.h:277
static clib_error_t * mastership_socket_read_ready(unix_file_t *uf)
Definition: mc_socket.c:246
#define MC_EVENT_LOGGING
Definition: mc.h:27
clib_error_t *(* tx_ack)(void *opaque, mc_peer_id_t peer_id, u32 buffer_index)
Definition: mc.h:280
void mc_msg_catchup_request_handler(mc_main_t *mcm, mc_msg_catchup_request_t *req, u32 catchup_opaque)
Definition: mc.c:1494
u8 data[0]
Packet data.
Definition: buffer.h:151
u8 * format_network_address(u8 *s, va_list *args)
Definition: unix-formats.c:205
u32 multicast_tx_ip4_address_host_byte_order
Definition: mc_socket.h:83
static void vlib_buffer_free_one(vlib_main_t *vm, u32 buffer_index)
Free one buffer Shorthand to free a single buffer chain.
Definition: buffer_funcs.h:322
vhost_vring_addr_t addr
Definition: vhost-user.h:82
#define clib_error_return(e, args...)
Definition: error.h:111
char * multicast_interface_name
Definition: mc_socket.h:79
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
struct vlib_main_t * vlib_main
Definition: mc.h:576
static uword catchup_request_fun(void *transport_main, u32 stream_index, mc_peer_id_t catchup_peer_id)
Definition: mc_socket.c:842
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
void( mc_msg_handler_t)(mc_main_t *mcm, void *msg, u32 buffer_index)
Definition: mc_socket.c:39
static mc_socket_catchup_t * find_catchup_from_file_descriptor(mc_socket_main_t *msm, int file_descriptor)
Definition: mc_socket.c:384
mc_transport_t transport
Definition: mc.h:537
static u32 mc_socket_peer_id_get_address(mc_peer_id_t i)
Definition: mc_socket.h:95
static void mc_byte_swap_msg_catchup_request(mc_msg_catchup_request_t *r)
Definition: mc.h:192
static clib_error_t * socket_setup(mc_socket_main_t *msm)
Definition: mc_socket.c:710