summaryrefslogtreecommitdiffstats
path: root/src/plugins/lacp/node.h
blob: 59af66f20d06e0f7b9d9cf8a4428e68f43c55602 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
/*
 * Copyright (c) 2017 Cisco and/or its affiliates.
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at:
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
 */
#ifndef __included_lacp_node_h__
#define __included_lacp_node_h__

#include <vlib/vlib.h>
#include <vlib/unix/unix.h>
#include <vppinfra/format.h>
#include <vppinfra/hash.h>
#include <lacp/protocol.h>
#include <lacp/rx_machine.h>
#include <lacp/tx_machine.h>
#include <lacp/mux_machine.h>
#include <lacp/ptx_machine.h>

typedef enum
{
  LACP_PACKET_TEMPLATE_ETHERNET,
  LACP_N_PACKET_TEMPLATES,
} lacp_packet_template_id_t;

typedef enum
{
  MARKER_PACKET_TEMPLATE_ETHERNET,
  MARKER_N_PACKET_TEMPLATES,
} marker_packet_template_id_t;

typedef enum
{
  LACP_PROCESS_EVENT_START = 1,
  LACP_PROCESS_EVENT_STOP = 2,
} lacp_process_event_t;

#define LACP_DBG(n, args...)			\
  {						\
    lacp_main_t *_lm = &lacp_main;              \
    if (_lm->debug || n->debug)			\
      clib_warning (args);			\
  }

#define LACP_DBG2(n, e, s, m, t)   		  \
  {						  \
    lacp_main_t *_lm = &lacp_main;                \
    if ((m)->debug && (_lm->debug || (n)->debug)) \
      (*m->debug)(n, e, s, t);			  \
  }

/* Packet counters */
#define foreach_lacp_error                                               \
_ (NONE, "good lacp packets -- consumed")	                         \
_ (CACHE_HIT, "good lacp packets -- cache hit")                          \
_ (UNSUPPORTED, "unsupported slow protocol packets")                     \
_ (TOO_SMALL, "bad lacp packets -- packet too small")                    \
_ (BAD_TLV, "bad lacp packets -- bad TLV length")                        \
_ (DISABLED, "lacp packets received on disabled interfaces")

typedef enum
{
#define _(sym,str) LACP_ERROR_##sym,
  foreach_lacp_error
#undef _
    LACP_N_ERROR,
} lacp_error_t;

#define SECS_IN_A_DAY 86400.0

/* lacp packet trace capture */
typedef struct
{
  u32 sw_if_index;
  u32 len;
  union
  {
    marker_pdu_t marker;
    lacp_pdu_t lacpdu;
  } pkt;
} lacp_input_trace_t;

/** LACP interface details struct */
typedef struct
{
  u32 sw_if_index;
  u8 interface_name[64];
  u32 rx_state;
  u32 tx_state;
  u32 mux_state;
  u32 ptx_state;
  u8 bond_interface_name[64];
  u16 actor_system_priority;
  u8 actor_system[6];
  u16 actor_key;
  u16 actor_port_priority;
  u16 actor_port_number;
  u8 actor_state;
  u16 partner_system_priority;
  u8 partner_system[6];
  u16 partner_key;
  u16 partner_port_priority;
  u16 partner_port_number;
  u8 partner_state;
} lacp_interface_details_t;

typedef struct
{
  /** API message ID base */
  u16 msg_id_base;

  /* convenience variables */
  vlib_main_t *vlib_main;
  vnet_main_t *vnet_main;

  /* Background process node index */
  u32 lacp_process_node_index;

  /* Packet templates for different encap types */
  vlib_packet_template_t packet_templates[LACP_N_PACKET_TEMPLATES];

  /* Packet templates for different encap types */
  vlib_packet_template_t marker_packet_templates[MARKER_N_PACKET_TEMPLATES];

  /* LACP interface count */
  volatile u32 lacp_int;

  /* debug is on or off */
  u8 debug;
} lacp_main_t;

extern lacp_state_struct lacp_state_array[];
extern lacp_main_t lacp_main;

