Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 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/mpls/mpls_types.h> |
| 17 | #include <vnet/dpo/drop_dpo.h> |
| 18 | |
| 19 | #include <vnet/fib/fib_table.h> |
| 20 | #include <vnet/fib/fib_entry.h> |
| 21 | #include <vnet/fib/fib_entry_src.h> |
| 22 | #include <vnet/fib/mpls_fib.h> |
| 23 | |
| 24 | /** |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 25 | * Source initialisation Function |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 26 | */ |
| 27 | static void |
| 28 | fib_entry_src_mpls_init (fib_entry_src_t *src) |
| 29 | { |
| 30 | mpls_eos_bit_t eos; |
| 31 | |
Neale Ranns | 450cd30 | 2016-11-09 17:49:42 +0000 | [diff] [blame] | 32 | src->fes_flags = FIB_ENTRY_SRC_FLAG_NONE; |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 33 | src->u.mpls.fesm_label = MPLS_LABEL_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 34 | |
| 35 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 36 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 37 | src->u.mpls.fesm_lfes[eos] = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 38 | } |
| 39 | } |
| 40 | |
| 41 | /** |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 42 | * Source deinitialisation Function |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 43 | */ |
| 44 | static void |
| 45 | fib_entry_src_mpls_deinit (fib_entry_src_t *src) |
| 46 | { |
| 47 | } |
| 48 | |
| 49 | static void |
| 50 | fib_entry_src_mpls_remove (fib_entry_src_t *src) |
| 51 | { |
| 52 | src->fes_pl = FIB_NODE_INDEX_INVALID; |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 53 | src->u.mpls.fesm_label = MPLS_LABEL_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 54 | } |
| 55 | |
| 56 | static void |
| 57 | fib_entry_src_mpls_add (fib_entry_src_t *src, |
| 58 | const fib_entry_t *entry, |
| 59 | fib_entry_flag_t flags, |
Neale Ranns | da78f95 | 2017-05-24 09:15:43 -0700 | [diff] [blame] | 60 | dpo_proto_t proto, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 61 | const dpo_id_t *dpo) |
| 62 | { |
| 63 | src->fes_pl = |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 64 | fib_path_list_create_special(proto, |
| 65 | FIB_PATH_LIST_FLAG_DROP, |
| 66 | drop_dpo_get(proto)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 67 | } |
| 68 | |
| 69 | static void |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 70 | fib_entry_src_mpls_set_data (fib_entry_src_t *src, |
| 71 | const fib_entry_t *entry, |
| 72 | const void *data) |
| 73 | { |
Neale Ranns | 5899fde | 2016-10-12 13:51:05 +0100 | [diff] [blame] | 74 | fib_protocol_t payload_proto; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 75 | fib_node_index_t fei; |
| 76 | mpls_label_t label; |
| 77 | mpls_eos_bit_t eos; |
| 78 | |
| 79 | /* |
| 80 | * post MPLS table alloc and the possible rea-alloc of fib entrys |
| 81 | * the entry pointer will no longer be valid. so save its index |
| 82 | */ |
| 83 | payload_proto = entry->fe_prefix.fp_proto; |
| 84 | fei = fib_entry_get_index(entry); |
| 85 | label = *(mpls_label_t*)data; |
| 86 | |
| 87 | if (MPLS_LABEL_INVALID == label) |
| 88 | { |
| 89 | /* |
| 90 | * removing the local label |
| 91 | */ |
| 92 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 93 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 94 | fib_table_entry_delete_index(src->u.mpls.fesm_lfes[eos], |
| 95 | FIB_SOURCE_SPECIAL); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 96 | } |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 97 | fib_table_unlock(MPLS_FIB_DEFAULT_TABLE_ID, |
| 98 | FIB_PROTOCOL_MPLS, |
| 99 | FIB_SOURCE_MPLS); |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 100 | src->u.mpls.fesm_label = label; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 101 | } |
| 102 | else |
| 103 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 104 | fib_prefix_t prefix = { |
| 105 | .fp_proto = FIB_PROTOCOL_MPLS, |
| 106 | .fp_label = label, |
| 107 | }; |
| 108 | fib_node_index_t fib_index; |
| 109 | dpo_id_t dpo = DPO_INVALID; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 110 | |
| 111 | /* |
| 112 | * adding a new local label. make sure the MPLS fib exists. |
| 113 | */ |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 114 | if (MPLS_LABEL_INVALID == src->u.mpls.fesm_label) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 115 | { |
| 116 | fib_index = |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 117 | fib_table_find_or_create_and_lock(FIB_PROTOCOL_MPLS, |
| 118 | MPLS_FIB_DEFAULT_TABLE_ID, |
Neale Ranns | 1500254 | 2017-09-10 04:39:11 -0700 | [diff] [blame] | 119 | FIB_SOURCE_MPLS); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 120 | } |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 121 | else |
| 122 | { |
| 123 | fib_index = mpls_fib_index_from_table_id(MPLS_FIB_DEFAULT_TABLE_ID); |
Neale Ranns | 1357f3b | 2016-10-16 12:01:42 -0700 | [diff] [blame] | 124 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 125 | /* |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame^] | 126 | * if this is a change in label, remove the old one first |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 127 | */ |
| 128 | if (src->u.mpls.fesm_label != label) |
| 129 | { |
| 130 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 131 | { |
| 132 | ASSERT(FIB_NODE_INDEX_INVALID != src->u.mpls.fesm_lfes[eos]); |
| 133 | fib_table_entry_delete_index(src->u.mpls.fesm_lfes[eos], |
| 134 | FIB_SOURCE_SPECIAL); |
| 135 | } |
| 136 | } |
| 137 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 138 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 139 | src->u.mpls.fesm_label = label; |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 140 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 141 | FOR_EACH_MPLS_EOS_BIT(eos) |
| 142 | { |
| 143 | prefix.fp_eos = eos; |
| 144 | prefix.fp_payload_proto = fib_proto_to_dpo(payload_proto); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 145 | |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 146 | fib_entry_contribute_forwarding(fei, |
| 147 | (eos ? |
| 148 | FIB_FORW_CHAIN_TYPE_MPLS_EOS : |
| 149 | FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS), |
| 150 | &dpo); |
| 151 | src->u.mpls.fesm_lfes[eos] = |
| 152 | fib_table_entry_special_dpo_add(fib_index, |
| 153 | &prefix, |
| 154 | FIB_SOURCE_SPECIAL, |
| 155 | FIB_ENTRY_FLAG_EXCLUSIVE, |
| 156 | &dpo); |
| 157 | dpo_reset(&dpo); |
| 158 | } |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 159 | } |
| 160 | } |
| 161 | |
| 162 | static const void * |
| 163 | fib_entry_src_mpls_get_data (fib_entry_src_t *src, |
| 164 | const fib_entry_t *entry) |
| 165 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 166 | return (&(src->u.mpls.fesm_label)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 167 | } |
| 168 | |
| 169 | static u8* |
| 170 | fib_entry_src_mpls_format (fib_entry_src_t *src, |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 171 | u8* s) |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 172 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 173 | return (format(s, " local-label:%d", src->u.mpls.fesm_label)); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 174 | } |
| 175 | |
| 176 | const static fib_entry_src_vft_t mpls_src_vft = { |
| 177 | .fesv_init = fib_entry_src_mpls_init, |
| 178 | .fesv_deinit = fib_entry_src_mpls_deinit, |
| 179 | .fesv_add = fib_entry_src_mpls_add, |
| 180 | .fesv_remove = fib_entry_src_mpls_remove, |
| 181 | .fesv_format = fib_entry_src_mpls_format, |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 182 | .fesv_set_data = fib_entry_src_mpls_set_data, |
| 183 | .fesv_get_data = fib_entry_src_mpls_get_data, |
Neale Ranns | a4e7766 | 2017-12-04 20:00:30 +0000 | [diff] [blame] | 184 | /* |
| 185 | * .fesv_fwd_update = fib_entry_src_mpls_fwd_update, |
| 186 | * When the forwarding for the IP entry is updated, any MPLS chains |
| 187 | * it has created are also updated. Since the MPLS entry will have already |
| 188 | * installed that chain/load-balance there is no need to update the netry |
| 189 | * FIXME: later: propagate any walk to the children of the MPLS entry. for SR |
| 190 | */ |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 191 | }; |
| 192 | |
| 193 | void |
| 194 | fib_entry_src_mpls_register (void) |
| 195 | { |
Neale Ranns | 2303cb1 | 2018-02-21 04:57:17 -0800 | [diff] [blame] | 196 | fib_entry_src_register(FIB_SOURCE_MPLS, &mpls_src_vft); |
Neale Ranns | 0bfe5d8 | 2016-08-25 15:29:12 +0100 | [diff] [blame] | 197 | } |