2 * mc.h: vlib reliable sequenced multicast distributed applications
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:
9 * http://www.apache.org/licenses/LICENSE-2.0
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.
18 #ifndef included_vlib_mc_h
19 #define included_vlib_mc_h
21 #include <vppinfra/elog.h>
22 #include <vppinfra/fifo.h>
23 #include <vppinfra/mhash.h>
24 #include <vlib/node.h>
26 #ifndef MC_EVENT_LOGGING
27 #define MC_EVENT_LOGGING 1
31 mc_need_byte_swap (void)
33 return CLIB_ARCH_IS_LITTLE_ENDIAN;
37 * Used to uniquely identify hosts.
38 * For IP4 this would be ip4_address plus tcp/udp port.
46 always_inline mc_peer_id_t
47 mc_byte_swap_peer_id (mc_peer_id_t i)
49 /* Peer id is already in network byte order. */
54 mc_peer_id_compare (mc_peer_id_t a, mc_peer_id_t b)
56 return memcmp (a.as_u8, b.as_u8, sizeof (a.as_u8));
59 /* Assert mastership. Lowest peer_id amount all peers wins mastership.
60 Only sent/received over mastership channel (MC_TRANSPORT_MASTERSHIP).
61 So, we don't need a message opcode. */
62 typedef CLIB_PACKED (struct
64 /* Peer id asserting mastership. */
66 /* Global sequence number asserted. */
67 u32 global_sequence;}) mc_msg_master_assert_t;
70 mc_byte_swap_msg_master_assert (mc_msg_master_assert_t * r)
72 if (mc_need_byte_swap ())
74 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
75 r->global_sequence = clib_byte_swap_u32 (r->global_sequence);
79 #define foreach_mc_msg_type \
81 _ (join_or_leave_request) \
90 #define _(f) MC_MSG_TYPE_##f,
93 } mc_relay_msg_type_t;
95 /* Request to join a given stream. Multicast over MC_TRANSPORT_JOIN. */
96 typedef CLIB_PACKED (struct
98 mc_peer_id_t peer_id; mc_relay_msg_type_t type:32;
99 /* MC_MSG_TYPE_join_or_leave_request */
100 /* Stream to join or leave. */
102 /* join = 1, leave = 0 */
103 u8 is_join;}) mc_msg_join_or_leave_request_t;
106 mc_byte_swap_msg_join_or_leave_request (mc_msg_join_or_leave_request_t * r)
108 if (mc_need_byte_swap ())
110 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
111 r->type = clib_byte_swap_u32 (r->type);
112 r->stream_index = clib_byte_swap_u32 (r->stream_index);
116 /* Join reply. Multicast over MC_TRANSPORT_JOIN. */
117 typedef CLIB_PACKED (struct
119 mc_peer_id_t peer_id; mc_relay_msg_type_t type:32;
120 /* MC_MSG_TYPE_join_reply */
122 /* Peer ID to contact to catchup with this stream. */
123 mc_peer_id_t catchup_peer_id;}) mc_msg_join_reply_t;
126 mc_byte_swap_msg_join_reply (mc_msg_join_reply_t * r)
128 if (mc_need_byte_swap ())
130 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
131 r->type = clib_byte_swap_u32 (r->type);
132 r->stream_index = clib_byte_swap_u32 (r->stream_index);
133 r->catchup_peer_id = mc_byte_swap_peer_id (r->catchup_peer_id);
137 /* Generic (application) request. Multicast over MC_TRANSPORT_USER_REQUEST_TO_RELAY and then
138 relayed by relay master after filling in global sequence number. */
139 typedef CLIB_PACKED (struct
141 mc_peer_id_t peer_id; u32 stream_index;
142 /* Global sequence number as filled in by relay master. */
144 /* Local sequence number as filled in by peer sending message. */
146 /* Size of request data. */
148 /* Opaque request data. */
149 u8 data[0];}) mc_msg_user_request_t;
152 mc_byte_swap_msg_user_request (mc_msg_user_request_t * r)
154 if (mc_need_byte_swap ())
156 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
157 r->stream_index = clib_byte_swap_u32 (r->stream_index);
158 r->global_sequence = clib_byte_swap_u32 (r->global_sequence);
159 r->local_sequence = clib_byte_swap_u32 (r->local_sequence);
160 r->n_data_bytes = clib_byte_swap_u32 (r->n_data_bytes);
164 /* Sent unicast over ACK channel. */
165 typedef CLIB_PACKED (struct
167 mc_peer_id_t peer_id;
171 i32 seq_cmp_result;}) mc_msg_user_ack_t;
174 mc_byte_swap_msg_user_ack (mc_msg_user_ack_t * r)
176 if (mc_need_byte_swap ())
178 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
179 r->stream_index = clib_byte_swap_u32 (r->stream_index);
180 r->global_sequence = clib_byte_swap_u32 (r->global_sequence);
181 r->local_sequence = clib_byte_swap_u32 (r->local_sequence);
182 r->seq_cmp_result = clib_byte_swap_i32 (r->seq_cmp_result);
186 /* Sent/received unicast over catchup channel (e.g. using TCP). */
187 typedef CLIB_PACKED (struct
189 mc_peer_id_t peer_id;
190 u32 stream_index;}) mc_msg_catchup_request_t;
193 mc_byte_swap_msg_catchup_request (mc_msg_catchup_request_t * r)
195 if (mc_need_byte_swap ())
197 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
198 r->stream_index = clib_byte_swap_u32 (r->stream_index);
202 /* Sent/received unicast over catchup channel. */
203 typedef CLIB_PACKED (struct
205 mc_peer_id_t peer_id; u32 stream_index;
206 /* Last global sequence number included in catchup data. */
207 u32 last_global_sequence_included;
208 /* Size of catchup data. */
211 u8 data[0];}) mc_msg_catchup_reply_t;
214 mc_byte_swap_msg_catchup_reply (mc_msg_catchup_reply_t * r)
216 if (mc_need_byte_swap ())
218 r->peer_id = mc_byte_swap_peer_id (r->peer_id);
219 r->stream_index = clib_byte_swap_u32 (r->stream_index);
220 r->last_global_sequence_included =
221 clib_byte_swap_u32 (r->last_global_sequence_included);
222 r->n_data_bytes = clib_byte_swap_u32 (r->n_data_bytes);
226 typedef struct _mc_serialize_msg
228 /* Name for this type. */
231 /* Functions to serialize/unserialize data. */
232 serialize_function_t *serialize;
233 serialize_function_t *unserialize;
235 /* Maximum message size in bytes when serialized.
236 If zero then this will be set to the largest sent message. */
237 u32 max_n_bytes_serialized;
239 /* Opaque to use for first argument to serialize/unserialize function. */
242 /* Index in global message vector. */
245 /* Registration list */
246 struct _mc_serialize_msg *next_registration;
247 } mc_serialize_msg_t;
251 /* Index into global message vector. */
253 } mc_serialize_stream_msg_t;
255 #define MC_SERIALIZE_MSG(x,...) \
256 __VA_ARGS__ mc_serialize_msg_t x; \
257 static void __mc_serialize_msg_registration_##x (void) \
258 __attribute__((__constructor__)) ; \
259 static void __mc_serialize_msg_registration_##x (void) \
261 vlib_main_t * vm = vlib_get_main(); \
262 x.next_registration = vm->mc_msg_registrations; \
263 vm->mc_msg_registrations = &x; \
265 __VA_ARGS__ mc_serialize_msg_t x
269 MC_TRANSPORT_MASTERSHIP,
271 MC_TRANSPORT_USER_REQUEST_TO_RELAY,
272 MC_TRANSPORT_USER_REQUEST_FROM_RELAY,
274 } mc_transport_type_t;
278 clib_error_t *(*tx_buffer) (void *opaque, mc_transport_type_t type,
281 clib_error_t *(*tx_ack) (void *opaque, mc_peer_id_t peer_id,
284 /* Returns catchup opaque. */
285 uword (*catchup_request_fun) (void *opaque, u32 stream_index,
286 mc_peer_id_t catchup_peer_id);
288 void (*catchup_send_fun) (void *opaque, uword catchup_opaque,
291 /* Opaque passed to callbacks. */
294 mc_peer_id_t our_ack_peer_id;
295 mc_peer_id_t our_catchup_peer_id;
297 /* Max packet size (MTU) for this transport.
298 For IP this is interface MTU less IP + UDP header size. */
301 format_function_t *format_peer_id;
306 /* Count of messages received from this peer from the past/future
307 (with seq_cmp != 0). */
308 u64 n_msgs_from_past;
309 u64 n_msgs_from_future;
310 } mc_stream_peer_stats_t;
314 /* ID of this peer. */
317 /* The last sequence we received from this peer. */
318 u32 last_sequence_received;
320 mc_stream_peer_stats_t stats, stats_last_clear;
327 /* Cached copy of local sequence number from buffer. */
330 /* Number of times this buffer has been sent (retried). */
333 /* Previous/next retries in doubly-linked list. */
334 u32 prev_index, next_index;
336 /* Bitmap of all peers which have acked this msg */
337 uword *unacked_by_peer_bitmap;
339 /* Message send or resend time */
345 /* Number of retries sent for this stream. */
357 /* Number of outstanding messages. */
360 /* Retry interval, in seconds */
366 /* User rx buffer callback */
367 void (*rx_buffer) (struct mc_main_t * mc_main,
368 struct mc_stream_t * stream,
369 mc_peer_id_t peer_id, u32 buffer_index);
371 /* User callback to create a snapshot */
372 u8 *(*catchup_snapshot) (struct mc_main_t * mc_main,
373 u8 * snapshot_vector,
374 u32 last_global_sequence_included);
376 /* User callback to replay a snapshot */
377 void (*catchup) (struct mc_main_t * mc_main,
378 u8 * snapshot_data, u32 n_snapshot_data_bytes);
380 /* Callback to save a snapshot for offline replay */
381 void (*save_snapshot) (struct mc_main_t * mc_main,
383 u8 * snapshot_data, u32 n_snapshot_data_bytes);
385 /* Called when a peer dies */
386 void (*peer_died) (struct mc_main_t * mc_main,
387 struct mc_stream_t * stream, mc_peer_id_t peer_id);
388 } mc_stream_config_t;
390 #define foreach_mc_stream_state \
393 _ (join_in_progress) \
399 #define _(f) MC_STREAM_STATE_##f,
400 foreach_mc_stream_state
404 typedef struct mc_stream_t
406 mc_stream_config_t config;
408 mc_stream_state_t state;
410 /* Index in stream pool. */
413 /* Stream index 0 is always for MC internal use. */
414 #define MC_STREAM_INDEX_INTERNAL 0
416 mc_retry_t *retry_pool;
418 /* Head and tail index of retry pool. */
419 u32 retry_head_index, retry_tail_index;
422 * Country club for recently retired messages
423 * If the set of peers is expanding and a new peer
424 * misses a message, we can easily retire the FIFO
425 * element before we even know about the new peer
427 mc_retry_t *retired_fifo;
429 /* Hash mapping local sequence to retry pool index. */
430 uword *retry_index_by_local_sequence;
432 /* catch-up fifo of VLIB buffer indices.
433 start recording when catching up. */
436 mc_stream_stats_t stats, stats_last_clear;
439 mc_stream_peer_t *peers;
441 /* Bitmap with ones for all peers in peer pool. */
442 uword *all_peer_bitmap;
444 /* Map of 64 bit id to index in stream pool. */
445 mhash_t peer_index_by_id;
447 /* Timeout, in case we're alone in the world */
450 vlib_one_time_waiting_process_t *procs_waiting_for_join_done;
452 vlib_one_time_waiting_process_t *procs_waiting_for_open_window;
454 /* Next sequence number to use */
455 u32 our_local_sequence;
458 * Last global sequence we processed.
459 * When supplying catchup data, we need to tell
460 * the client precisely where to start replaying
462 u32 last_global_sequence_processed;
464 /* Vector of unique messages we've sent on this stream. */
465 mc_serialize_stream_msg_t *stream_msgs;
467 /* Vector global message index into per stream message index. */
468 u32 *stream_msg_index_by_global_index;
470 /* Hashed by message name. */
471 uword *stream_msg_index_by_name;
473 u64 user_requests_sent;
474 u64 user_requests_received;
478 mc_stream_free (mc_stream_t * s)
480 pool_free (s->retry_pool);
481 hash_free (s->retry_index_by_local_sequence);
482 clib_fifo_free (s->catchup_fifo);
483 pool_free (s->peers);
484 mhash_free (&s->peer_index_by_id);
485 vec_free (s->procs_waiting_for_join_done);
486 vec_free (s->procs_waiting_for_open_window);
490 mc_stream_init (mc_stream_t * s)
492 memset (s, 0, sizeof (s[0]));
493 s->retry_head_index = s->retry_tail_index = ~0;
500 u8 *catchup_snapshot;
501 } mc_catchup_process_arg_t;
505 MC_RELAY_STATE_NEGOTIATE,
506 MC_RELAY_STATE_MASTER,
507 MC_RELAY_STATE_SLAVE,
512 mc_peer_id_t peer_id;
514 f64 time_last_master_assert_received;
515 } mc_mastership_peer_t;
521 } mc_stream_and_buffer_t;
523 typedef struct mc_main_t
525 mc_relay_state_t relay_state;
528 u32 we_can_be_relay_master;
530 u64 relay_master_peer_id;
532 mc_mastership_peer_t *mastership_peers;
534 /* Map of 64 bit id to index in stream pool. */
535 mhash_t mastership_peer_index_by_id;
537 /* The transport we're using. */
538 mc_transport_t transport;
540 /* Last-used global sequence number. */
541 u32 relay_global_sequence;
543 /* Vector of streams. */
544 mc_stream_t *stream_vector;
546 /* Hash table mapping stream name to pool index. */
547 uword *stream_index_by_name;
549 uword *procs_waiting_for_stream_name_by_name;
551 vlib_one_time_waiting_process_t **procs_waiting_for_stream_name_pool;
553 int joins_in_progress;
555 mc_catchup_process_arg_t *catchup_process_args;
557 /* Node indices for mastership, join ager,
558 retry and catchup processes. */
559 u32 mastership_process;
560 u32 join_ager_process;
563 u32 unserialize_process;
565 /* Global vector of messages. */
566 mc_serialize_msg_t **global_msgs;
568 /* Hash table mapping message name to index. */
569 uword *global_msg_index_by_name;
571 /* Shared serialize/unserialize main. */
572 serialize_main_t serialize_mains[VLIB_N_RX_TX];
574 vlib_serialize_buffer_main_t serialize_buffer_mains[VLIB_N_RX_TX];
576 /* Convenience variables */
577 struct vlib_main_t *vlib_main;
578 elog_main_t *elog_main;
580 /* Maps 64 bit peer id to elog string table offset for this formatted peer id. */
581 mhash_t elog_id_by_peer_id;
583 uword *elog_id_by_msg_name;
585 /* For mc_unserialize. */
586 mc_stream_and_buffer_t *mc_unserialize_stream_and_buffers;
589 always_inline mc_stream_t *
590 mc_stream_by_name (mc_main_t * m, char *name)
592 uword *p = hash_get (m->stream_index_by_name, name);
593 return p ? vec_elt_at_index (m->stream_vector, p[0]) : 0;
596 always_inline mc_stream_t *
597 mc_stream_by_index (mc_main_t * m, u32 i)
599 return i < vec_len (m->stream_vector) ? m->stream_vector + i : 0;
603 mc_clear_stream_stats (mc_main_t * m)
607 vec_foreach (s, m->stream_vector)
609 s->stats_last_clear = s->stats;
611 pool_foreach (p, s->peers, ({
612 p->stats_last_clear = p->stats;
618 /* Declare all message handlers. */
619 #define _(f) void mc_msg_##f##_handler (mc_main_t * mcm, mc_msg_##f##_t * msg, u32 buffer_index);
622 u32 mc_stream_join (mc_main_t * mcm, mc_stream_config_t *);
624 void mc_stream_leave (mc_main_t * mcm, u32 stream_index);
626 void mc_wait_for_stream_ready (mc_main_t * m, char *stream_name);
628 u32 mc_stream_send (mc_main_t * mcm, u32 stream_index, u32 buffer_index);
630 void mc_main_init (mc_main_t * mcm, char *tag);
632 void mc_enable_disable_mastership (mc_main_t * mcm, int we_can_be_master);
634 void *mc_get_vlib_buffer (struct vlib_main_t *vm, u32 n_bytes,
637 format_function_t format_mc_main;
639 clib_error_t *mc_serialize_internal (mc_main_t * mc,
641 u32 multiple_messages_per_vlib_buffer,
642 mc_serialize_msg_t * msg, ...);
644 clib_error_t *mc_serialize_va (mc_main_t * mc,
646 u32 multiple_messages_per_vlib_buffer,
647 mc_serialize_msg_t * msg, va_list * va);
649 #define mc_serialize_stream(mc,si,msg,args...) \
650 mc_serialize_internal((mc),(si),(0),(msg),(msg)->serialize,args)
652 #define mc_serialize(mc,msg,args...) \
653 mc_serialize_internal((mc),(~0),(0),(msg),(msg)->serialize,args)
655 #define mc_serialize2(mc,add,msg,args...) \
656 mc_serialize_internal((mc),(~0),(add),(msg),(msg)->serialize,args)
658 void mc_unserialize (mc_main_t * mcm, mc_stream_t * s, u32 buffer_index);
659 uword mc_unserialize_message (mc_main_t * mcm, mc_stream_t * s,
660 serialize_main_t * m);
662 serialize_function_t serialize_mc_main, unserialize_mc_main;
665 mc_max_message_size_in_bytes (mc_main_t * mcm)
667 return mcm->transport.max_packet_size - sizeof (mc_msg_user_request_t);
671 mc_serialize_n_bytes_left (mc_main_t * mcm, serialize_main_t * m)
673 return mc_max_message_size_in_bytes (mcm) -
674 serialize_vlib_buffer_n_bytes (m);
677 void unserialize_mc_stream (serialize_main_t * m, va_list * va);
678 void mc_stream_join_process_hold (void);
680 #endif /* included_vlib_mc_h */
683 * fd.io coding-style-patch-verification: ON
686 * eval: (c-set-style "gnu")