FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
mpls_label_dpo.c
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2016 Cisco and/or its affiliates.
3  * Licensed under the Apache License, Version 2.0 (the "License");
4  * you may not use this file except in compliance with the License.
5  * You may obtain a copy of the License at:
6  *
7  * http://www.apache.org/licenses/LICENSE-2.0
8  *
9  * Unless required by applicable law or agreed to in writing, software
10  * distributed under the License is distributed on an "AS IS" BASIS,
11  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12  * See the License for the specific language governing permissions and
13  * limitations under the License.
14  */
15 
16 #include <vnet/ip/ip.h>
18 #include <vnet/mpls/mpls.h>
19 
20 /*
21  * pool of all MPLS Label DPOs
22  */
24 
25 static mpls_label_dpo_t *
27 {
28  mpls_label_dpo_t *mld;
29 
30  pool_get_aligned(mpls_label_dpo_pool, mld, CLIB_CACHE_LINE_BYTES);
31  memset(mld, 0, sizeof(*mld));
32 
33  dpo_reset(&mld->mld_dpo);
34 
35  return (mld);
36 }
37 
38 static index_t
40 {
41  return (mld - mpls_label_dpo_pool);
42 }
43 
44 index_t
46  mpls_eos_bit_t eos,
47  u8 ttl,
48  u8 exp,
49  const dpo_id_t *dpo)
50 {
51  mpls_label_dpo_t *mld;
52 
53  mld = mpls_label_dpo_alloc();
54 
59 
60  /*
61  * get the header in network byte order since we will paint it
62  * on a packet in the data-plane
63  */
65  clib_host_to_net_u32(mld->mld_hdr.label_exp_s_ttl);
66 
68 
69  return (mpls_label_dpo_get_index(mld));
70 }
71 
72 u8*
73 format_mpls_label_dpo (u8 *s, va_list *args)
74 {
75  index_t index = va_arg (*args, index_t);
76  u32 indent = va_arg (*args, u32);
78  mpls_label_dpo_t *mld;
79 
80  mld = mpls_label_dpo_get(index);
81 
82  hdr.label_exp_s_ttl =
83  clib_net_to_host_u32(mld->mld_hdr.label_exp_s_ttl);
84 
85  s = format(s, "mpls-label:[%d]:", index);
86  s = format(s, "%U\n", format_mpls_header, hdr);
87  s = format(s, "%U", format_white_space, indent);
88  s = format(s, "%U", format_dpo_id, &mld->mld_dpo, indent+2);
89 
90  return (s);
91 }
92 
93 static void
95 {
96  mpls_label_dpo_t *mld;
97 
98  mld = mpls_label_dpo_get(dpo->dpoi_index);
99 
100  mld->mld_locks++;
101 }
102 
103 static void
105 {
106  mpls_label_dpo_t *mld;
107 
108  mld = mpls_label_dpo_get(dpo->dpoi_index);
109 
110  mld->mld_locks--;
111 
112  if (0 == mld->mld_locks)
113  {
114  dpo_reset(&mld->mld_dpo);
115  pool_put(mpls_label_dpo_pool, mld);
116  }
117 }
118 
119 /**
120  * @brief A struct to hold tracing information for the MPLS label imposition
121  * node.
122  */
124 {
125  /**
126  * The MPLS header imposed
127  */
130 
133  vlib_node_runtime_t * node,
134  vlib_frame_t * from_frame)
135 {
136  u32 n_left_from, next_index, * from, * to_next;
137 
138  from = vlib_frame_vector_args (from_frame);
139  n_left_from = from_frame->n_vectors;
140 
141  next_index = node->cached_next_index;
142 
143  while (n_left_from > 0)
144  {
145  u32 n_left_to_next;
146 
147  vlib_get_next_frame(vm, node, next_index, to_next, n_left_to_next);
148 
149  while (n_left_from > 0 && n_left_to_next > 0)
150  {
151  mpls_unicast_header_t *hdr0;
152  mpls_label_dpo_t *mld0;
153  vlib_buffer_t * b0;
154  u32 bi0, mldi0;
155  u32 next0;
156 
157  bi0 = from[0];
158  to_next[0] = bi0;
159  from += 1;
160  to_next += 1;
161  n_left_from -= 1;
162  n_left_to_next -= 1;
163 
164  b0 = vlib_get_buffer (vm, bi0);
165 
166  /* dst lookup was done by ip4 lookup */
167  mldi0 = vnet_buffer(b0)->ip.adj_index[VLIB_TX];
168  mld0 = mpls_label_dpo_get(mldi0);
169 
170  /* Paint the MPLS header */
171  vlib_buffer_advance(b0, -sizeof(*hdr0));
172  hdr0 = vlib_buffer_get_current(b0);
173 
174  // FIXME.
175  // need to copy the TTL from the correct place.
176  // for IPvX imposition from the IP header
177  // so we need a deidcated ipx-to-mpls-label-imp-node
178  // for mpls switch and stack another solution is required.
179  *hdr0 = mld0->mld_hdr;
180 
181  next0 = mld0->mld_dpo.dpoi_next_node;
182  vnet_buffer(b0)->ip.adj_index[VLIB_TX] = mld0->mld_dpo.dpoi_index;
183 
185  {
187  vlib_add_trace (vm, node, b0, sizeof (*tr));
188  tr->hdr = *hdr0;
189  }
190 
191  vlib_validate_buffer_enqueue_x1(vm, node, next_index, to_next,
192  n_left_to_next, bi0, next0);
193  }
194  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
195  }
196  return from_frame->n_vectors;
197 }
198 
199 static u8 *
201 {
202  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
203  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
206  uword indent;
207 
208  t = va_arg (*args, mpls_label_imposition_trace_t *);
209  indent = format_get_indent (s);
210  hdr.label_exp_s_ttl = clib_net_to_host_u32(t->hdr.label_exp_s_ttl);
211 
212  s = format (s, "%Umpls-header:%U",
213  format_white_space, indent,
214  format_mpls_header, hdr);
215  return (s);
216 }
217 
219  .function = mpls_label_imposition,
220  .name = "mpls-label-imposition",
221  .vector_size = sizeof (u32),
222 
223  .format_trace = format_mpls_label_imposition_trace,
224  .n_next_nodes = 1,
225  .next_nodes = {
226  [0] = "error-drop",
227  }
228 };
230 
231 static void
233 {
234  fib_show_memory_usage("MPLS label",
235  pool_elts(mpls_label_dpo_pool),
236  pool_len(mpls_label_dpo_pool),
237  sizeof(mpls_label_dpo_t));
238 }
239 
240 const static dpo_vft_t mld_vft = {
242  .dv_unlock = mpls_label_dpo_unlock,
243  .dv_format = format_mpls_label_dpo,
244  .dv_mem_show = mpls_label_dpo_mem_show,
245 };
246 
247 const static char* const mpls_label_imp_ip4_nodes[] =
248 {
249  "mpls-label-imposition",
250  NULL,
251 };
252 const static char* const mpls_label_imp_ip6_nodes[] =
253 {
254  "mpls-label-imposition",
255  NULL,
256 };
257 const static char* const mpls_label_imp_mpls_nodes[] =
258 {
259  "mpls-label-imposition",
260  NULL,
261 };
262 const static char* const * const mpls_label_imp_nodes[DPO_PROTO_NUM] =
263 {
267 };
268 
269 
270 void
272 {
274 }
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
dpo_lock_fn_t dv_lock
A reference counting lock function.
Definition: dpo.h:318
static const char *const mpls_label_imp_ip6_nodes[]
static uword mpls_label_imposition(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
index_t mpls_label_dpo_create(mpls_label_t label, mpls_eos_bit_t eos, u8 ttl, u8 exp, const dpo_id_t *dpo)
#define CLIB_UNUSED(x)
Definition: clib.h:79
A virtual function table regisitered for a DPO type.
Definition: dpo.h:313
static const char *const mpls_label_imp_mpls_nodes[]
format_function_t format_mpls_header
Definition: mpls.h:166
mpls_unicast_header_t hdr
The MPLS header imposed.
#define NULL
Definition: clib.h:55
u32 mpls_label_t
A label value only, i.e.
Definition: packet.h:24
u32 index_t
A Data-Path Object is an object that represents actions that are applied to packets are they are swit...
Definition: dpo.h:41
u8 * format_mpls_label_dpo(u8 *s, va_list *args)
#define pool_len(p)
Number of elements in pool vector.
Definition: pool.h:121
static index_t mpls_label_dpo_get_index(mpls_label_dpo_t *mld)
void dpo_register(dpo_type_t type, const dpo_vft_t *vft, const char *const *const *nodes)
For a given DPO type Register:
Definition: dpo.c:221
#define always_inline
Definition: clib.h:84
static void * vlib_buffer_get_current(vlib_buffer_t *b)
Get pointer to current data to process.
Definition: buffer.h:190
u8 * format_white_space(u8 *s, va_list *va)
Definition: std-formats.c:113
void fib_show_memory_usage(const char *name, u32 in_use_elts, u32 allocd_elts, size_t size_elt)
Show the memory usage for a type.
Definition: fib_node.c:210
static void vnet_mpls_uc_set_label(mpls_label_t *label_exp_s_ttl, u32 value)
Definition: packet.h:97
static void vnet_mpls_uc_set_exp(mpls_label_t *label_exp_s_ttl, u32 exp)
Definition: packet.h:104
The identity of a DPO is a combination of its type and its instance number/index of objects of that t...
Definition: dpo.h:138
static uword format_get_indent(u8 *s)
Definition: format.h:72
#define pool_put(P, E)
Free an object E in pool P.
Definition: pool.h:214
#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
#define pool_get_aligned(P, E, A)
Allocate an object E from a pool P (general version).
Definition: pool.h:169
u16 n_vectors
Definition: node.h:344
static mpls_label_dpo_t * mpls_label_dpo_get(index_t index)
static void vlib_buffer_advance(vlib_buffer_t *b, word l)
Advance current data pointer by the supplied (signed!) amount.
Definition: buffer.h:203
vlib_node_registration_t mpls_label_imposition_node
(constructor) VLIB_REGISTER_NODE (mpls_label_imposition_node)
void mpls_label_dpo_module_init(void)
u16 cached_next_index
Definition: node.h:463
static void mpls_label_dpo_lock(dpo_id_t *dpo)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
dpo_id_t mld_dpo
Next DPO in the graph.
struct mpls_label_imposition_trace_t_ mpls_label_imposition_trace_t
A struct to hold tracing information for the MPLS label imposition node.
mpls_label_t label_exp_s_ttl
Definition: packet.h:31
static void vnet_mpls_uc_set_s(mpls_label_t *label_exp_s_ttl, u32 eos)
Definition: packet.h:111
u8 * format_dpo_id(u8 *s, va_list *args)
Format a DPO_id_t oject
Definition: dpo.c:98
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
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
Definition: defs.h:47
#define DPO_PROTO_NUM
Definition: dpo.h:72
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:154
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
static void mpls_label_dpo_mem_show(void)
static void vnet_mpls_uc_set_ttl(mpls_label_t *label_exp_s_ttl, u32 ttl)
Definition: packet.h:118
#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 const char *const *const mpls_label_imp_nodes[DPO_PROTO_NUM]
static u8 * format_mpls_label_imposition_trace(u8 *s, va_list *args)
mpls_label_dpo_t * mpls_label_dpo_pool
void dpo_reset(dpo_id_t *dpo)
reset a DPO ID The DPO will be unlocked.
Definition: dpo.c:171
u16 mld_locks
Number of locks/users of the label.
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:150
#define CLIB_CACHE_LINE_BYTES
Definition: cache.h:67
u32 flags
buffer flags: VLIB_BUFFER_IS_TRACED: trace this buffer.
Definition: buffer.h:85
static mpls_label_dpo_t * mpls_label_dpo_alloc(void)
mpls_unicast_header_t mld_hdr
The MPLS label header to impose.
A struct to hold tracing information for the MPLS label imposition node.
static void mpls_label_dpo_unlock(dpo_id_t *dpo)
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
A representation of an MPLS label for imposition in the data-path.
static const char *const mpls_label_imp_ip4_nodes[]
void dpo_stack(dpo_type_t child_type, dpo_proto_t child_proto, dpo_id_t *dpo, const dpo_id_t *parent)
Stack one DPO object on another, and thus establish a child-parent relationship.
Definition: dpo.c:373
enum mpls_eos_bit_t_ mpls_eos_bit_t
static uword pool_elts(void *v)
Number of active elements in a pool.
Definition: pool.h:109