summaryrefslogtreecommitdiffstats
path: root/src/vnet/handoff.h
blob: f50b86d5c6d0166ec98a241efd5a2ca991419563 (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
/*
 * Copyright (c) 2016 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_vnet_handoff_h
#define included_vnet_handoff_h

#include <vlib/vlib.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ip/ip4_packet.h>
#include <vnet/ip/ip6_packet.h>
#include <vnet/mpls/packet.h>

static inline u64
ipv4_get_key (ip4_header_t * ip)
{
  u64 hash_key;

  hash_key = *((u64 *) (&ip->address_pair)) ^ ip->protocol;

  return hash_key;
}

static inline u64
ipv6_get_key (ip6_header_t * ip)
{
  u64 hash_key;

  hash_key = ip->src_address.as_u64[0] ^
    rotate_left (ip->src_address.as_u64[1], 13) ^
    rotate_left (ip->dst_address.as_u64[0], 26) ^
    rotate_left (ip->dst_address.as_u64[1], 39) ^ ip->protocol;

  return hash_key;
}

#define MPLS_BOTTOM_OF_STACK_BIT_MASK   0x00000100U
#define MPLS_LABEL_MASK                 0xFFFFF000U

static inline u64
mpls_get_key (mpls_unicast_header_t * m)
{
  u64 hash_key;
  u8 ip_ver;


  /* find the bottom of the MPLS label stack. */
  if (PREDICT_TRUE (m->label_exp_s_ttl &
		    clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)))
    {
      goto bottom_lbl_found;
    }
  m++;

  if (PREDICT_TRUE (m->label_exp_s_ttl &
		    clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK)))
    {
      goto bottom_lbl_found;
    }
  m++;

  if (m->label_exp_s_ttl &
      clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
    {
      goto bottom_lbl_found;
    }
  m++;

  if (m->label_exp_s_ttl &
      clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
    {
      goto bottom_lbl_found;
    }
  m++;

  if (m->label_exp_s_ttl &
      clib_net_to_host_u32 (MPLS_BOTTOM_OF_STACK_BIT_MASK))
    {
      goto bottom_lbl_found;
    }

  /* the bottom label was not found - use the last label */
  hash_key = m->label_exp_s_ttl & clib_net_to_host_u32 (MPLS_LABEL_MASK);

  return hash_key;

bottom_lbl_found:
  m++;
  ip_ver = (*((u8 *) m) >> 4);

  /* find out if it is IPV4 or IPV6 header */
  if (PREDICT_TRUE (ip_ver == 4))
    {
      hash_key = ipv4_get_key ((ip4_header_t *) m);
    }
  else if (PREDICT_TRUE (ip_ver == 6))
    {
      hash_key = ipv6_get_key ((ip6_header_t *) m);
    }
  else
    {
      /* use the bottom label */
      hash_key =
	(m - 1)->label_exp_s_ttl & clib_net_to_host_u32 (MPLS_LABEL_MASK);
    }

  return hash_key;

}

static inline u64
eth_get_sym_key (ethernet_header_t * h0)
{
  u64 hash_key;

  if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
    {
      ip4_header_t *ip = (ip4_header_t *) (h0 + 1);
      hash_key =
	(u64) (ip->src_address.as_u32 ^
	       ip->dst_address.as_u32 ^ ip->protocol);
    }
  else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
    {
      ip6_header_t *ip = (ip6_header_t *) (h0 + 1);
      hash_key = (u64) (ip->src_address.as_u64[0] ^
			ip->src_address.as_u64[1] ^
			ip->dst_address.as_u64[0] ^
			ip->dst_address.as_u64[1] ^ ip->protocol);
    }
  else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
    {
      hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1));
    }
  else
    if (PREDICT_FALSE
	((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN))
	 || (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD))))
    {
      ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1);

      outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ?
	outer + 1 : outer;
      if (PREDICT_TRUE (outer->type) ==
	  clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
	{
	  ip4_header_t *ip = (ip4_header_t *) (outer + 1);
	  hash_key =
	    (u64) (ip->src_address.as_u32 ^
		   ip->dst_address.as_u32 ^ ip->protocol);
	}
      else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
	{
	  ip6_header_t *ip = (ip6_header_t *) (outer + 1);
	  hash_key =
	    (u64) (ip->src_address.as_u64[0] ^ ip->src_address.as_u64[1] ^
		   ip->dst_address.as_u64[0] ^
		   ip->dst_address.as_u64[1] ^ ip->protocol);
	}
      else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
	{
	  hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1));
	}
      else
	{
	  hash_key = outer->type;
	}
    }
  else
    {
      hash_key = 0;
    }

  return hash_key;
}

static inline u64
eth_get_key (ethernet_header_t * h0)
{
  u64 hash_key;

  if (PREDICT_TRUE (h0->type) == clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
    {
      hash_key = ipv4_get_key ((ip4_header_t *) (h0 + 1));
    }
  else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
    {
      hash_key = ipv6_get_key ((ip6_header_t *) (h0 + 1));
    }
  else if (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
    {
      hash_key = mpls_get_key ((mpls_unicast_header_t *) (h0 + 1));
    }
  else if ((h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ||
	   (h0->type == clib_host_to_net_u16 (ETHERNET_TYPE_DOT1AD)))
    {
      ethernet_vlan_header_t *outer = (ethernet_vlan_header_t *) (h0 + 1);

      outer = (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_VLAN)) ?
	outer + 1 : outer;
      if (PREDICT_TRUE (outer->type) ==
	  clib_host_to_net_u16 (ETHERNET_TYPE_IP4))
	{
	  hash_key = ipv4_get_key ((ip4_header_t *) (outer + 1));
	}
      else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_IP6))
	{
	  hash_key = ipv6_get_key ((ip6_header_t *) (outer + 1));
	}
      else if (outer->type == clib_host_to_net_u16 (ETHERNET_TYPE_MPLS))
	{
	  hash_key = mpls_get_key ((mpls_unicast_header_t *) (outer + 1));
	}
      else
	{
	  hash_key = outer->type;
	}
    }
  else
    {
      hash_key = 0;
    }

  return hash_key;
}

#endif /* included_vnet_handoff_h */

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
an class="n">punt_reg_mk_key (pr->pr_reason, pr->pr_node_index)); } /** * reconstruct the DP per-reason DB */ static void punt_reg_mk_dp (vlib_punt_reason_t reason) { u32 pri, *prip, *pris; const punt_reg_t *pr; u16 *edges, *old; u64 key; pris = NULL; edges = NULL; vec_validate (punt_dp_db, reason); old = punt_dp_db[reason]; /* *INDENT-OFF* */ hash_foreach (key, pri, punt_reg_db, ({ vec_add1(pris, pri); })); /* *INDENT-ON* */ /* * A check for an empty vector is done in the DP, so the a zero * length vector here is ok */ vec_foreach (prip, pris) { pr = pool_elt_at_index (punt_reg_pool, *prip); if (pr->pr_reason == reason) vec_add1 (edges, pr->pr_edge); } /* atomic update of the DP */ punt_dp_db[reason] = edges; vec_free (old); } int vlib_punt_register (vlib_punt_hdl_t client, vlib_punt_reason_t reason, const char *node_name) { vlib_node_t *punt_to, *punt_from; punt_client_t *pc; vlib_main_t *vm; punt_reg_t *pr; u32 pri; if (reason >= punt_reason_last) return -1; if (!punt_validate_client (client)) return -2; vm = vlib_get_main (); pc = pool_elt_at_index (punt_client_pool, client); punt_to = vlib_get_node_by_name (vm, (u8 *) node_name); punt_from = vlib_get_node_by_name (vm, (u8 *) "punt-dispatch"); /* * find a global matching registration */ pri = punt_reg_find (reason, punt_to->index); if (~0 != pri) { u32 pos; pos = vec_search (pc->pc_regs, pri); if (~0 != pos) { /* duplicate registration for this client */ return -1; } pr = pool_elt_at_index (punt_reg_pool, pri); } else { pool_get (punt_reg_pool, pr); pr->pr_reason = reason; pr->pr_node_index = punt_to->index; pr->pr_edge = vlib_node_add_next (vm, punt_from->index, pr->pr_node_index); pri = pr - punt_reg_pool; if (0 == punt_reason_data[reason].pd_users++ && NULL != punt_reason_data[reason].pd_fn) punt_reason_data[reason].pd_fn (VLIB_ENABLE, punt_reason_data[reason].pd_data); punt_reg_add (pr); } /* * add this reg to the list the client has made */ pr->pr_locks++; vec_add1 (pc->pc_regs, pri); punt_reg_mk_dp (reason); return 0; } int vlib_punt_unregister (vlib_punt_hdl_t client, vlib_punt_reason_t reason, const char *node_name) { vlib_node_t *punt_to; punt_client_t *pc; vlib_main_t *vm; punt_reg_t *pr; u32 pri; if (reason >= punt_reason_last) return -1; vm = vlib_get_main (); pc = pool_elt_at_index (punt_client_pool, client); punt_to = vlib_get_node_by_name (vm, (u8 *) node_name); /* * construct a registration and check if it's one this client already has */ pri = punt_reg_find (reason, punt_to->index); if (~0 != pri) { u32 pos; pos = vec_search (pc->pc_regs, pri); if (~0 == pos) { /* not a registration for this client */ return -1; } vec_del1 (pc->pc_regs, pos); pr = pool_elt_at_index (punt_reg_pool, pri); pr->pr_locks--; if (0 == pr->pr_locks) { if (0 == --punt_reason_data[reason].pd_users && NULL != punt_reason_data[reason].pd_fn) punt_reason_data[reason].pd_fn (VLIB_DISABLE, punt_reason_data[reason].pd_data); punt_reg_remove (pr); pool_put (punt_reg_pool, pr); } } /* * rebuild the DP data-base */ punt_reg_mk_dp (reason); return (0); } int vlib_punt_reason_validate (vlib_punt_reason_t reason) { if (reason < punt_reason_last) return (0); return (-1); } int vlib_punt_reason_alloc (vlib_punt_hdl_t client, const char *reason_name, punt_interested_listener_t fn, void *data, vlib_punt_reason_t * reason) { vlib_punt_reason_t new; if (!punt_validate_client (client)) return -2; new = punt_reason_last++; vec_validate (punt_reason_data, new); punt_reason_data[new].pd_name = format (NULL, "%s", reason_name); punt_reason_data[new].pd_reason = new; punt_reason_data[new].pd_fn = fn; punt_reason_data[new].pd_data = data; vec_add1 (punt_reason_data[new].pd_owners, client); vlib_validate_combined_counter (&punt_counters, new); vlib_zero_combined_counter (&punt_counters, new); *reason = new; /* build the DP data-base */ punt_reg_mk_dp (*reason); return (0); } void punt_reason_walk (punt_reason_walk_cb_t cb, void *ctx) { punt_reason_data_t *pd; vec_foreach (pd, punt_reason_data) { cb (pd->pd_reason, pd->pd_name, ctx); } } /* Parse node name -> node index. */ uword unformat_punt_client (unformat_input_t * input, va_list * args) { u32 *result = va_arg (*args, u32 *); return unformat_user (input, unformat_hash_vec_string, punt_client_db, result); } u8 * format_punt_reg (u8 * s, va_list * args) { u32 pri = va_arg (*args, u32); punt_reg_t *pr; pr = pool_elt_at_index (punt_reg_pool, pri); s = format (s, "%U -> %U", format_vlib_punt_reason, pr->pr_reason, format_vlib_node_name, vlib_get_main (), pr->pr_node_index); return (s); } u8 * format_punt_reason_data (u8 * s, va_list * args) { punt_reason_data_t *pd = va_arg (*args, punt_reason_data_t *); punt_client_t *pc; u32 *pci; s = format (s, "[%d] %v from:[", pd->pd_reason, pd->pd_name); vec_foreach (pci, pd->pd_owners) { pc = pool_elt_at_index (punt_client_pool, *pci); s = format (s, "%v ", pc->pc_name); } s = format (s, "]"); return (s); } u8 * format_punt_client (u8 * s, va_list * args) { u32 pci = va_arg (*args, u32); punt_format_flags_t flags = va_arg (*args, punt_format_flags_t); punt_client_t *pc; pc = pool_elt_at_index (punt_client_pool, pci); s = format (s, "%v", pc->pc_name); if (flags & PUNT_FORMAT_FLAG_DETAIL) { punt_reason_data_t *pd; u32 *pri; s = format (s, "\n registrations:"); vec_foreach (pri, pc->pc_regs) { s = format (s, "\n [%U]", format_punt_reg, *pri); } s = format (s, "\n reasons:"); vec_foreach (pd, punt_reason_data) { u32 *tmp; vec_foreach (tmp, pd->pd_owners) { if (*tmp == pci) s = format (s, "\n %U", format_punt_reason_data, pd); } } } return (s); } static clib_error_t * punt_client_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u32 pci = ~0; while (unformat_check_input (input) != UNFORMAT_END_OF_INPUT) { if (unformat (input, "%U", unformat_punt_client, &pci)) ; else break; } if (~0 != pci) { vlib_cli_output (vm, "%U", format_punt_client, pci, PUNT_FORMAT_FLAG_DETAIL); } else { u8 *name; /* *INDENT-OFF* */ hash_foreach(name, pci, punt_client_db, ({ vlib_cli_output (vm, "%U", format_punt_client, pci, PUNT_FORMAT_FLAG_NONE); })); /* *INDENT-ON* */ } return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (punt_client_show_command, static) = { .path = "show punt client", .short_help = "show client[s] registered with the punt infra", .function = punt_client_show, }; /* *INDENT-ON* */ static clib_error_t * punt_reason_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { const punt_reason_data_t *pd; vec_foreach (pd, punt_reason_data) { vlib_cli_output (vm, "%U", format_punt_reason_data, pd); } return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (punt_reason_show_command, static) = { .path = "show punt reasons", .short_help = "show all punt reasons", .function = punt_reason_show, }; /* *INDENT-ON* */ static clib_error_t * punt_db_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { u32 pri, ii, jj; u64 key; /* *INDENT-OFF* */ hash_foreach (key, pri, punt_reg_db, ({ vlib_cli_output (vm, " %U", format_punt_reg, pri); })); /* *INDENT-ON* */ vlib_cli_output (vm, "\nDerived data-plane data-base:"); vlib_cli_output (vm, " (for each punt-reason the edge[s] from punt-dispatch)"); vec_foreach_index (ii, punt_dp_db) { u8 *s = NULL; vlib_cli_output (vm, " %U", format_vlib_punt_reason, ii); vec_foreach_index (jj, punt_dp_db[ii]) { s = format (s, "%d ", punt_dp_db[ii][jj]); } vlib_cli_output (vm, " [%v]", s); vec_free (s); } return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (punt_db_show_command, static) = { .path = "show punt db", .short_help = "show the punt DB", .function = punt_db_show, }; /* *INDENT-ON* */ static clib_error_t * punt_stats_show (vlib_main_t * vm, unformat_input_t * input, vlib_cli_command_t * cmd) { vlib_combined_counter_main_t *cm = &punt_counters; vlib_counter_t c; u32 ii; for (ii = 0; ii < vlib_combined_counter_n_counters (cm); ii++) { vlib_get_combined_counter (cm, ii, &c); vlib_cli_output (vm, "%U packets:%lld bytes:%lld", format_vlib_punt_reason, ii, c.packets, c.bytes); } return (NULL); } /* *INDENT-OFF* */ VLIB_CLI_COMMAND (punt_stats_show_command, static) = { .path = "show punt stats", .short_help = "show the punt stats", .function = punt_stats_show, }; /* *INDENT-ON* */ static clib_error_t * punt_init (vlib_main_t * vm) { punt_client_db = hash_create_vec (0, sizeof (u8), sizeof (u32)); return (NULL); } VLIB_INIT_FUNCTION (punt_init); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */