aboutsummaryrefslogtreecommitdiffstats
path: root/src/plugins/ip_session_redirect/test_api.c
blob: e4026a673ffc4d15b3ad9b11428df4de52844c73 (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
/* Copyright (c) 2021-2022 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 <vlib/vlib.h>
#include <vnet/fib/fib_api.h>
#include <vnet/ip/ip_format_fns.h>
#include <vnet/classify/vnet_classify.h>
#include <vat/vat.h>
#include <vlibapi/api.h>
#include <vlibmemory/api.h>
#define __plugin_msg_base ip_session_redirect_test_main.msg_id_base
#include <vlibapi/vat_helper_macros.h>
/* declare message IDs */
#include "ip_session_redirect.api_enum.h"
#include "ip_session_redirect.api_types.h"
#include "ip_session_redirect.h"

typedef struct
{
  /* API message ID base */
  u16 msg_id_base;
  vat_main_t *vat_main;
} ip_session_redirect_test_main_t;

ip_session_redirect_test_main_t ip_session_redirect_test_main;

static int
api_ip_session_redirect_add_parse (vat_main_t *vam, u32 *table_index,
				   u32 *opaque_index, dpo_proto_t *proto,
				   int *is_punt, u8 **match,
				   fib_route_path_t **paths)
{
  vnet_classify_main_t *cm = &vnet_classify_main;
  fib_route_path_t path;

  *table_index = ~0;
  *opaque_index = ~0;
  *proto = DPO_PROTO_IP4;
  *is_punt = 0;
  *match = 0;
  *paths = 0;

  while (unformat_check_input (vam->input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (vam->input, "punt"))
	*is_punt = 1;
      else if (unformat (vam->input, "table %u", table_index))
	;
      else if (unformat (vam->input, "opaque-index %u", opaque_index))
	;
      else if (unformat (vam->input, "match %U", unformat_classify_match, cm,
			 match, *table_index))
	;
      else if (unformat (vam->input, "via %U", unformat_fib_route_path, &path,
			 proto))
	vec_add1 (*paths, path);
      else
	{
	  clib_warning ("unknown input `%U'", format_unformat_error,
			vam->input);
	  return -99;
	}
    }

  return 0;
}

static int
api_ip_session_redirect_add (vat_main_t *vam)
{
  vl_api_ip_session_redirect_add_t *mp;
  fib_route_path_t *paths;
  dpo_proto_t proto;
  u32 opaque_index;
  u32 table_index;
  int is_punt;
  int ret, i;
  u8 *match;

  ret = api_ip_session_redirect_add_parse (vam, &table_index, &opaque_index,
					   &proto, &is_punt, &match, &paths);
  if (ret)
    goto err;

  M2 (IP_SESSION_REDIRECT_ADD, mp, vec_len (paths) * sizeof (mp->paths[0]));

  mp->table_index = htonl (table_index);
  mp->opaque_index = htonl (opaque_index);
  mp->is_punt = is_punt;
  memcpy_s (mp->match, sizeof (mp->match), match, vec_len (match));
  mp->n_paths = vec_len (paths);
  vec_foreach_index (i, paths)
    fib_api_path_encode (&paths[i], &mp->paths[i]);

  S (mp);
  W (ret);

err:
  vec_free (match);
  vec_free (paths);
  return ret;
}

static int
api_ip_session_redirect_add_v2 (vat_main_t *vam)
{
  vl_api_ip_session_redirect_add_v2_t *mp;
  fib_route_path_t *paths;
  dpo_proto_t proto;
  u32 opaque_index;
  u32 table_index;
  int is_punt;
  int ret, i;
  u8 *match;

  ret = api_ip_session_redirect_add_parse (vam, &table_index, &opaque_index,
					   &proto, &is_punt, &match, &paths);
  if (ret)
    goto err;

  M2 (IP_SESSION_REDIRECT_ADD_V2, mp, vec_len (paths) * sizeof (mp->paths[0]));

  mp->table_index = htonl (table_index);
  mp->opaque_index = htonl (opaque_index);
  mp->proto = fib_api_path_dpo_proto_to_nh (proto);
  mp->is_punt = is_punt;
  memcpy_s (mp->match, sizeof (mp->match), match, vec_len (match));
  mp->n_paths = vec_len (paths);
  vec_foreach_index (i, paths)
    fib_api_path_encode (&paths[i], &mp->paths[i]);

  S (mp);
  W (ret);

err:
  vec_free (match);
  vec_free (paths);
  return ret;
}

static int
api_ip_session_redirect_del (vat_main_t *vam)
{
  vnet_classify_main_t *cm = &vnet_classify_main;
  vl_api_ip_session_redirect_del_t *mp;
  u32 table_index = ~0;
  u8 *match = 0;
  int ret;

  while (unformat_check_input (vam->input) != UNFORMAT_END_OF_INPUT)
    {
      if (unformat (vam->input, "table %u", &table_index))
	;
      else if (unformat (vam->input, "match %U", unformat_classify_match, cm,
			 &match, table_index))
	;
      else
	{
	  clib_warning ("unknown input '%U'", format_unformat_error,
			vam->input);
	  return -99;
	}
    }

  M2 (IP_SESSION_REDIRECT_DEL, mp, vec_len (match));

  mp->table_index = htonl (table_index);
  mp->match_len = htonl (vec_len (match));
  clib_memcpy (mp->match, match, vec_len (match));

  S (mp);
  W (ret);

  return ret;
}

#include "ip_session_redirect.api_test.c"

/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */