FD.io VPP  v16.12-rc0-308-g931be3a
Vector Packet Processing
mpls_lookup.c
Go to the documentation of this file.
1 /*
2  * mpls_lookup.c: MPLS lookup
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>
21 #include <vnet/fib/mpls_fib.h>
22 #include <vnet/dpo/load_balance.h>
23 
25 
26 typedef struct {
32 
33 static u8 *
34 format_mpls_lookup_trace (u8 * s, va_list * args)
35 {
36  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
37  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
38  mpls_lookup_trace_t * t = va_arg (*args, mpls_lookup_trace_t *);
39 
40  s = format (s, "MPLS: next [%d], lookup fib index %d, LB index %d "
41  "label %d eos %d",
42  t->next_index, t->lfib_index, t->lb_index,
44  clib_net_to_host_u32(t->label_net_byte_order)),
46  return s;
47 }
48 
49 /*
50  * Compute flow hash.
51  * We'll use it to select which adjacency to use for this flow. And other things.
52  */
55  flow_hash_config_t flow_hash_config)
56 {
57  // FIXME
59 }
60 
61 static inline uword
63  vlib_node_runtime_t * node,
64  vlib_frame_t * from_frame)
65 {
67  u32 n_left_from, next_index, * from, * to_next;
68  mpls_main_t * mm = &mpls_main;
69  u32 cpu_index = os_get_cpu_number();
70 
71  from = vlib_frame_vector_args (from_frame);
72  n_left_from = from_frame->n_vectors;
73  next_index = node->cached_next_index;
74 
75  while (n_left_from > 0)
76  {
77  u32 n_left_to_next;
78 
79  vlib_get_next_frame (vm, node, next_index,
80  to_next, n_left_to_next);
81 
82  while (n_left_from > 0 && n_left_to_next > 0)
83  {
84  u32 lbi0, next0, lfib_index0, bi0, hash_c0;
85  const mpls_unicast_header_t * h0;
86  const load_balance_t *lb0;
87  const dpo_id_t *dpo0;
88  vlib_buffer_t * b0;
89 
90  bi0 = from[0];
91  to_next[0] = bi0;
92  from += 1;
93  to_next += 1;
94  n_left_from -= 1;
95  n_left_to_next -= 1;
96 
97  b0 = vlib_get_buffer (vm, bi0);
98  h0 = vlib_buffer_get_current (b0);
99 
100  lfib_index0 = vec_elt(mm->fib_index_by_sw_if_index,
101  vnet_buffer(b0)->sw_if_index[VLIB_RX]);
102 
103  lbi0 = mpls_fib_table_forwarding_lookup (lfib_index0, h0);
104  lb0 = load_balance_get(lbi0);
105 
106  hash_c0 = vnet_buffer(b0)->ip.flow_hash = 0;
107  if (PREDICT_FALSE(lb0->lb_n_buckets > 1))
108  {
109  hash_c0 = vnet_buffer (b0)->ip.flow_hash =
111  }
112 
113  ASSERT (lb0->lb_n_buckets > 0);
114  ASSERT (is_pow2 (lb0->lb_n_buckets));
115 
116  dpo0 = load_balance_get_bucket_i(lb0,
117  (hash_c0 &
118  (lb0->lb_n_buckets_minus_1)));
119 
120  next0 = dpo0->dpoi_next_node;
121  vnet_buffer (b0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
122 
124  (cm, cpu_index, lbi0, 1,
125  vlib_buffer_length_in_chain (vm, b0));
126 
127  /*
128  * pop the label that was just used in the lookup
129  */
130  vlib_buffer_advance(b0, sizeof(*h0));
131 
133  {
134  mpls_lookup_trace_t *tr = vlib_add_trace (vm, node,
135  b0, sizeof (*tr));
136  tr->next_index = next0;
137  tr->lb_index = lbi0;
138  tr->lfib_index = lfib_index0;
140  }
141 
142  vlib_validate_buffer_enqueue_x1 (vm, node, next_index,
143  to_next, n_left_to_next,
144  bi0, next0);
145  }
146 
147  vlib_put_next_frame (vm, node, next_index, n_left_to_next);
148  }
150  MPLS_ERROR_PKTS_DECAP, from_frame->n_vectors);
151  return from_frame->n_vectors;
152 }
153 
154 static char * mpls_error_strings[] = {
155 #define mpls_error(n,s) s,
156 #include "error.def"
157 #undef mpls_error
158 };
159 
161  .function = mpls_lookup,
162  .name = "mpls-lookup",
163  /* Takes a vector of packets. */
164  .vector_size = sizeof (u32),
165  .n_errors = MPLS_N_ERROR,
166  .error_strings = mpls_error_strings,
167 
168  .sibling_of = "ip4-lookup",
169 
170  .format_buffer = format_mpls_header,
171  .format_trace = format_mpls_lookup_trace,
172  .unformat_buffer = unformat_mpls_header,
173 };
174 
176 
177 typedef struct {
181 
182 static u8 *
183 format_mpls_load_balance_trace (u8 * s, va_list * args)
184 {
185  CLIB_UNUSED (vlib_main_t * vm) = va_arg (*args, vlib_main_t *);
186  CLIB_UNUSED (vlib_node_t * node) = va_arg (*args, vlib_node_t *);
188 
189  s = format (s, "MPLS: next [%d], LB index %d ",
190  t->next_index, t->lb_index);
191  return s;
192 }
193 
196  vlib_node_runtime_t * node,
197  vlib_frame_t * frame)
198 {
200  u32 n_left_from, n_left_to_next, * from, * to_next;
201  ip_lookup_next_t next;
202  u32 cpu_index = os_get_cpu_number();
203 
204  from = vlib_frame_vector_args (frame);
205  n_left_from = frame->n_vectors;
206  next = node->cached_next_index;
207 
208  while (n_left_from > 0)
209  {
210  vlib_get_next_frame (vm, node, next,
211  to_next, n_left_to_next);
212 
213 
214  while (n_left_from > 0 && n_left_to_next > 0)
215  {
216  const mpls_unicast_header_t *hdr0;
217  const load_balance_t *lb0;
218  u32 pi0, lbi0, hc0, next0;
219  const dpo_id_t *dpo0;
220  vlib_buffer_t * p0;
221 
222  pi0 = from[0];
223  to_next[0] = pi0;
224 
225  p0 = vlib_get_buffer (vm, pi0);
226 
227  hdr0 = vlib_buffer_get_current (p0);
228  lbi0 = vnet_buffer (p0)->ip.adj_index[VLIB_TX];
229 
230  lb0 = load_balance_get(lbi0);
231  hc0 = lb0->lb_hash_config;
232  vnet_buffer(p0)->ip.flow_hash = mpls_compute_flow_hash(hdr0, hc0);
233 
234  dpo0 = load_balance_get_bucket_i(lb0,
235  vnet_buffer(p0)->ip.flow_hash &
236  (lb0->lb_n_buckets_minus_1));
237 
238  next0 = dpo0->dpoi_next_node;
239  vnet_buffer (p0)->ip.adj_index[VLIB_TX] = dpo0->dpoi_index;
240 
242  (cm, cpu_index, lbi0, 1,
243  vlib_buffer_length_in_chain (vm, p0));
244 
245  from += 1;
246  to_next += 1;
247  n_left_to_next -= 1;
248  n_left_from -= 1;
249 
250  if (PREDICT_FALSE (next0 != next))
251  {
252  n_left_to_next += 1;
253  vlib_put_next_frame (vm, node, next, n_left_to_next);
254  next = next0;
255  vlib_get_next_frame (vm, node, next,
256  to_next, n_left_to_next);
257  to_next[0] = pi0;
258  to_next += 1;
259  n_left_to_next -= 1;
260  }
261  }
262 
263  vlib_put_next_frame (vm, node, next, n_left_to_next);
264  }
265 
266  return frame->n_vectors;
267 }
268 
270  .function = mpls_load_balance,
271  .name = "mpls-load-balance",
272  .vector_size = sizeof (u32),
273  .sibling_of = "mpls-lookup",
274 
275  .format_trace = format_mpls_load_balance_trace,
276 };
277 
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
u16 lb_n_buckets
number of buckets in the load-balance.
Definition: load_balance.h:87
ip_lookup_next_t
Common (IP4/IP6) next index stored in adjacency.
Definition: lookup.h:60
vlib_combined_counter_main_t lbm_to_counters
Definition: load_balance.h:45
#define CLIB_UNUSED(x)
Definition: clib.h:79
format_function_t format_mpls_header
Definition: mpls.h:166
static u32 mpls_compute_flow_hash(const mpls_unicast_header_t *hdr, flow_hash_config_t flow_hash_config)
Definition: mpls_lookup.c:54
u32 * fib_index_by_sw_if_index
Definition: mpls.h:89
flow_hash_config_t lb_hash_config
the hash config to use when selecting a bucket.
Definition: load_balance.h:122
struct _vlib_node_registration vlib_node_registration_t
static uword vlib_buffer_length_in_chain(vlib_main_t *vm, vlib_buffer_t *b)
Get length in bytes of the buffer chain.
Definition: buffer_funcs.h:112
#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
u16 lb_n_buckets_minus_1
number of buckets in the load-balance - 1.
Definition: load_balance.h:92
static uword mpls_lookup(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *from_frame)
Definition: mpls_lookup.c:62
vlib_node_registration_t mpls_lookup_node
(constructor) VLIB_REGISTER_NODE (mpls_lookup_node)
Definition: mpls_lookup.c:24
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 const dpo_id_t * load_balance_get_bucket_i(const load_balance_t *lb, u32 bucket)
Definition: load_balance.h:194
uword os_get_cpu_number(void)
Definition: unix-misc.c:224
The FIB DPO provieds;.
Definition: load_balance.h:83
#define PREDICT_FALSE(x)
Definition: clib.h:97
static u32 vnet_mpls_uc_get_label(mpls_label_t label_exp_s_ttl)
Definition: packet.h:77
load_balance_main_t load_balance_main
The one instance of load-balance main.
Definition: load_balance.c:55
#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
static void vlib_node_increment_counter(vlib_main_t *vm, u32 node_index, u32 counter_index, u64 increment)
Definition: node_funcs.h:1113
u16 n_vectors
Definition: node.h:344
mpls_main_t mpls_main
Definition: mpls.c:25
unformat_function_t unformat_mpls_header
Definition: mpls.h:181
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_combined_counter_main_t lbm_via_counters
Definition: load_balance.h:46
u16 cached_next_index
Definition: node.h:463
static void vlib_increment_combined_counter(vlib_combined_counter_main_t *cm, u32 cpu_index, u32 index, u32 packet_increment, u32 byte_increment)
Increment a combined counter.
Definition: counter.h:241
#define ASSERT(truth)
unsigned int u32
Definition: types.h:88
#define vnet_buffer(b)
Definition: buffer.h:333
static load_balance_t * load_balance_get(index_t lbi)
Definition: load_balance.h:185
static char * mpls_error_strings[]
Definition: mpls_lookup.c:154
mpls_label_t label_exp_s_ttl
Definition: packet.h:31
u32 flow_hash_config_t
A flow hash configuration is a mask of the flow hash options.
Definition: lookup.h:157
static uword mpls_load_balance(vlib_main_t *vm, vlib_node_runtime_t *node, vlib_frame_t *frame)
Definition: mpls_lookup.c:195
#define VLIB_BUFFER_IS_TRACED
Definition: buffer.h:95
static uword is_pow2(uword x)
Definition: clib.h:266
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
#define vec_elt(v, i)
Get vector value at index i.
Definition: defs.h:47
index_t dpoi_index
the index of objects of that type
Definition: dpo.h:154
static u32 vnet_mpls_uc_get_s(mpls_label_t label_exp_s_ttl)
Definition: packet.h:87
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
A collection of combined counters.
Definition: counter.h:212
static index_t mpls_fib_table_forwarding_lookup(u32 mpls_fib_index, const mpls_unicast_header_t *hdr)
Lookup a label and EOS bit in the MPLS_FIB table to retrieve the load-balance index to be used for pa...
Definition: mpls_fib.h:79
#define VLIB_NODE_FUNCTION_MULTIARCH(node, fn)
Definition: node.h:158
static u8 * format_mpls_load_balance_trace(u8 *s, va_list *args)
Definition: mpls_lookup.c:183
#define VLIB_REGISTER_NODE(x,...)
Definition: node.h:143
u8 * format(u8 *s, const char *fmt,...)
Definition: format.c:418
u16 dpoi_next_node
The next VLIB node to follow.
Definition: dpo.h:150
static u8 * format_mpls_lookup_trace(u8 *s, va_list *args)
Definition: mpls_lookup.c:34
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
Definition: defs.h:46
vlib_node_registration_t mpls_load_balance_node
(constructor) VLIB_REGISTER_NODE (mpls_load_balance_node)
Definition: mpls_lookup.c:269