void lacp_create_periodic_process (void);
clib_error_t *lacp_plugin_api_hookup (vlib_main_t * vm);
int lacp_dump_ifs (lacp_interface_details_t ** out_bondids);
lacp_error_t lacp_input (vlib_main_t * vm, vlib_buffer_t * b0, u32 bi0);
void lacp_periodic (vlib_main_t * vm);
u8 *lacp_input_format_trace (u8 * s, va_list * args);
void lacp_init_neighbor (slave_if_t * sif, u8 * hw_address,
			 u16 port_number, u32 group);
void lacp_init_state_machines (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_rx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_tx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_ptx_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_init_mux_machine (vlib_main_t * vm, slave_if_t * sif);
void lacp_selection_logic (vlib_main_t * vm, slave_if_t * sif);
void lacp_send_lacp_pdu (vlib_main_t * vm, slave_if_t * sif);

static inline void
lacp_stop_timer (f64 * timer)
{
  *timer = 0.0;
}

static inline u8
lacp_timer_is_running (f64 timer)
{
  return (timer != 0.0);
}

static inline u8
lacp_timer_is_expired (vlib_main_t * vm, f64 timer)
{
  f64 now = vlib_time_now (vm);

  return (now >= timer);
}

static inline u8 *
format_rx_sm_state (u8 * s, va_list * args)
{
  lacp_state_struct lacp_rx_sm_state_array[] = {
#define _(b, s, n) {.bit = b, .str = #s, },
    foreach_lacp_rx_sm_state
#undef _
    {.str = NULL}
  };
  int state = va_arg (*args, int);
  lacp_state_struct *state_entry = lacp_rx_sm_state_array;

  if (state >= (sizeof (lacp_rx_sm_state_array) / sizeof (*state_entry)))
    s = format (s, "Bad state %d", state);
  else
    s = format (s, "%s", state_entry[state].str);

  return s;
}

static inline u8 *
format_tx_sm_state (u8 * s, va_list * args)
{
  lacp_state_struct lacp_tx_sm_state_array[] = {
#define _(b, s, n) {.bit = b, .str = #s, },
    foreach_lacp_tx_sm_state
#undef _
    {.str = NULL}
  };
  int state = va_arg (*args, int);
  lacp_state_struct *state_entry = lacp_tx_sm_state_array;

  if (state >= (sizeof (lacp_tx_sm_state_array) / sizeof (*state_entry)))
    s = format (s, "Bad state %d", state);
  else
    s = format (s, "%s", state_entry[state].str);

  return s;
}

static inline u8 *
format_mux_sm_state (u8 * s, va_list * args)
{
  lacp_state_struct lacp_mux_sm_state_array[] = {
#define _(b, s, n) {.bit = b, .str = #s, },
    foreach_lacp_mux_sm_state
#undef _
    {.str = NULL}
  };
  int state = va_arg (*args, int);
  lacp_state_struct *state_entry = lacp_mux_sm_state_array;

  if (state >= (sizeof (lacp_mux_sm_state_array) / sizeof (*state_entry)))
    s = format (s, "Bad state %d", state);
  else
    s = format (s, "%s", state_entry[state].str);

  return s;
}

static inline u8 *
format_ptx_sm_state (u8 * s, va_list * args)
{
  lacp_state_struct lacp_ptx_sm_state_array[] = {
#define _(b, s, n) {.bit = b, .str = #s, },
    foreach_lacp_ptx_sm_state
#undef _
    {.str = NULL}
  };
  int state = va_arg (*args, int);
  lacp_state_struct *state_entry = lacp_ptx_sm_state_array;

  if (state >= (sizeof (lacp_ptx_sm_state_array) / sizeof (*state_entry)))
    s = format (s, "Bad state %d", state);
  else
    s = format (s, "%s", state_entry[state].str);

  return s;
}

static inline int
lacp_bit_test (u8 val, u8 bit)
{
  if (val & (1 << bit))
    return 1;
  else
    return 0;
}

#endif /* __included_lacp_node_h__ */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
eturn -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_PROFILE_ADD_DEL, mp); clib_memcpy (mp->name, name, vec_len (name)); mp->is_add = is_add; vec_free (name); S (mp); W (ret); return ret; } static int api_ikev2_profile_set_auth (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_profile_set_auth_t *mp; u8 *name = 0; u8 *data = 0; u32 auth_method = 0; u8 is_hex = 0; int ret; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "name %U", unformat_token, valid_chars, &name)) vec_add1 (name, 0); else if (unformat (i, "auth_method %U", unformat_ikev2_auth_method, &auth_method)) ; else if (unformat (i, "auth_data 0x%U", unformat_hex_string, &data)) is_hex = 1; else if (unformat (i, "auth_data %v", &data)) ; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } if (!vec_len (data)) { errmsg ("auth_data must be specified"); return -99; } if (!auth_method) { errmsg ("auth_method must be specified"); return -99; } M (IKEV2_PROFILE_SET_AUTH, mp); mp->is_hex = is_hex; mp->auth_method = (u8) auth_method; mp->data_len = vec_len (data); clib_memcpy (mp->name, name, vec_len (name)); clib_memcpy (mp->data, data, vec_len (data)); vec_free (name); vec_free (data); S (mp); W (ret); return ret; } static int api_ikev2_profile_set_id (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_profile_set_id_t *mp; u8 *name = 0; u8 *data = 0; u8 is_local = 0; u32 id_type = 0; ip4_address_t ip4; int ret; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "name %U", unformat_token, valid_chars, &name)) vec_add1 (name, 0); else if (unformat (i, "id_type %U", unformat_ikev2_id_type, &id_type)) ; else if (unformat (i, "id_data %U", unformat_ip4_address, &ip4)) { data = vec_new (u8, 4); clib_memcpy (data, ip4.as_u8, 4); } else if (unformat (i, "id_data 0x%U", unformat_hex_string, &data)) ; else if (unformat (i, "id_data %v", &data)) ; else if (unformat (i, "local")) is_local = 1; else if (unformat (i, "remote")) is_local = 0; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } if (!vec_len (data)) { errmsg ("id_data must be specified"); return -99; } if (!id_type) { errmsg ("id_type must be specified"); return -99; } M (IKEV2_PROFILE_SET_ID, mp); mp->is_local = is_local; mp->id_type = (u8) id_type; mp->data_len = vec_len (data); clib_memcpy (mp->name, name, vec_len (name)); clib_memcpy (mp->data, data, vec_len (data)); vec_free (name); vec_free (data); S (mp); W (ret); return ret; } static int api_ikev2_profile_set_ts (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_profile_set_ts_t *mp; u8 *name = 0; u8 is_local = 0; u32 proto = 0, start_port = 0, end_port = (u32) ~ 0; ip4_address_t start_addr, end_addr; const char *valid_chars = "a-zA-Z0-9_"; int ret; start_addr.as_u32 = 0; end_addr.as_u32 = (u32) ~ 0; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "name %U", unformat_token, valid_chars, &name)) vec_add1 (name, 0); else if (unformat (i, "protocol %d", &proto)) ; else if (unformat (i, "start_port %d", &start_port)) ; else if (unformat (i, "end_port %d", &end_port)) ; else if (unformat (i, "start_addr %U", unformat_ip4_address, &start_addr)) ; else if (unformat (i, "end_addr %U", unformat_ip4_address, &end_addr)) ; else if (unformat (i, "local")) is_local = 1; else if (unformat (i, "remote")) is_local = 0; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_PROFILE_SET_TS, mp); mp->is_local = is_local; mp->proto = (u8) proto; mp->start_port = (u16) start_port; mp->end_port = (u16) end_port; mp->start_addr = start_addr.as_u32; mp->end_addr = end_addr.as_u32; clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); S (mp); W (ret); return ret; } static int api_ikev2_set_local_key (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_set_local_key_t *mp; u8 *file = 0; int ret; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "file %v", &file)) vec_add1 (file, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (file)) { errmsg ("RSA key file must be specified"); return -99; } if (vec_len (file) > 256) { errmsg ("file name too long"); return -99; } M (IKEV2_SET_LOCAL_KEY, mp); clib_memcpy (mp->key_file, file, vec_len (file)); vec_free (file); S (mp); W (ret); return ret; } static int api_ikev2_set_tunnel_interface (vat_main_t * vam) { return (0); } static int api_ikev2_set_responder (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_set_responder_t *mp; int ret; u8 *name = 0; u32 sw_if_index = ~0; ip4_address_t address; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%U interface %d address %U", unformat_token, valid_chars, &name, &sw_if_index, unformat_ip4_address, &address)) vec_add1 (name, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_SET_RESPONDER, mp); clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); mp->sw_if_index = sw_if_index; clib_memcpy (mp->address, &address, sizeof (address)); S (mp); W (ret); return ret; } static int api_ikev2_set_ike_transforms (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_set_ike_transforms_t *mp; int ret; u8 *name = 0; u32 crypto_alg, crypto_key_size, integ_alg, dh_group; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name, &crypto_alg, &crypto_key_size, &integ_alg, &dh_group)) vec_add1 (name, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_SET_IKE_TRANSFORMS, mp); clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); mp->crypto_alg = crypto_alg; mp->crypto_key_size = crypto_key_size; mp->integ_alg = integ_alg; mp->dh_group = dh_group; S (mp); W (ret); return ret; } static int api_ikev2_set_esp_transforms (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_set_esp_transforms_t *mp; int ret; u8 *name = 0; u32 crypto_alg, crypto_key_size, integ_alg, dh_group; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%U %d %d %d %d", unformat_token, valid_chars, &name, &crypto_alg, &crypto_key_size, &integ_alg, &dh_group)) vec_add1 (name, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_SET_ESP_TRANSFORMS, mp); clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); mp->crypto_alg = crypto_alg; mp->crypto_key_size = crypto_key_size; mp->integ_alg = integ_alg; mp->dh_group = dh_group; S (mp); W (ret); return ret; } static int api_ikev2_set_sa_lifetime (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_set_sa_lifetime_t *mp; int ret; u8 *name = 0; u64 lifetime, lifetime_maxdata; u32 lifetime_jitter, handover; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%U %lu %u %u %lu", unformat_token, valid_chars, &name, &lifetime, &lifetime_jitter, &handover, &lifetime_maxdata)) vec_add1 (name, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_SET_SA_LIFETIME, mp); clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); mp->lifetime = lifetime; mp->lifetime_jitter = lifetime_jitter; mp->handover = handover; mp->lifetime_maxdata = lifetime_maxdata; S (mp); W (ret); return ret; } static int api_ikev2_initiate_sa_init (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_initiate_sa_init_t *mp; int ret; u8 *name = 0; const char *valid_chars = "a-zA-Z0-9_"; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%U", unformat_token, valid_chars, &name)) vec_add1 (name, 0); else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } if (!vec_len (name)) { errmsg ("profile name must be specified"); return -99; } if (vec_len (name) > 64) { errmsg ("profile name too long"); return -99; } M (IKEV2_INITIATE_SA_INIT, mp); clib_memcpy (mp->name, name, vec_len (name)); vec_free (name); S (mp); W (ret); return ret; } static int api_ikev2_initiate_del_ike_sa (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_initiate_del_ike_sa_t *mp; int ret; u64 ispi; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%lx", &ispi)) ; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } M (IKEV2_INITIATE_DEL_IKE_SA, mp); mp->ispi = ispi; S (mp); W (ret); return ret; } static int api_ikev2_initiate_del_child_sa (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_initiate_del_child_sa_t *mp; int ret; u32 ispi; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%x", &ispi)) ; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } M (IKEV2_INITIATE_DEL_CHILD_SA, mp); mp->ispi = ispi; S (mp); W (ret); return ret; } static int api_ikev2_initiate_rekey_child_sa (vat_main_t * vam) { unformat_input_t *i = vam->input; vl_api_ikev2_initiate_rekey_child_sa_t *mp; int ret; u32 ispi; while (unformat_check_input (i) != UNFORMAT_END_OF_INPUT) { if (unformat (i, "%x", &ispi)) ; else { errmsg ("parse error '%U'", format_unformat_error, i); return -99; } } M (IKEV2_INITIATE_REKEY_CHILD_SA, mp); mp->ispi = ispi; S (mp); W (ret); return ret; } #include <ikev2/ikev2.api_test.c> /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */