FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
policy_encap.c
Go to the documentation of this file.
1 /*
2  * policy_encap.c: mpls-o-e policy encap
3  *
4  * Copyright (c) 2012-2014 Cisco and/or its affiliates.
5  * Licensed under the Apache License, Version 2.0 (the "License");
6  * you may not use this file except in compliance with the License.
7  * You may obtain a copy of the License at:
8  *
9  * http://www.apache.org/licenses/LICENSE-2.0
10  *
11  * Unless required by applicable law or agreed to in writing, software
12  * distributed under the License is distributed on an "AS IS" BASIS,
13  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
14  * See the License for the specific language governing permissions and
15  * limitations under the License.
16  */
17 
18 #include <vlib/vlib.h>
19 #include <vnet/pg/pg.h>
20 #include <vnet/mpls/mpls.h>
22 
23 typedef struct {
27 
28 u8 * format_mpls_policy_encap_trace (u8 * s, va_list * args)
29 {
30  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
31  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
33 
34  s = format (s, "MPLS-POLICY-ENCAP: next-index %d encap-index %d",
35  t->next_index, t->encap_index);
36 
37  return s;
38 }
39 
41 
42 #define foreach_mpls_policy_encap_next \
43 _(DROP, "error-drop")
44 
45 typedef enum {
46 #define _(s,n) MPLS_POLICY_ENCAP_NEXT_##s,
48 #undef _
51 
52 #define foreach_mpls_policy_error \
53 _(PKTS_ENCAP, "mpls policy tunnel packets encapsulated")
54 
55 typedef enum {
56 #define _(n,s) MPLS_POLICY_ENCAP_ERROR_##n,
59 #undef _
61 
63  {
64 #define _(n,s) s,
66 #undef _
67 };
68 
69 static uword
71  vlib_node_runtime_t * node,
72  vlib_frame_t * from_frame)
73 {
74  u32 n_left_from, next_index, * from, * to_next;
75  mpls_main_t * mm = &mpls_main;
76 
77  from = vlib_frame_vector_args (from_frame);
78  n_left_from = from_frame->n_vectors;
79 
80  next_index = node->cached_next_index;
81 
82  while (n_left_from > 0)
83  {
84  u32 n_left_to_next;
85 
86  vlib_get_next_frame (vm, node, next_index,
87  to_next, n_left_to_next);
88 
89  while (n_left_from > 0 && n_left_to_next > 0)
90  {
91  u32 bi0;
92  vlib_buffer_t * b0;
93  u8 * h0;
94  u32 encap_index0;
95  u32 next0;
96  mpls_encap_t * e0;
97 
98  bi0 = from[0];
99  to_next[0] = bi0;
100  from += 1;
101  to_next += 1;
102  n_left_from -= 1;
103  n_left_to_next -= 1;
104 
105  b0 = vlib_get_buffer (vm, bi0);
106 
107  encap_index0 = vnet_buffer(b0)->l2_classify.opaque_index;
108 
109  e0 = pool_elt_at_index (mm->encaps, encap_index0);
110 
112  h0 = vlib_buffer_get_current (b0);
113  clib_memcpy (h0, e0->rewrite, vec_len(e0->rewrite));
114 
115  next0 = e0->output_next_index;
116 
118  {
120  vlib_add_trace (vm, node, b0, sizeof (*tr));
121  tr->next_index = next0;
122  tr->encap_index = encap_index0;
123  }
124  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
125  to_next, n_left_to_next,
126  bi0, next0);
127  }
128  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
129  }
131  MPLS_POLICY_ENCAP_ERROR_PKTS_ENCAP,
132  from_frame->n_vectors);
133  return from_frame->n_vectors;
134 }
135 
137  .function = mpls_policy_encap,
138  .name = "mpls-policy-encap",
139  /* Takes a vector of packets. */
140  .vector_size = sizeof (u32),
141 
142  .runtime_data_bytes = 0,
143 
144  .n_errors = MPLS_POLICY_ENCAP_N_ERROR,
145  .error_strings = mpls_policy_encap_error_strings,
146 
147  .format_trace = format_mpls_policy_encap_trace,
148 
149  .n_next_nodes = MPLS_POLICY_ENCAP_N_NEXT,
150  .next_nodes = {
151 #define _(s,n) [MPLS_POLICY_ENCAP_NEXT_##s] = n,
153 #undef _
154  },
155 };
156 
158 
159 static clib_error_t *
161 {
162  mpls_main_t * mm = &mpls_main;
163  clib_error_t * error;
164 
165  if ((error = vlib_call_init_function (vm, mpls_init)))
166  return error;
167 
170  ip4_classify_node.index,
171  mpls_policy_encap_node.index);
172 
175  ip6_classify_node.index,
176  mpls_policy_encap_node.index);
177 
178  return 0;
179 }
180 
void vlib_put_next_frame(vlib_main_t *vm, vlib_node_runtime_t *r, u32 next_index, u32 n_vectors_left)
Release pointer to next frame vector data.
Definition: main.c:457
#define CLIB_UNUSED(x)
Definition: clib.h:79
struct _vlib_node_registration vlib_node_registration_t
static clib_error_t * mpls_policy_encap_init(vlib_main_t *vm)
Definition: policy_encap.c:160
u8 * format_mpls_policy_encap_trace(u8 *s, va_list *args)
Definition: policy_encap.c:28
static uword vlib_node_add_next(vlib_main_t *vm, uword node, uword next_node)
Definition: node_funcs.h:1063
vlib_node_registration_t ip4_classify_node
(constructor) VLIB_REGISTER_NODE (ip4_classify_node)
Definition: ip_classify.c:38
#define VLIB_INIT_FUNCTION(x)
Definition: init.h:111
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
#define vlib_call_init_function(vm, x)
Definition: init.h:161
#define foreach_mpls_policy_encap_next
Definition: policy_encap.c:42
#define pool_elt_at_index(p, i)
Returns pointer to element at given index.
Definition: pool.h:369
mpls_policy_encap_error_t
Definition: policy_encap.c:55
#define PREDICT_FALSE(x)
Definition: clib.h:97
#define vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next, n_left_to_next, bi0, next0)
Finish enqueueing one buffer forward in the graph.
Definition: buffer_node.h:216
#define vlib_get_next_frame(vm, node, next_index, vectors, n_vectors_left)
Get pointer to next frame vector data by (vlib_node_runtime_t, next_index).
Definition: node_funcs.h:350
u32 output_next_index
Definition: mpls.h:54
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
u8 * rewrite
Definition: mpls.h:53
u16 n_vectors
Definition: node.h:344
mpls_main_t mpls_main
Definition: mpls.c:25
#define clib_memcpy(a, b, c)
Definition: string.h:64
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:203
u32 ip4_classify_mpls_policy_encap_next_index
Definition: mpls.h:114
mpls_policy_encap_next_t
Definition: policy_encap.c:45
static uword mpls_policy_encap(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: policy_encap.c:70
u16 cached_next_index
Definition: node.h:463
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
u32 ip6_classify_mpls_policy_encap_next_index
Definition: mpls.h:115
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
#define foreach_mpls_policy_error
Definition: policy_encap.c:52
u64 uword
Definition: types.h:112
static void * vlib_add_trace(vlib_main_t *vm, vlib_node_runtime_t *r, vlib_buffer_t *b, u32 n_data_bytes)
Definition: trace_funcs.h:55
static clib_error_t * mpls_init(vlib_main_t *vm)
Definition: mpls.c:693
i64 word
Definition: types.h:111
#define vec_len(v)
Number of elements in vector (rvalue-only, NULL tolerant)
unsigned char u8
Definition: types.h:56
static void * vlib_frame_vector_args(vlib_frame_t *f)
Get pointer to frame vector data.
Definition: node_funcs.h:253
vlib_main_t * vlib_main
Definition: mpls.h:127
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
static char * mpls_policy_encap_error_strings[]
Definition: policy_encap.c:62
vlib_node_registration_t ip6_classify_node
(constructor) VLIB_REGISTER_NODE (ip6_classify_node)
Definition: ip_classify.c:39
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static vlib_buffer_t * vlib_get_buffer(vlib_main_t *vm, u32 buffer_index)
Translate buffer index into buffer pointer.
Definition: buffer_funcs.h:69
vlib_node_registration_t mpls_policy_encap_node
(constructor) VLIB_REGISTER_NODE (mpls_policy_encap_node)
Definition: policy_encap.c:40
mpls_encap_t * encaps
Definition: mpls.h:110