Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 1 | /* |
| 2 | * MPLS packet format |
| 3 | * |
| 4 | * Copyright (c) 2012 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 | |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 18 | #ifndef included_vnet_mpls_packet_h |
| 19 | #define included_vnet_mpls_packet_h |
| 20 | |
| 21 | #include <vnet/ip/ip_packet.h> |
| 22 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 23 | /** |
| 24 | * A label value only, i.e. 20bits. |
| 25 | */ |
| 26 | typedef u32 mpls_label_t; |
| 27 | |
| 28 | typedef struct { |
| 29 | /* Label: top 20 bits [in network byte order] */ |
| 30 | /* Experimental: 3 bits ... */ |
| 31 | /* S (bottom of label stack): 1 bit */ |
| 32 | /* TTL: 8 bits */ |
| 33 | mpls_label_t label_exp_s_ttl; |
| 34 | } mpls_unicast_header_t; |
| 35 | |
| 36 | typedef enum mpls_eos_bit_t_ |
| 37 | { |
| 38 | MPLS_NON_EOS = 0, |
| 39 | MPLS_EOS = 1, |
| 40 | } mpls_eos_bit_t; |
| 41 | |
| 42 | #define MPLS_EOS_BITS { \ |
| 43 | [MPLS_NON_EOS] = "neos", \ |
| 44 | [MPLS_EOS] = "eos", \ |
| 45 | } |
| 46 | |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 47 | /** |
| 48 | * The Default TTL added to MPLS label headers when no other value is available |
| 49 | */ |
| 50 | #define MPLS_LABEL_DEFAULT_TTL 64 |
| 51 | |
| 52 | /** |
| 53 | * The Default EXP added to MPLS label headers when no other value is available |
| 54 | */ |
| 55 | #define MPLS_LABEL_DEFAULT_EXP 0 |
| 56 | |
| 57 | /** |
| 58 | * When in uniform mode convert an IPv[46] DSCP value to an MPLS EXP value |
| 59 | */ |
Neale Ranns | 038e1df | 2019-07-19 14:01:02 +0000 | [diff] [blame] | 60 | static inline u8 ip_dscp_to_mpls_exp (ip_dscp_t tos) |
Neale Ranns | 31ed744 | 2018-02-23 05:29:09 -0800 | [diff] [blame] | 61 | { |
| 62 | return (tos >> 5); |
| 63 | } |
| 64 | |
| 65 | /** |
| 66 | * When in uniform mode convert an MPLS EXP value to an IPv[46] DSCP value |
| 67 | */ |
| 68 | static inline u8 mpls_exp_to_ip_dscp (u8 exp) |
| 69 | { |
| 70 | return (exp << 5); |
| 71 | } |
| 72 | |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 73 | #define FOR_EACH_MPLS_EOS_BIT(_eos) \ |
| 74 | for (_eos = MPLS_NON_EOS; _eos <= MPLS_EOS; _eos++) |
| 75 | |
| 76 | #define MPLS_ENTRY_LABEL_OFFSET 0 |
| 77 | #define MPLS_ENTRY_LABEL_SHIFT 12 |
| 78 | #define MPLS_ENTRY_LABEL_MASK 0x000fffff |
| 79 | #define MPLS_ENTRY_LABEL_BITS \ |
| 80 | (MPLS_ENTRY_LABEL_MASK << MPLS_ENTRY_LABEL_SHIFT) |
| 81 | |
| 82 | #define MPLS_ENTRY_EXP_OFFSET 2 /* byte offset to EXP bits */ |
| 83 | #define MPLS_ENTRY_EXP_SHIFT 9 |
| 84 | #define MPLS_ENTRY_EXP_MASK 0x07 |
| 85 | #define MPLS_ENTRY_EXP(mpls) \ |
| 86 | (((mpls)>>MPLS_ENTRY_EXP_SHIFT) & MPLS_ENTRY_EXP_MASK) |
| 87 | #define MPLS_ENTRY_EXP_BITS \ |
| 88 | (MPLS_ENTRY_EXP_MASK << MPLS_ENTRY_EXP_SHIFT) |
| 89 | |
| 90 | #define MPLS_ENTRY_EOS_OFFSET 2 /* byte offset to EOS bit */ |
| 91 | #define MPLS_ENTRY_EOS_SHIFT 8 |
| 92 | #define MPLS_ENTRY_EOS_MASK 0x01 /* EOS bit in its byte */ |
| 93 | #define MPLS_ENTRY_EOS(mpls) \ |
| 94 | (((mpls) >> MPLS_ENTRY_EOS_SHIFT) & MPLS_ENTRY_EOS_MASK) |
| 95 | #define MPLS_ENTRY_EOS_BIT (MPLS_ENTRY_EOS_MASK << MPLS_ENTRY_EOS_SHIFT) |
| 96 | |
| 97 | #define MPLS_ENTRY_TTL_OFFSET 3 /* byte offset to ttl field */ |
| 98 | #define MPLS_ENTRY_TTL_SHIFT 0 |
| 99 | #define MPLS_ENTRY_TTL_MASK 0xff |
| 100 | #define MPLS_ENTRY_TTL(mpls) \ |
| 101 | (((mpls) >> MPLS_ENTRY_TTL_SHIFT) & MPLS_ENTRY_TTL_MASK) |
| 102 | #define MPLS_ENTRY_TTL_BITS \ |
| 103 | (MPLS_ENTRY_TTL_MASK << MPLS_ENTRY_TTL_SHIFT) |
| 104 | |
| 105 | static inline u32 vnet_mpls_uc_get_label (mpls_label_t label_exp_s_ttl) |
| 106 | { |
| 107 | return (label_exp_s_ttl>>MPLS_ENTRY_LABEL_SHIFT); |
| 108 | } |
| 109 | |
| 110 | static inline u32 vnet_mpls_uc_get_exp (mpls_label_t label_exp_s_ttl) |
| 111 | { |
| 112 | return (MPLS_ENTRY_EXP(label_exp_s_ttl)); |
| 113 | } |
| 114 | |
| 115 | static inline u32 vnet_mpls_uc_get_s (mpls_label_t label_exp_s_ttl) |
| 116 | { |
| 117 | return (MPLS_ENTRY_EOS(label_exp_s_ttl)); |
| 118 | } |
| 119 | |
| 120 | static inline u32 vnet_mpls_uc_get_ttl (mpls_label_t label_exp_s_ttl) |
| 121 | { |
| 122 | return (MPLS_ENTRY_TTL(label_exp_s_ttl)); |
| 123 | } |
| 124 | |
| 125 | static inline void vnet_mpls_uc_set_label (mpls_label_t *label_exp_s_ttl, |
| 126 | u32 value) |
| 127 | { |
| 128 | *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_LABEL_BITS)) | |
| 129 | ((value & MPLS_ENTRY_LABEL_MASK) << MPLS_ENTRY_LABEL_SHIFT)); |
| 130 | } |
| 131 | |
| 132 | static inline void vnet_mpls_uc_set_exp (mpls_label_t *label_exp_s_ttl, |
| 133 | u32 exp) |
| 134 | { |
| 135 | *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EXP_BITS)) | |
| 136 | ((exp & MPLS_ENTRY_EXP_MASK) << MPLS_ENTRY_EXP_SHIFT)); |
| 137 | } |
| 138 | |
| 139 | static inline void vnet_mpls_uc_set_s (mpls_label_t *label_exp_s_ttl, |
| 140 | u32 eos) |
| 141 | { |
| 142 | *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_EOS_BIT)) | |
| 143 | ((eos & MPLS_ENTRY_EOS_MASK) << MPLS_ENTRY_EOS_SHIFT)); |
| 144 | } |
| 145 | |
| 146 | static inline void vnet_mpls_uc_set_ttl (mpls_label_t *label_exp_s_ttl, |
| 147 | u32 ttl) |
| 148 | { |
| 149 | *label_exp_s_ttl = (((*label_exp_s_ttl) & ~(MPLS_ENTRY_TTL_BITS)) | |
| 150 | ((ttl & MPLS_ENTRY_TTL_MASK))); |
| 151 | } |
| 152 | |
| 153 | #endif /* included_vnet_mpls_packet_h */ |