blob: 5b88be17d8f4d48863da87552785c4c17c2004a4 [file] [log] [blame]
Neale Ranns6af1c042017-05-26 03:48:53 -07001/*
2 * Copyright (c) 2015 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#ifndef __MPLS_LOOKUP_H__
17#define __MPLS_LOOKUP_H__
18
19#include <vnet/mpls/mpls.h>
20#include <vnet/ip/ip.h>
Neale Rannsef90ed02018-09-13 08:45:12 -070021#include <vnet/bier/bier_fwd.h>
Neale Rannse4031132020-10-26 13:00:06 +000022#include <vnet/ip/ip4_inlines.h>
23#include <vnet/ip/ip6_inlines.h>
Neale Ranns6af1c042017-05-26 03:48:53 -070024
25/**
26 * The arc/edge from the MPLS lookup node to the MPLS replicate node
27 */
BenoƮt Ganne47727c02019-02-12 13:35:08 +010028extern u32 mpls_lookup_to_replicate_edge;
Neale Ranns6af1c042017-05-26 03:48:53 -070029
Neale Rannsce9e0b42018-08-01 12:53:17 -070030/**
31 * Enum of statically configred MPLS lookup next nodes
32 */
33typedef enum mpls_lookup_next_t_
34{
35 MPLS_LOOKUP_NEXT_DROP = 0,
36} mpls_lookup_next_t;
37
Neale Ranns6af1c042017-05-26 03:48:53 -070038/*
39 * Compute flow hash.
40 * We'll use it to select which adjacency to use for this flow. And other things.
41 */
42always_inline u32
43mpls_compute_flow_hash (const mpls_unicast_header_t * hdr,
44 flow_hash_config_t flow_hash_config)
45{
46 /*
47 * We need to byte swap so we use the numerical value. i.e. an odd label
48 * leads to an odd bucket. as opposed to a label above and below value X.
49 */
50 u8 next_label_is_entropy;
51 mpls_label_t ho_label;
52 u32 hash, value;
53
54 ho_label = clib_net_to_host_u32(hdr->label_exp_s_ttl);
55 hash = vnet_mpls_uc_get_label(ho_label);
56 next_label_is_entropy = 0;
57
58 while (MPLS_EOS != vnet_mpls_uc_get_s(ho_label))
59 {
60 hdr++;
61 ho_label = clib_net_to_host_u32(hdr->label_exp_s_ttl);
62 value = vnet_mpls_uc_get_label(ho_label);
63
64 if (1 == next_label_is_entropy)
65 {
66 /*
67 * The label is an entropy value, use it alone as the hash
68 */
69 return (ho_label);
70 }
71 if (MPLS_IETF_ENTROPY_LABEL == value)
72 {
73 /*
74 * we've met a label in the stack indicating that tha next
75 * label is an entropy value
76 */
77 next_label_is_entropy = 1;
78 }
79 else
80 {
81 /*
82 * XOR the label values in the stack together to
83 * build up the hash value
84 */
85 hash ^= value;
86 }
87 }
88
89 /*
90 * check the top nibble for v4 and v6
91 */
92 hdr++;
93
94 switch (((u8*)hdr)[0] >> 4)
95 {
96 case 4:
97 /* incorporate the v4 flow-hash */
98 hash ^= ip4_compute_flow_hash ((const ip4_header_t *)hdr,
99 IP_FLOW_HASH_DEFAULT);
100 break;
101 case 6:
102 /* incorporate the v6 flow-hash */
103 hash ^= ip6_compute_flow_hash ((const ip6_header_t *)hdr,
104 IP_FLOW_HASH_DEFAULT);
105 break;
Neale Rannsef90ed02018-09-13 08:45:12 -0700106 case 5:
107 /* incorporate the bier flow-hash */
108 hash ^= bier_compute_flow_hash ((const bier_hdr_t *)hdr);
109 break;
Neale Ranns6af1c042017-05-26 03:48:53 -0700110 default:
111 break;
112 }
113
114 return (hash);
115}
116
117#endif /* __MPLS_LOOKUP_H__ */