aboutsummaryrefslogtreecommitdiffstats
path: root/src/vnet/l2/l2_input_vtr.c
blob: 60a39631e8700e873a7a4417f71cc703231e9121 (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
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
/*
 * l2_input_vtr.c : layer 2 input vlan tag rewrite processing
 *
 * Copyright (c) 2013 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/vnet.h>
#include <vnet/ethernet/ethernet.h>
#include <vnet/ethernet/packet.h>
#include <vnet/l2/l2_input.h>
#include <vnet/l2/feat_bitmap.h>
#include <vnet/l2/l2_vtr.h>
#include <vnet/l2/l2_input_vtr.h>
#include <vnet/l2/l2_output.h>

#include <vppinfra/error.h>
#include <vppinfra/cache.h>


typedef struct
{
  /* per-pkt trace data */
  u8 src[6];
  u8 dst[6];
  u8 raw[12];			/* raw data (vlans) */
  u32 sw_if_index;
} l2_invtr_trace_t;

/* packet trace format function */
static u8 *
format_l2_invtr_trace (u8 * s, va_list * args)
{
  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
  l2_invtr_trace_t *t = va_arg (*args, l2_invtr_trace_t *);

  s = format (s, "l2-input-vtr: sw_if_index %d dst %U src %U data "
	      "%02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x %02x",
	      t->sw_if_index,
	      format_ethernet_address, t->dst,
	      format_ethernet_address, t->src,
	      t->raw[0], t->raw[1], t->raw[2], t->raw[3], t->raw[4],
	      t->raw[5], t->raw[6], t->raw[7], t->raw[8], t->raw[9],
	      t->raw[10], t->raw[11]);
  return s;
}

l2_invtr_main_t l2_invtr_main;

static vlib_node_registration_t l2_invtr_node;

#define foreach_l2_invtr_error			\
_(L2_INVTR,    "L2 inverter packets")		\
_(DROP,        "L2 input tag rewrite drops")

typedef enum
{
#define _(sym,str) L2_INVTR_ERROR_##sym,
  foreach_l2_invtr_error
#undef _
    L2_INVTR_N_ERROR,
} l2_invtr_error_t;

static char *l2_invtr_error_strings[] = {
#define _(sym,string) string,
  foreach_l2_invtr_error
#undef _
};

typedef enum
{
  L2_INVTR_NEXT_DROP,
  L2_INVTR_N_NEXT,
} l2_invtr_next_t;


static uword
l2_invtr_node_fn (vlib_main_t * vm,
		  vlib_node_runtime_t * node, vlib_frame_t * frame)
{
  u32 n_left_from, *from, *to_next;
  l2_invtr_next_t next_index;
  l2_invtr_main_t *msm = &l2_invtr_main;

  from = vlib_frame_vector_args (frame);
  n_left_from = frame->n_vectors;	/* number of packets to process */
  next_index = node->cached_next_index;

  while (n_left_from > 0)
    {
      u32 n_left_to_next;

      /* get space to enqueue frame to graph node "next_index" */
      vlib_get_next_frame (vm, node, next_index, to_next, n_left_to_next);

      while (n_left_from >= 6 && n_left_to_next >= 2)
	{
	  u32 bi0, bi1;
	  vlib_buffer_t *b0, *b1;
	  u32 next0, next1;
	  u32 sw_if_index0, sw_if_index1;
	  u32 feature_bitmap0, feature_bitmap1;

	  /* Prefetch next iteration. */
	  {
	    vlib_buffer_t *p2, *p3, *p4, *p5;
	    u32 sw_if_index2, sw_if_index3;

	    p2 = vlib_get_buffer (vm, from[2]);
	    p3 = vlib_get_buffer (vm, from[3]);
	    p4 = vlib_get_buffer (vm, from[4]);
	    p5 = vlib_get_buffer (vm, from[5]);

	    /* Prefetch the buffer header and packet for the N+2 loop iteration */
	    vlib_prefetch_buffer_header (p4, LOAD);
	    vlib_prefetch_buffer_header (p5, LOAD);

	    CLIB_PREFETCH (p4->data, CLIB_CACHE_LINE_BYTES, STORE);
	    CLIB_PREFETCH (p5->data, CLIB_CACHE_LINE_BYTES, STORE);

	    /*
	     * Prefetch the input config for the N+1 loop iteration
	     * This depends on the buffer header above
	     */
	    sw_if_index2 = vnet_buffer (p2)->sw_if_index[VLIB_RX];
	    sw_if_index3 = vnet_buffer (p3)->sw_if_index[VLIB_RX];
	    CLIB_PREFETCH (vec_elt_at_index
			   (l2output_main.configs, sw_if_index2),
			   CLIB_CACHE_LINE_BYTES, LOAD);
	    CLIB_PREFETCH (vec_elt_at_index
			   (l2output_main.configs, sw_if_index3),
			   CLIB_CACHE_LINE_BYTES, LOAD);
	  }

	  /* speculatively enqueue b0 and b1 to the current next frame */
	  /* bi is "buffer index", b is pointer to the buffer */
	  to_next[0] = bi0 = from[0];
	  to_next[1] = bi1 = from[1];
	  from += 2;
	  to_next += 2;
	  n_left_from -= 2;
	  n_left_to_next -= 2;

	  b0 = vlib_get_buffer (vm, bi0);
	  b1 = vlib_get_buffer (vm, bi1);

	  /* RX interface handles */
	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];
	  sw_if_index1 = vnet_buffer (b1)->sw_if_index[VLIB_RX];

	  /* process 2 packets */

	  /* Remove ourself from the feature bitmap */
	  feature_bitmap0 =
	    vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;
	  feature_bitmap1 =
	    vnet_buffer (b1)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;

	  /* save for next feature graph nodes */
	  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;
	  vnet_buffer (b1)->l2.feature_bitmap = feature_bitmap1;

	  /* Determine the next node */
	  next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
						   feature_bitmap0);
	  next1 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
						   feature_bitmap1);

	  l2_output_config_t *config0;
	  l2_output_config_t *config1;
	  config0 = vec_elt_at_index (l2output_main.configs, sw_if_index0);
	  config1 = vec_elt_at_index (l2output_main.configs, sw_if_index1);

	  if (PREDICT_FALSE (config0->out_vtr_flag))
	    {
	      if (config0->output_vtr.push_and_pop_bytes)
		{
		  /* perform the tag rewrite on two packets */
		  if (l2_vtr_process
		      (b0,
		       &(vec_elt_at_index
			 (l2output_main.configs, sw_if_index0)->input_vtr)))
		    {
		      /* Drop packet */
		      next0 = L2_INVTR_NEXT_DROP;
		      b0->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	      else if (config0->output_pbb_vtr.push_and_pop_bytes)
		{
		  if (l2_pbb_process (b0, &(config0->input_pbb_vtr)))
		    {
		      /* Drop packet */
		      next0 = L2_INVTR_NEXT_DROP;
		      b0->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	    }
	  if (PREDICT_FALSE (config1->out_vtr_flag))
	    {
	      if (config1->output_vtr.push_and_pop_bytes)
		{
		  if (l2_vtr_process
		      (b1,
		       &(vec_elt_at_index
			 (l2output_main.configs, sw_if_index1)->input_vtr)))
		    {
		      /* Drop packet */
		      next1 = L2_INVTR_NEXT_DROP;
		      b1->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	      else if (config1->output_pbb_vtr.push_and_pop_bytes)
		{
		  if (l2_pbb_process (b1, &(config1->input_pbb_vtr)))
		    {
		      /* Drop packet */
		      next1 = L2_INVTR_NEXT_DROP;
		      b1->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	    }

	  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)))
	    {
	      if (b0->flags & VLIB_BUFFER_IS_TRACED)
		{
		  l2_invtr_trace_t *t =
		    vlib_add_trace (vm, node, b0, sizeof (*t));
		  ethernet_header_t *h0 = vlib_buffer_get_current (b0);
		  t->sw_if_index = sw_if_index0;
		  clib_memcpy (t->src, h0->src_address, 6);
		  clib_memcpy (t->dst, h0->dst_address, 6);
		  clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
		}
	      if (b1->flags & VLIB_BUFFER_IS_TRACED)
		{
		  l2_invtr_trace_t *t =
		    vlib_add_trace (vm, node, b1, sizeof (*t));
		  ethernet_header_t *h1 = vlib_buffer_get_current (b1);
		  t->sw_if_index = sw_if_index0;
		  clib_memcpy (t->src, h1->src_address, 6);
		  clib_memcpy (t->dst, h1->dst_address, 6);
		  clib_memcpy (t->raw, &h1->type, sizeof (t->raw));
		}
	    }

	  /* verify speculative enqueues, maybe switch current next frame */
	  /* if next0==next1==next_index then nothing special needs to be done */
	  vlib_validate_buffer_enqueue_x2 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, bi1, next0, next1);
	}

      while (n_left_from > 0 && n_left_to_next > 0)
	{
	  u32 bi0;
	  vlib_buffer_t *b0;
	  u32 next0;
	  u32 sw_if_index0;
	  u32 feature_bitmap0;

	  /* speculatively enqueue b0 to the current next frame */
	  bi0 = from[0];
	  to_next[0] = bi0;
	  from += 1;
	  to_next += 1;
	  n_left_from -= 1;
	  n_left_to_next -= 1;

	  b0 = vlib_get_buffer (vm, bi0);

	  sw_if_index0 = vnet_buffer (b0)->sw_if_index[VLIB_RX];

	  /* process 1 packet */

	  /* Remove ourself from the feature bitmap */
	  feature_bitmap0 =
	    vnet_buffer (b0)->l2.feature_bitmap & ~L2INPUT_FEAT_VTR;

	  /* save for next feature graph nodes */
	  vnet_buffer (b0)->l2.feature_bitmap = feature_bitmap0;

	  /* Determine the next node */
	  next0 = feat_bitmap_get_next_node_index (msm->feat_next_node_index,
						   feature_bitmap0);

	  l2_output_config_t *config0;
	  config0 = vec_elt_at_index (l2output_main.configs, sw_if_index0);

	  if (PREDICT_FALSE (config0->out_vtr_flag))
	    {
	      if (config0->output_vtr.push_and_pop_bytes)
		{
		  /* perform the tag rewrite on one packet */
		  if (l2_vtr_process
		      (b0,
		       &(vec_elt_at_index
			 (l2output_main.configs, sw_if_index0)->input_vtr)))
		    {
		      /* Drop packet */
		      next0 = L2_INVTR_NEXT_DROP;
		      b0->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	      else if (config0->output_pbb_vtr.push_and_pop_bytes)
		{
		  if (l2_pbb_process (b0, &(config0->input_pbb_vtr)))
		    {
		      /* Drop packet */
		      next0 = L2_INVTR_NEXT_DROP;
		      b0->error = node->errors[L2_INVTR_ERROR_DROP];
		    }
		}
	    }

	  if (PREDICT_FALSE ((node->flags & VLIB_NODE_FLAG_TRACE)
			     && (b0->flags & VLIB_BUFFER_IS_TRACED)))
	    {
	      l2_invtr_trace_t *t =
		vlib_add_trace (vm, node, b0, sizeof (*t));
	      ethernet_header_t *h0 = vlib_buffer_get_current (b0);
	      t->sw_if_index = sw_if_index0;
	      clib_memcpy (t->src, h0->src_address, 6);
	      clib_memcpy (t->dst, h0->dst_address, 6);
	      clib_memcpy (t->raw, &h0->type, sizeof (t->raw));
	    }

	  /* verify speculative enqueue, maybe switch current next frame */
	  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
					   to_next, n_left_to_next,
					   bi0, next0);
	}

      vlib_put_next_frame (vm, node, next_index, n_left_to_next);
    }

  return frame->n_vectors;
}


/* *INDENT-OFF* */
VLIB_REGISTER_NODE (l2_invtr_node,static) = {
  .function = l2_invtr_node_fn,
  .name = "l2-input-vtr",
  .vector_size = sizeof (u32),
  .format_trace = format_l2_invtr_trace,
  .type = VLIB_NODE_TYPE_INTERNAL,

  .n_errors = ARRAY_LEN(l2_invtr_error_strings),
  .error_strings = l2_invtr_error_strings,

  .n_next_nodes = L2_INVTR_N_NEXT,

  /* edit / add dispositions here */
  .next_nodes = {
       [L2_INVTR_NEXT_DROP]  = "error-drop",
  },
};
/* *INDENT-ON* */

VLIB_NODE_FUNCTION_MULTIARCH (l2_invtr_node, l2_invtr_node_fn)
     clib_error_t *l2_invtr_init (vlib_main_t * vm)
{
  l2_invtr_main_t *mp = &l2_invtr_main;

  mp->vlib_main = vm;
  mp->vnet_main = vnet_get_main ();

  /* Initialize the feature next-node indexes */
  feat_bitmap_init_next_nodes (vm,
			       l2_invtr_node.index,
			       L2INPUT_N_FEAT,
			       l2input_get_feat_names (),
			       mp->feat_next_node_index);

  return 0;
}

VLIB_INIT_FUNCTION (l2_invtr_init);


/*
 * fd.io coding-style-patch-verification: ON
 *
 * Local Variables:
 * eval: (c-set-style "gnu")
 * End:
 */
p)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_ACCEPT_SESSION); mp->context = server->index; mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); if (session_has_transport (s)) { listener = listen_session_get (s->listener_index); mp->listener_handle = listen_session_get_handle (listener); if (application_is_proxy (server)) { listener = application_first_listener (server, session_get_fib_proto (s), session_get_transport_proto (s)); if (listener) mp->listener_handle = listen_session_get_handle (listener); } vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); mp->handle = session_handle (s); tp_vft = transport_protocol_get_vft (session_get_transport_proto (s)); tc = tp_vft->get_connection (s->connection_index, s->thread_index); mp->port = tc->rmt_port; mp->is_ip4 = tc->is_ip4; clib_memcpy (&mp->ip, &tc->rmt_ip, sizeof (tc->rmt_ip)); } else { local_session_t *ls = (local_session_t *) s; local_session_t *ll; if (application_local_session_listener_has_transport (ls)) { listener = listen_session_get (ls->listener_index); mp->listener_handle = listen_session_get_handle (listener); mp->is_ip4 = session_type_is_ip4 (listener->session_type); } else { ll = application_get_local_listen_session (server, ls->listener_index); if (ll->transport_listener_index != ~0) { listener = listen_session_get (ll->transport_listener_index); mp->listener_handle = listen_session_get_handle (listener); } else { mp->listener_handle = application_local_session_handle (ll); } mp->is_ip4 = session_type_is_ip4 (ll->listener_session_type); } mp->handle = application_local_session_handle (ls); mp->port = ls->port; mp->vpp_event_queue_address = ls->client_evt_q; mp->server_event_queue_address = ls->server_evt_q; } vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); return 0; } void send_local_session_disconnect_callback (u32 app_index, local_session_t * ls) { application_t *app = application_get (app_index); vl_api_disconnect_session_t *mp; vl_api_registration_t *reg; reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { clib_warning ("no registration: %u", app->api_client_index); return; } mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION); mp->handle = application_local_session_handle (ls); mp->context = app->api_client_index; vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); } static void send_session_disconnect_callback (stream_session_t * s) { application_t *app = application_get (s->app_index); vl_api_disconnect_session_t *mp; vl_api_registration_t *reg; reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { clib_warning ("no registration: %u", app->api_client_index); return; } mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_DISCONNECT_SESSION); mp->handle = session_handle (s); mp->context = app->api_client_index; vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); } static void send_session_reset_callback (stream_session_t * s) { application_t *app = application_get (s->app_index); vl_api_registration_t *reg; vl_api_reset_session_t *mp; reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { clib_warning ("no registration: %u", app->api_client_index); return; } mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); memset (mp, 0, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_RESET_SESSION); mp->handle = session_handle (s); vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); } int send_session_connected_callback (u32 app_index, u32 api_context, stream_session_t * s, u8 is_fail) { vl_api_connect_session_reply_t *mp; transport_connection_t *tc; vl_api_registration_t *reg; svm_queue_t *vpp_queue; application_t *app; app = application_get (app_index); reg = vl_mem_api_client_index_to_registration (app->api_client_index); if (!reg) { clib_warning ("no registration: %u", app->api_client_index); return -1; } mp = vl_mem_api_alloc_as_if_client_w_reg (reg, sizeof (*mp)); mp->_vl_msg_id = clib_host_to_net_u16 (VL_API_CONNECT_SESSION_REPLY); mp->context = api_context; if (is_fail) goto done; if (session_has_transport (s)) { tc = session_get_transport (s); if (!tc) { is_fail = 1; goto done; } vpp_queue = session_manager_get_vpp_event_queue (s->thread_index); mp->handle = session_handle (s); mp->vpp_event_queue_address = pointer_to_uword (vpp_queue); clib_memcpy (mp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); mp->is_ip4 = tc->is_ip4; mp->lcl_port = tc->lcl_port; mp->server_rx_fifo = pointer_to_uword (s->server_rx_fifo); mp->server_tx_fifo = pointer_to_uword (s->server_tx_fifo); } else { local_session_t *ls = (local_session_t *) s; mp->handle = application_local_session_handle (ls); mp->lcl_port = ls->port; mp->vpp_event_queue_address = ls->server_evt_q; mp->client_event_queue_address = ls->client_evt_q; mp->server_rx_fifo = pointer_to_uword (s->server_tx_fifo); mp->server_tx_fifo = pointer_to_uword (s->server_rx_fifo); } done: mp->retval = is_fail ? clib_host_to_net_u32 (VNET_API_ERROR_SESSION_CONNECT) : 0; vl_msg_api_send_shmem (reg->vl_input_queue, (u8 *) & mp); return 0; } static session_cb_vft_t session_cb_vft = { .session_accept_callback = send_session_accept_callback, .session_disconnect_callback = send_session_disconnect_callback, .session_connected_callback = send_session_connected_callback, .session_reset_callback = send_session_reset_callback, .add_segment_callback = send_add_segment_callback, .del_segment_callback = send_del_segment_callback, }; static void vl_api_session_enable_disable_t_handler (vl_api_session_enable_disable_t * mp) { vl_api_session_enable_disable_reply_t *rmp; vlib_main_t *vm = vlib_get_main (); int rv = 0; vnet_session_enable_disable (vm, mp->is_enable); REPLY_MACRO (VL_API_SESSION_ENABLE_DISABLE_REPLY); } static void vl_api_application_attach_t_handler (vl_api_application_attach_t * mp) { vl_api_application_attach_reply_t *rmp; ssvm_private_t *segp, *evt_q_segment; vnet_app_attach_args_t _a, *a = &_a; vl_api_registration_t *reg; clib_error_t *error = 0; int rv = 0; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } STATIC_ASSERT (sizeof (u64) * APP_OPTIONS_N_OPTIONS <= sizeof (mp->options), "Out of options, fix api message definition"); memset (a, 0, sizeof (*a)); a->api_client_index = mp->client_index; a->options = mp->options; a->session_cb_vft = &session_cb_vft; if (mp->namespace_id_len > 64) { rv = VNET_API_ERROR_INVALID_VALUE; goto done; } if (mp->namespace_id_len) { vec_validate (a->namespace_id, mp->namespace_id_len - 1); clib_memcpy (a->namespace_id, mp->namespace_id, mp->namespace_id_len); } if ((error = vnet_application_attach (a))) { rv = clib_error_get_code (error); clib_error_report (error); } vec_free (a->namespace_id); done: /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_APPLICATION_ATTACH_REPLY, ({ if (!rv) { segp = a->segment; rmp->segment_name_length = 0; rmp->segment_size = segp->ssvm_size; if (vec_len (segp->name)) { memcpy (rmp->segment_name, segp->name, vec_len (segp->name)); rmp->segment_name_length = vec_len (segp->name); } rmp->app_event_queue_address = a->app_event_queue_address; } })); /* *INDENT-ON* */ if (rv) return; /* Send fifo segment fd if needed */ if (ssvm_type (a->segment) == SSVM_SEGMENT_MEMFD) session_send_memfd_fd (reg, a->segment); /* Send event queues segment */ if ((evt_q_segment = session_manager_get_evt_q_segment ())) session_send_memfd_fd (reg, evt_q_segment); } static void vl_api_application_detach_t_handler (vl_api_application_detach_t * mp) { vl_api_application_detach_reply_t *rmp; int rv = VNET_API_ERROR_INVALID_VALUE_2; vnet_app_detach_args_t _a, *a = &_a; application_t *app; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { a->app_index = app->index; rv = vnet_application_detach (a); } done: REPLY_MACRO (VL_API_APPLICATION_DETACH_REPLY); } static void vl_api_bind_uri_t_handler (vl_api_bind_uri_t * mp) { transport_connection_t *tc = 0; vnet_bind_args_t _a, *a = &_a; vl_api_bind_uri_reply_t *rmp; stream_session_t *s; application_t *app = 0; svm_queue_t *vpp_evt_q; int rv; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { memset (a, 0, sizeof (*a)); a->uri = (char *) mp->uri; a->app_index = app->index; rv = vnet_bind_uri (a); } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } done: /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_BIND_URI_REPLY, ({ if (!rv) { rmp->handle = a->handle; if (app && application_has_global_scope (app)) { s = listen_session_get_from_handle (a->handle); tc = listen_session_get_transport (s); rmp->lcl_is_ip4 = tc->is_ip4; rmp->lcl_port = tc->lcl_port; clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof(tc->lcl_ip)); if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL) { rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo); rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo); vpp_evt_q = session_manager_get_vpp_event_queue (0); rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q); } } } })); /* *INDENT-ON* */ } static void vl_api_unbind_uri_t_handler (vl_api_unbind_uri_t * mp) { vl_api_unbind_uri_reply_t *rmp; application_t *app; vnet_unbind_args_t _a, *a = &_a; int rv; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { a->uri = (char *) mp->uri; a->app_index = app->index; rv = vnet_unbind_uri (a); } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } done: REPLY_MACRO (VL_API_UNBIND_URI_REPLY); } static void vl_api_connect_uri_t_handler (vl_api_connect_uri_t * mp) { vl_api_connect_session_reply_t *rmp; vnet_connect_args_t _a, *a = &_a; application_t *app; clib_error_t *error = 0; int rv = 0; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { a->uri = (char *) mp->uri; a->api_context = mp->context; a->app_index = app->index; if ((error = vnet_connect_uri (a))) { rv = clib_error_get_code (error); clib_error_report (error); } } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } /* * Don't reply to stream (tcp) connects. The reply will come once * the connection is established. In case of the redirects, the reply * will come from the server app. */ if (rv == 0) return; done: /* *INDENT-OFF* */ REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); /* *INDENT-ON* */ } static void vl_api_disconnect_session_t_handler (vl_api_disconnect_session_t * mp) { vl_api_disconnect_session_reply_t *rmp; vnet_disconnect_args_t _a, *a = &_a; application_t *app; int rv = 0; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { a->handle = mp->handle; a->app_index = app->index; rv = vnet_disconnect_session (a); } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } done: REPLY_MACRO2 (VL_API_DISCONNECT_SESSION_REPLY, rmp->handle = mp->handle); } static void vl_api_disconnect_session_reply_t_handler (vl_api_disconnect_session_reply_t * mp) { vnet_disconnect_args_t _a, *a = &_a; application_t *app; /* Client objected to disconnecting the session, log and continue */ if (mp->retval) { clib_warning ("client retval %d", mp->retval); return; } /* Disconnect has been confirmed. Confirm close to transport */ app = application_lookup (mp->context); if (app) { a->handle = mp->handle; a->app_index = app->index; vnet_disconnect_session (a); } } static void vl_api_reset_session_reply_t_handler (vl_api_reset_session_reply_t * mp) { application_t *app; stream_session_t *s; u32 index, thread_index; app = application_lookup (mp->client_index); if (!app) return; session_parse_handle (mp->handle, &index, &thread_index); s = session_get_if_valid (index, thread_index); if (s == 0 || app->index != s->app_index) { clib_warning ("Invalid session!"); return; } /* Client objected to resetting the session, log and continue */ if (mp->retval) { clib_warning ("client retval %d", mp->retval); return; } /* This comes as a response to a reset, transport only waiting for * confirmation to remove connection state, no need to disconnect */ stream_session_cleanup (s); } static void vl_api_accept_session_reply_t_handler (vl_api_accept_session_reply_t * mp) { vnet_disconnect_args_t _a = { 0 }, *a = &_a; local_session_t *ls; stream_session_t *s; /* Server isn't interested, kill the session */ if (mp->retval) { a->app_index = mp->context; a->handle = mp->handle; vnet_disconnect_session (a); return; } if (session_handle_is_local (mp->handle)) { ls = application_get_local_session_from_handle (mp->handle); if (!ls || ls->app_index != mp->context) { clib_warning ("server %u doesn't own local handle %llu", mp->context, mp->handle); return; } if (application_local_session_connect_notify (ls)) return; ls->session_state = SESSION_STATE_READY; } else { s = session_get_from_handle_if_valid (mp->handle); if (!s) { clib_warning ("session doesn't exist"); return; } if (s->app_index != mp->context) { clib_warning ("app doesn't own session"); return; } s->session_state = SESSION_STATE_READY; } } static void vl_api_map_another_segment_reply_t_handler (vl_api_map_another_segment_reply_t * mp) { clib_warning ("not implemented"); } static void vl_api_bind_sock_t_handler (vl_api_bind_sock_t * mp) { vl_api_bind_sock_reply_t *rmp; vnet_bind_args_t _a, *a = &_a; int rv = 0; clib_error_t *error; application_t *app = 0; stream_session_t *s; transport_connection_t *tc = 0; ip46_address_t *ip46; svm_queue_t *vpp_evt_q; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (!app) { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; goto done; } ip46 = (ip46_address_t *) mp->ip; memset (a, 0, sizeof (*a)); a->sep.is_ip4 = mp->is_ip4; a->sep.ip = *ip46; a->sep.port = mp->port; a->sep.fib_index = mp->vrf; a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; a->sep.transport_proto = mp->proto; a->app_index = app->index; if ((error = vnet_bind (a))) { rv = clib_error_get_code (error); clib_error_report (error); } done: /* *INDENT-OFF* */ REPLY_MACRO2 (VL_API_BIND_SOCK_REPLY,({ if (!rv) { rmp->handle = a->handle; rmp->lcl_port = mp->port; rmp->lcl_is_ip4 = mp->is_ip4; if (app && application_has_global_scope (app)) { s = listen_session_get_from_handle (a->handle); tc = listen_session_get_transport (s); clib_memcpy (rmp->lcl_ip, &tc->lcl_ip, sizeof (tc->lcl_ip)); if (session_transport_service_type (s) == TRANSPORT_SERVICE_CL) { rmp->rx_fifo = pointer_to_uword (s->server_rx_fifo); rmp->tx_fifo = pointer_to_uword (s->server_tx_fifo); vpp_evt_q = session_manager_get_vpp_event_queue (0); rmp->vpp_evt_q = pointer_to_uword (vpp_evt_q); } } } })); /* *INDENT-ON* */ } static void vl_api_unbind_sock_t_handler (vl_api_unbind_sock_t * mp) { vl_api_unbind_sock_reply_t *rmp; vnet_unbind_args_t _a, *a = &_a; application_t *app; clib_error_t *error; int rv = 0; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { a->app_index = app->index; a->handle = mp->handle; if ((error = vnet_unbind (a))) { rv = clib_error_get_code (error); clib_error_report (error); } } done: REPLY_MACRO (VL_API_UNBIND_SOCK_REPLY); } static void vl_api_connect_sock_t_handler (vl_api_connect_sock_t * mp) { vl_api_connect_session_reply_t *rmp; vnet_connect_args_t _a, *a = &_a; application_t *app; clib_error_t *error = 0; int rv = 0; if (session_manager_is_enabled () == 0) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } app = application_lookup (mp->client_index); if (app) { svm_queue_t *client_q; ip46_address_t *ip46 = (ip46_address_t *) mp->ip; memset (a, 0, sizeof (*a)); client_q = vl_api_client_index_to_input_queue (mp->client_index); mp->client_queue_address = pointer_to_uword (client_q); a->sep.is_ip4 = mp->is_ip4; a->sep.ip = *ip46; a->sep.port = mp->port; a->sep.transport_proto = mp->proto; a->sep.fib_index = mp->vrf; a->sep.sw_if_index = ENDPOINT_INVALID_INDEX; if (mp->hostname_len) { vec_validate (a->sep.hostname, mp->hostname_len - 1); clib_memcpy (a->sep.hostname, mp->hostname, mp->hostname_len); } a->api_context = mp->context; a->app_index = app->index; if ((error = vnet_connect (a))) { rv = clib_error_get_code (error); clib_error_report (error); } vec_free (a->sep.hostname); } else { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; } if (rv == 0) return; /* Got some error, relay it */ done: REPLY_MACRO (VL_API_CONNECT_SESSION_REPLY); } static void vl_api_app_namespace_add_del_t_handler (vl_api_app_namespace_add_del_t * mp) { vl_api_app_namespace_add_del_reply_t *rmp; clib_error_t *error = 0; u32 appns_index = 0; u8 *ns_id = 0; int rv = 0; if (!session_manager_is_enabled ()) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } if (mp->namespace_id_len > ARRAY_LEN (mp->namespace_id)) { rv = VNET_API_ERROR_INVALID_VALUE; goto done; } vec_validate (ns_id, mp->namespace_id_len - 1); clib_memcpy (ns_id, mp->namespace_id, mp->namespace_id_len); vnet_app_namespace_add_del_args_t args = { .ns_id = ns_id, .secret = clib_net_to_host_u64 (mp->secret), .sw_if_index = clib_net_to_host_u32 (mp->sw_if_index), .ip4_fib_id = clib_net_to_host_u32 (mp->ip4_fib_id), .ip6_fib_id = clib_net_to_host_u32 (mp->ip6_fib_id), .is_add = 1 }; error = vnet_app_namespace_add_del (&args); if (error) { rv = clib_error_get_code (error); clib_error_report (error); } else { appns_index = app_namespace_index_from_id (ns_id); if (appns_index == APP_NAMESPACE_INVALID_INDEX) { clib_warning ("app ns lookup failed"); rv = VNET_API_ERROR_UNSPECIFIED; } } vec_free (ns_id); /* *INDENT-OFF* */ done: REPLY_MACRO2 (VL_API_APP_NAMESPACE_ADD_DEL_REPLY, ({ if (!rv) rmp->appns_index = clib_host_to_net_u32 (appns_index); })); /* *INDENT-ON* */ } static void vl_api_session_rule_add_del_t_handler (vl_api_session_rule_add_del_t * mp) { vl_api_session_rule_add_del_reply_t *rmp; session_rule_add_del_args_t args; session_rule_table_add_del_args_t *table_args = &args.table_args; clib_error_t *error; u8 fib_proto; int rv = 0; memset (&args, 0, sizeof (args)); fib_proto = mp->is_ip4 ? FIB_PROTOCOL_IP4 : FIB_PROTOCOL_IP6; table_args->lcl.fp_len = mp->lcl_plen; table_args->lcl.fp_proto = fib_proto; table_args->rmt.fp_len = mp->rmt_plen; table_args->rmt.fp_proto = fib_proto; table_args->lcl_port = mp->lcl_port; table_args->rmt_port = mp->rmt_port; table_args->action_index = clib_net_to_host_u32 (mp->action_index); table_args->is_add = mp->is_add; mp->tag[sizeof (mp->tag) - 1] = 0; table_args->tag = format (0, "%s", mp->tag); args.appns_index = clib_net_to_host_u32 (mp->appns_index); args.scope = mp->scope; args.transport_proto = mp->transport_proto; memset (&table_args->lcl.fp_addr, 0, sizeof (table_args->lcl.fp_addr)); memset (&table_args->rmt.fp_addr, 0, sizeof (table_args->rmt.fp_addr)); ip_set (&table_args->lcl.fp_addr, mp->lcl_ip, mp->is_ip4); ip_set (&table_args->rmt.fp_addr, mp->rmt_ip, mp->is_ip4); error = vnet_session_rule_add_del (&args); if (error) { rv = clib_error_get_code (error); clib_error_report (error); } vec_free (table_args->tag); REPLY_MACRO (VL_API_SESSION_RULE_ADD_DEL_REPLY); } static void send_session_rule_details4 (mma_rule_16_t * rule, u8 is_local, u8 transport_proto, u32 appns_index, u8 * tag, vl_api_registration_t * reg, u32 context) { vl_api_session_rules_details_t *rmp = 0; session_mask_or_match_4_t *match = (session_mask_or_match_4_t *) & rule->match; session_mask_or_match_4_t *mask = (session_mask_or_match_4_t *) & rule->mask; rmp = vl_msg_api_alloc (sizeof (*rmp)); memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); rmp->context = context; rmp->is_ip4 = 1; clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip)); clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip)); rmp->lcl_plen = ip4_mask_to_preflen (&mask->lcl_ip); rmp->rmt_plen = ip4_mask_to_preflen (&mask->rmt_ip); rmp->lcl_port = match->lcl_port; rmp->rmt_port = match->rmt_port; rmp->action_index = clib_host_to_net_u32 (rule->action_index); rmp->scope = is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL; rmp->transport_proto = transport_proto; rmp->appns_index = clib_host_to_net_u32 (appns_index); if (tag) { clib_memcpy (rmp->tag, tag, vec_len (tag)); rmp->tag[vec_len (tag)] = 0; } vl_api_send_msg (reg, (u8 *) rmp); } static void send_session_rule_details6 (mma_rule_40_t * rule, u8 is_local, u8 transport_proto, u32 appns_index, u8 * tag, vl_api_registration_t * reg, u32 context) { vl_api_session_rules_details_t *rmp = 0; session_mask_or_match_6_t *match = (session_mask_or_match_6_t *) & rule->match; session_mask_or_match_6_t *mask = (session_mask_or_match_6_t *) & rule->mask; rmp = vl_msg_api_alloc (sizeof (*rmp)); memset (rmp, 0, sizeof (*rmp)); rmp->_vl_msg_id = ntohs (VL_API_SESSION_RULES_DETAILS); rmp->context = context; rmp->is_ip4 = 0; clib_memcpy (rmp->lcl_ip, &match->lcl_ip, sizeof (match->lcl_ip)); clib_memcpy (rmp->rmt_ip, &match->rmt_ip, sizeof (match->rmt_ip)); rmp->lcl_plen = ip6_mask_to_preflen (&mask->lcl_ip); rmp->rmt_plen = ip6_mask_to_preflen (&mask->rmt_ip); rmp->lcl_port = match->lcl_port; rmp->rmt_port = match->rmt_port; rmp->action_index = clib_host_to_net_u32 (rule->action_index); rmp->scope = is_local ? SESSION_RULE_SCOPE_LOCAL : SESSION_RULE_SCOPE_GLOBAL; rmp->transport_proto = transport_proto; rmp->appns_index = clib_host_to_net_u32 (appns_index); if (tag) { clib_memcpy (rmp->tag, tag, vec_len (tag)); rmp->tag[vec_len (tag)] = 0; } vl_api_send_msg (reg, (u8 *) rmp); } static void send_session_rules_table_details (session_rules_table_t * srt, u8 fib_proto, u8 tp, u8 is_local, u32 appns_index, vl_api_registration_t * reg, u32 context) { mma_rule_16_t *rule16; mma_rule_40_t *rule40; mma_rules_table_16_t *srt16; mma_rules_table_40_t *srt40; u32 ri; if (is_local || fib_proto == FIB_PROTOCOL_IP4) { u8 *tag = 0; /* *INDENT-OFF* */ srt16 = &srt->session_rules_tables_16; pool_foreach (rule16, srt16->rules, ({ ri = mma_rules_table_rule_index_16 (srt16, rule16); tag = session_rules_table_rule_tag (srt, ri, 1); send_session_rule_details4 (rule16, is_local, tp, appns_index, tag, reg, context); })); /* *INDENT-ON* */ } if (is_local || fib_proto == FIB_PROTOCOL_IP6) { u8 *tag = 0; /* *INDENT-OFF* */ srt40 = &srt->session_rules_tables_40; pool_foreach (rule40, srt40->rules, ({ ri = mma_rules_table_rule_index_40 (srt40, rule40); tag = session_rules_table_rule_tag (srt, ri, 1); send_session_rule_details6 (rule40, is_local, tp, appns_index, tag, reg, context); })); /* *INDENT-ON* */ } } static void vl_api_session_rules_dump_t_handler (vl_api_one_map_server_dump_t * mp) { vl_api_registration_t *reg; session_table_t *st; u8 tp; reg = vl_api_client_index_to_registration (mp->client_index); if (!reg) return; /* *INDENT-OFF* */ session_table_foreach (st, ({ for (tp = 0; tp < TRANSPORT_N_PROTO; tp++) { send_session_rules_table_details (&st->session_rules[tp], st->active_fib_proto, tp, st->is_local, st->appns_index, reg, mp->context); } })); /* *INDENT-ON* */ } static void vl_api_application_tls_cert_add_t_handler (vl_api_application_tls_cert_add_t * mp) { vl_api_app_namespace_add_del_reply_t *rmp; vnet_app_add_tls_cert_args_t _a, *a = &_a; clib_error_t *error; application_t *app; u32 cert_len; int rv = 0; if (!session_manager_is_enabled ()) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } if (!(app = application_lookup (mp->client_index))) { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; goto done; } memset (a, 0, sizeof (*a)); a->app_index = app->index; cert_len = clib_net_to_host_u16 (mp->cert_len); if (cert_len > 10000) { rv = VNET_API_ERROR_INVALID_VALUE; goto done; } vec_validate (a->cert, cert_len); clib_memcpy (a->cert, mp->cert, cert_len); if ((error = vnet_app_add_tls_cert (a))) { rv = clib_error_get_code (error); clib_error_report (error); } vec_free (a->cert); done: REPLY_MACRO (VL_API_APPLICATION_TLS_CERT_ADD_REPLY); } static void vl_api_application_tls_key_add_t_handler (vl_api_application_tls_key_add_t * mp) { vl_api_app_namespace_add_del_reply_t *rmp; vnet_app_add_tls_key_args_t _a, *a = &_a; clib_error_t *error; application_t *app; u32 key_len; int rv = 0; if (!session_manager_is_enabled ()) { rv = VNET_API_ERROR_FEATURE_DISABLED; goto done; } if (!(app = application_lookup (mp->client_index))) { rv = VNET_API_ERROR_APPLICATION_NOT_ATTACHED; goto done; } memset (a, 0, sizeof (*a)); a->app_index = app->index; key_len = clib_net_to_host_u16 (mp->key_len); if (key_len > 10000) { rv = VNET_API_ERROR_INVALID_VALUE; goto done; } vec_validate (a->key, key_len); clib_memcpy (a->key, mp->key, key_len); if ((error = vnet_app_add_tls_key (a))) { rv = clib_error_get_code (error); clib_error_report (error); } vec_free (a->key); done: REPLY_MACRO (VL_API_APPLICATION_TLS_KEY_ADD_REPLY); } static clib_error_t * application_reaper_cb (u32 client_index) { application_t *app = application_lookup (client_index); vnet_app_detach_args_t _a, *a = &_a; if (app) { a->app_index = app->index; vnet_application_detach (a); } return 0; } VL_MSG_API_REAPER_FUNCTION (application_reaper_cb); #define vl_msg_name_crc_list #include <vnet/vnet_all_api_h.h> #undef vl_msg_name_crc_list static void setup_message_id_table (api_main_t * am) { #define _(id,n,crc) vl_msg_api_add_msg_name_crc (am, #n "_" #crc, id); foreach_vl_msg_name_crc_session; #undef _ } /* * session_api_hookup * Add uri's API message handlers to the table. * vlib has alread mapped shared memory and * added the client registration handlers. * See .../open-repo/vlib/memclnt_vlib.c:memclnt_process() */ static clib_error_t * session_api_hookup (vlib_main_t * vm) { api_main_t *am = &api_main; #define _(N,n) \ vl_msg_api_set_handlers(VL_API_##N, #n, \ vl_api_##n##_t_handler, \ vl_noop_handler, \ vl_api_##n##_t_endian, \ vl_api_##n##_t_print, \ sizeof(vl_api_##n##_t), 1); foreach_session_api_msg; #undef _ /* * Messages which bounce off the data-plane to * an API client. Simply tells the message handling infra not * to free the message. * * Bounced message handlers MUST NOT block the data plane */ am->message_bounce[VL_API_CONNECT_URI] = 1; am->message_bounce[VL_API_CONNECT_SOCK] = 1; /* * Set up the (msg_name, crc, message-id) table */ setup_message_id_table (am); return 0; } VLIB_API_INIT_FUNCTION (session_api_hookup); /* * fd.io coding-style-patch-verification: ON * * Local Variables: * eval: (c-set-style "gnu") * End: */