summaryrefslogtreecommitdiffstats
path: root/src/vnet/flow/flow.c
blob: 9b6a376af3e382c2225a33bb37793cb54904bc92 (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
/*
 * Copyright (c) 2018 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.
 */

#include <vnet/vnet.h>
#include <vnet/ip/ip.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/flow/flow.h>

vnet_flow_main_t flow_main;

int
vnet_flow_get_range (vnet_main_t * vnm, char *owner, u32 count, u32 * start)
{
  vnet_flow_main_t *fm = &flow_main;
  vnet_flow_range_t *r;

  /* skip 0 */
  if (fm->flows_used == 0)
    fm->flows_used = 1;

  *start = fm->flows_used;
  fm->flows_used += count;
  vec_add2 (fm->ranges, r, 1);
  r->start = *start;
  r->count = count;
  r->owner = format (0, "%s%c", owner, 0);
  return 0;
}

int
vnet_flow_add (vnet_main_t * vnm, vnet_flow_t * flow, u32 * flow_index)
{
  vnet_flow_main_t *fm = &flow_main;
  vnet_flow_t *f;

  pool_get (fm->global_flow_pool, f);
  *flow_index = f - fm->global_flow_pool;
  clib_memcpy_fast (f, flow, sizeof (vnet_flow_t));
  f->private_data = 0;
  f->index = *flow_index;
  return 0;
}

vnet_flow_t *
vnet_get_flow (u32 flow_index)
{
  vnet_flow_main_t *fm = &flow_main;
  if (pool_is_free_index (fm->global_flow_pool, flow_index))
    return 0;

  return pool_elt_at_index (fm->global_flow_pool, flow_index);
}

int
vnet_flow_del (vnet_main_t * vnm, u32 flow_index)
{
  vnet_flow_main_t *fm = &flow_main;
  vnet_flow_t *f = vnet_get_flow (flow_index);
  uword hw_if_index;
  uword private_data;

  if (f == 0)
    return VNET_FLOW_ERROR_NO_SUCH_ENTRY;

  /* *INDENT-OFF* */
  hash_foreach (hw_if_index, private_data, f->private_data,
    ({
     vnet_flow_disable (vnm, flow_index, hw_if_index);
    }));
  /* *INDENT-ON* */

  hash_free (f->private_data);
  clib_memset (f, 0, sizeof (*f));
  pool_put (fm->global_flow_pool, f);
  return 0;
}

int
vnet_flow_enable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
{
  vnet_flow_t *f = vnet_get_flow (flow_index);
  vnet_hw_interface_t *hi;
  vnet_device_class_t *dev_class;
  uword private_data;
  int rv;

  if (f == 0)
    return VNET_FLOW_ERROR_NO_SUCH_ENTRY;

  if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
    return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;

  /* don't enable flow twice */
  if (hash_get (f->private_data, hw_if_index) != 0)
    return VNET_FLOW_ERROR_ALREADY_DONE;

  hi = vnet_get_hw_interface (vnm, hw_if_index);
  dev_class = vnet_get_device_class (vnm, hi->dev_class_index);

  if (dev_class->flow_ops_function == 0)
    return VNET_FLOW_ERROR_NOT_SUPPORTED;

  if (f->actions & VNET_FLOW_ACTION_REDIRECT_TO_NODE)
    {
      vnet_hw_interface_t *hw = vnet_get_hw_interface (vnm, hw_if_index);
      f->redirect_device_input_next_index =
	vlib_node_add_next (vnm->vlib_main, hw->input_node_index,
			    f->redirect_node_index);
    }

  rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_ADD_FLOW,
				     hi->dev_instance, flow_index,
				     &private_data);

  if (rv)
    return rv;

  hash_set (f->private_data, hw_if_index, private_data);
  return 0;
}

int
vnet_flow_disable (vnet_main_t * vnm, u32 flow_index, u32 hw_if_index)
{
  vnet_flow_t *f = vnet_get_flow (flow_index);
  vnet_hw_interface_t *hi;
  vnet_device_class_t *dev_class;
  uword *p;
  int rv;

  if (f == 0)
    return VNET_FLOW_ERROR_NO_SUCH_ENTRY;

  if (!vnet_hw_interface_is_valid (vnm, hw_if_index))
    return VNET_FLOW_ERROR_NO_SUCH_INTERFACE;

  /* don't disable if not enabled */
  if ((p = hash_get (f->private_data, hw_if_index)) == 0)
    return VNET_FLOW_ERROR_ALREADY_DONE;

  hi = vnet_get_hw_interface (vnm, hw_if_index);
  dev_class = vnet_get_device_class (vnm, hi->dev_class_index);

  rv = dev_class->flow_ops_function (vnm, VNET_FLOW_DEV_OP_DEL_FLOW,
				     hi->dev_instance, flow_index, p);

  if (rv)
    return rv;

  hash_unset (f->private_data, hw_if_index);
  return 0;
}

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
x1 (ip0, sum0); ip0->checksum = ~ip_csum_fold (sum0); ASSERT (ip4_header_checksum_is_valid (ip0)); } } } static void ip4_pg_edit_function (pg_main_t * pg, pg_stream_t * s, pg_edit_group_t * g, u32 * packets, u32 n_packets) { vlib_main_t *vm = vlib_get_main (); u32 ip_offset; ip_offset = g->start_byte_offset; switch (g->edit_function_opaque) { case IP4_PG_EDIT_LENGTH: compute_length_and_or_checksum (vm, packets, n_packets, ip_offset, IP4_PG_EDIT_LENGTH); break; case IP4_PG_EDIT_CHECKSUM: compute_length_and_or_checksum (vm, packets, n_packets, ip_offset, IP4_PG_EDIT_CHECKSUM); break; case IP4_PG_EDIT_LENGTH | IP4_PG_EDIT_CHECKSUM: compute_length_and_or_checksum (vm, packets, n_packets, ip_offset, IP4_PG_EDIT_LENGTH | IP4_PG_EDIT_CHECKSUM); break; default: ASSERT (0); break; } } typedef struct { pg_edit_t ip_version, header_length; pg_edit_t tos; pg_edit_t length; pg_edit_t fragment_id, fragment_offset; /* Flags together with fragment offset. */ pg_edit_t mf_flag, df_flag, ce_flag; pg_edit_t ttl; pg_edit_t protocol; pg_edit_t checksum; pg_edit_t src_address, dst_address; } pg_ip4_header_t; static inline void pg_ip4_header_init (pg_ip4_header_t * p) { /* Initialize fields that are not bit fields in the IP header. */ #define _(f) pg_edit_init (&p->f, ip4_header_t, f); _(tos); _(length); _(fragment_id); _(ttl); _(protocol); _(checksum); _(src_address); _(dst_address); #undef _ /* Initialize bit fields. */ pg_edit_init_bitfield (&p->header_length, ip4_header_t, ip_version_and_header_length, 0, 4); pg_edit_init_bitfield (&p->ip_version, ip4_header_t, ip_version_and_header_length, 4, 4); pg_edit_init_bitfield (&p->fragment_offset, ip4_header_t, flags_and_fragment_offset, 0, 13); pg_edit_init_bitfield (&p->mf_flag, ip4_header_t, flags_and_fragment_offset, 13, 1); pg_edit_init_bitfield (&p->df_flag, ip4_header_t, flags_and_fragment_offset, 14, 1); pg_edit_init_bitfield (&p->ce_flag, ip4_header_t, flags_and_fragment_offset, 15, 1); } uword unformat_pg_ip4_header (unformat_input_t * input, va_list * args) { pg_stream_t *s = va_arg (*args, pg_stream_t *); pg_ip4_header_t *p; u32 group_index; p = pg_create_edit_group (s, sizeof (p[0]), sizeof (ip4_header_t), &group_index); pg_ip4_header_init (p); /* Defaults. */ pg_edit_set_fixed (&p->ip_version, 4); pg_edit_set_fixed (&p->header_length, sizeof (ip4_header_t) / sizeof (u32)); pg_edit_set_fixed (&p->tos, 0); pg_edit_set_fixed (&p->ttl, 64); pg_edit_set_fixed (&p->fragment_id, 0); pg_edit_set_fixed (&p->fragment_offset, 0); pg_edit_set_fixed (&p->mf_flag, 0); pg_edit_set_fixed (&p->df_flag, 0); pg_edit_set_fixed (&p->ce_flag, 0); p->length.type = PG_EDIT_UNSPECIFIED; p->checksum.type = PG_EDIT_UNSPECIFIED; if (unformat (input, "%U: %U -> %U", unformat_pg_edit, unformat_ip_protocol, &p->protocol, unformat_pg_edit, unformat_ip4_address, &p->src_address, unformat_pg_edit, unformat_ip4_address, &p->dst_address)) goto found; if (!unformat (input, "%U:", unformat_pg_edit, unformat_ip_protocol, &p->protocol)) goto error; found: /* Parse options. */ while (1) { if (unformat (input, "version %U", unformat_pg_edit, unformat_pg_number, &p->ip_version)) ; else if (unformat (input, "header-length %U", unformat_pg_edit, unformat_pg_number, &p->header_length)) ; else if (unformat (input, "tos %U", unformat_pg_edit, unformat_pg_number, &p->tos)) ; else if (unformat (input, "length %U", unformat_pg_edit, unformat_pg_number, &p->length)) ; else if (unformat (input, "checksum %U", unformat_pg_edit, unformat_pg_number, &p->checksum)) ; else if (unformat (input, "ttl %U", unformat_pg_edit, unformat_pg_number, &p->ttl)) ; else if (unformat (input, "fragment id %U offset %U", unformat_pg_edit, unformat_pg_number, &p->fragment_id, unformat_pg_edit, unformat_pg_number, &p->fragment_offset)) { int i; for (i = 0; i < ARRAY_LEN (p->fragment_offset.values); i++) pg_edit_set_value (&p->fragment_offset, i, pg_edit_get_value (&p->fragment_offset, i) / 8); } /* Flags. */ else if (unformat (input, "mf") || unformat (input, "MF")) pg_edit_set_fixed (&p->mf_flag, 1); else if (unformat (input, "df") || unformat (input, "DF")) pg_edit_set_fixed (&p->df_flag, 1); else if (unformat (input, "ce") || unformat (input, "CE")) pg_edit_set_fixed (&p->ce_flag, 1); /* Can't parse input: try next protocol level. */ else break; } { ip_main_t *im = &ip_main; ip_protocol_t protocol; ip_protocol_info_t *pi; pi = 0; if (p->protocol.type == PG_EDIT_FIXED) { protocol = pg_edit_get_value (&p->protocol, PG_EDIT_LO); pi = ip_get_protocol_info (im, protocol); } if (pi && pi->unformat_pg_edit && unformat_user (input, pi->unformat_pg_edit, s)) ; else if (!unformat_user (input, unformat_pg_payload, s)) goto error; if (p->length.type == PG_EDIT_UNSPECIFIED && s->min_packet_bytes == s->max_packet_bytes && group_index + 1 < vec_len (s->edit_groups)) { pg_edit_set_fixed (&p->length, pg_edit_group_n_bytes (s, group_index)); } /* Compute IP header checksum if all edits are fixed. */ if (p->checksum.type == PG_EDIT_UNSPECIFIED) { ip4_header_t fixed_header, fixed_mask, cmp_mask; /* See if header is all fixed and specified except for checksum field. */ clib_memset (&cmp_mask, ~0, sizeof (cmp_mask)); cmp_mask.checksum = 0; pg_edit_group_get_fixed_packet_data (s, group_index, &fixed_header, &fixed_mask); if (!memcmp (&fixed_mask, &cmp_mask, sizeof (cmp_mask))) pg_edit_set_fixed (&p->checksum, clib_net_to_host_u16 (ip4_header_checksum (&fixed_header))); } p = pg_get_edit_group (s, group_index); if (p->length.type == PG_EDIT_UNSPECIFIED || p->checksum.type == PG_EDIT_UNSPECIFIED) { pg_edit_group_t *g = pg_stream_get_group (s, group_index); g->edit_function = ip4_pg_edit_function; g->edit_function_opaque = 0; if (p->length.type == PG_EDIT_UNSPECIFIED) g->edit_function_opaque |= IP4_PG_EDIT_LENGTH; if (p->checksum.type == PG_EDIT_UNSPECIFIED) g->edit_function_opaque |= IP4_PG_EDIT_CHECKSUM; } return 1; } error: /* Free up any edits we may have added. */ pg_free_edit_group (s); return 0; } /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */