Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [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/fib/fib_entry_delegate.h> |
| 17 | #include <vnet/fib/fib_entry.h> |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 18 | #include <vnet/fib/fib_attached_export.h> |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 19 | |
| 20 | static fib_entry_delegate_t * |
| 21 | fib_entry_delegate_find_i (const fib_entry_t *fib_entry, |
| 22 | fib_entry_delegate_type_t type, |
| 23 | u32 *index) |
| 24 | { |
| 25 | fib_entry_delegate_t *delegate; |
| 26 | int ii; |
| 27 | |
| 28 | ii = 0; |
| 29 | vec_foreach(delegate, fib_entry->fe_delegates) |
| 30 | { |
| 31 | if (delegate->fd_type == type) |
| 32 | { |
| 33 | if (NULL != index) |
| 34 | *index = ii; |
| 35 | |
| 36 | return (delegate); |
| 37 | } |
| 38 | else |
| 39 | { |
| 40 | ii++; |
| 41 | } |
| 42 | } |
| 43 | |
| 44 | return (NULL); |
| 45 | } |
| 46 | |
| 47 | fib_entry_delegate_t * |
| 48 | fib_entry_delegate_get (const fib_entry_t *fib_entry, |
| 49 | fib_entry_delegate_type_t type) |
| 50 | { |
| 51 | return (fib_entry_delegate_find_i(fib_entry, type, NULL)); |
| 52 | } |
| 53 | |
| 54 | void |
| 55 | fib_entry_delegate_remove (fib_entry_t *fib_entry, |
| 56 | fib_entry_delegate_type_t type) |
| 57 | { |
| 58 | fib_entry_delegate_t *fed; |
| 59 | u32 index = ~0; |
| 60 | |
| 61 | fed = fib_entry_delegate_find_i(fib_entry, type, &index); |
| 62 | |
| 63 | ASSERT(NULL != fed); |
| 64 | |
| 65 | vec_del1(fib_entry->fe_delegates, index); |
| 66 | } |
| 67 | |
| 68 | static int |
| 69 | fib_entry_delegate_cmp_for_sort (void * v1, |
| 70 | void * v2) |
| 71 | { |
| 72 | fib_entry_delegate_t *delegate1 = v1, *delegate2 = v2; |
| 73 | |
| 74 | return (delegate1->fd_type - delegate2->fd_type); |
| 75 | } |
| 76 | |
| 77 | static void |
| 78 | fib_entry_delegate_init (fib_entry_t *fib_entry, |
| 79 | fib_entry_delegate_type_t type) |
| 80 | |
| 81 | { |
| 82 | fib_entry_delegate_t delegate = { |
| 83 | .fd_entry_index = fib_entry_get_index(fib_entry), |
| 84 | .fd_type = type, |
| 85 | }; |
| 86 | |
| 87 | vec_add1(fib_entry->fe_delegates, delegate); |
| 88 | vec_sort_with_function(fib_entry->fe_delegates, |
| 89 | fib_entry_delegate_cmp_for_sort); |
| 90 | } |
| 91 | |
| 92 | fib_entry_delegate_t * |
| 93 | fib_entry_delegate_find_or_add (fib_entry_t *fib_entry, |
| 94 | fib_entry_delegate_type_t fdt) |
| 95 | { |
| 96 | fib_entry_delegate_t *delegate; |
| 97 | |
| 98 | delegate = fib_entry_delegate_get(fib_entry, fdt); |
| 99 | |
| 100 | if (NULL == delegate) |
| 101 | { |
| 102 | fib_entry_delegate_init(fib_entry, fdt); |
| 103 | } |
| 104 | |
| 105 | return (fib_entry_delegate_get(fib_entry, fdt)); |
| 106 | } |
| 107 | |
| 108 | fib_entry_delegate_type_t |
| 109 | fib_entry_chain_type_to_delegate_type (fib_forward_chain_type_t fct) |
| 110 | { |
| 111 | switch (fct) |
| 112 | { |
| 113 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP4: |
| 114 | return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4); |
| 115 | case FIB_FORW_CHAIN_TYPE_UNICAST_IP6: |
| 116 | return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6); |
| 117 | case FIB_FORW_CHAIN_TYPE_MPLS_EOS: |
| 118 | return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS); |
| 119 | case FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS: |
| 120 | return (FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS); |
| 121 | case FIB_FORW_CHAIN_TYPE_ETHERNET: |
| 122 | return (FIB_ENTRY_DELEGATE_CHAIN_ETHERNET); |
Neale Ranns | 32e1c01 | 2016-11-22 17:07:28 +0000 | [diff] [blame] | 123 | case FIB_FORW_CHAIN_TYPE_MCAST_IP4: |
| 124 | case FIB_FORW_CHAIN_TYPE_MCAST_IP6: |
| 125 | break; |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 126 | case FIB_FORW_CHAIN_TYPE_NSH: |
| 127 | return (FIB_ENTRY_DELEGATE_CHAIN_NSH); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 128 | } |
| 129 | ASSERT(0); |
| 130 | return (FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4); |
| 131 | } |
| 132 | |
| 133 | fib_forward_chain_type_t |
| 134 | fib_entry_delegate_type_to_chain_type (fib_entry_delegate_type_t fdt) |
| 135 | { |
| 136 | switch (fdt) |
| 137 | { |
| 138 | case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4: |
| 139 | return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); |
| 140 | case FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6: |
| 141 | return (FIB_FORW_CHAIN_TYPE_UNICAST_IP6); |
| 142 | case FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS: |
| 143 | return (FIB_FORW_CHAIN_TYPE_MPLS_EOS); |
| 144 | case FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS: |
| 145 | return (FIB_FORW_CHAIN_TYPE_MPLS_NON_EOS); |
| 146 | case FIB_ENTRY_DELEGATE_CHAIN_ETHERNET: |
| 147 | return (FIB_FORW_CHAIN_TYPE_ETHERNET); |
Florin Coras | ce1b4c7 | 2017-01-26 14:25:34 -0800 | [diff] [blame] | 148 | case FIB_ENTRY_DELEGATE_CHAIN_NSH: |
| 149 | return (FIB_FORW_CHAIN_TYPE_NSH); |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 150 | case FIB_ENTRY_DELEGATE_COVERED: |
| 151 | case FIB_ENTRY_DELEGATE_ATTACHED_IMPORT: |
| 152 | case FIB_ENTRY_DELEGATE_ATTACHED_EXPORT: |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 153 | case FIB_ENTRY_DELEGATE_BFD: |
Neale Ranns | ad422ed | 2016-11-02 14:20:04 +0000 | [diff] [blame] | 154 | break; |
| 155 | } |
| 156 | ASSERT(0); |
| 157 | return (FIB_FORW_CHAIN_TYPE_UNICAST_IP4); |
| 158 | } |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 159 | |
| 160 | /** |
| 161 | * typedef for printing a delegate |
| 162 | */ |
| 163 | typedef u8 * (*fib_entry_delegate_format_t)(const fib_entry_delegate_t *fed, |
| 164 | u8 *s); |
| 165 | |
| 166 | /** |
| 167 | * Print a delegate that represents a forwarding chain |
| 168 | */ |
| 169 | static u8 * |
| 170 | fib_entry_delegate_fmt_fwd_chain (const fib_entry_delegate_t *fed, |
| 171 | u8 *s) |
| 172 | { |
| 173 | s = format(s, "%U-chain\n %U", |
| 174 | format_fib_forw_chain_type, |
| 175 | fib_entry_delegate_type_to_chain_type(fed->fd_type), |
| 176 | format_dpo_id, &fed->fd_dpo, 2); |
| 177 | |
| 178 | return (s); |
| 179 | } |
| 180 | |
| 181 | /** |
| 182 | * Print a delegate that represents cover tracking |
| 183 | */ |
| 184 | static u8 * |
| 185 | fib_entry_delegate_fmt_covered (const fib_entry_delegate_t *fed, |
| 186 | u8 *s) |
| 187 | { |
| 188 | s = format(s, "covered:["); |
| 189 | s = fib_node_children_format(fed->fd_list, s); |
| 190 | s = format(s, "]"); |
| 191 | |
| 192 | return (s); |
| 193 | } |
| 194 | |
| 195 | /** |
| 196 | * Print a delegate that represents attached-import tracking |
| 197 | */ |
| 198 | static u8 * |
| 199 | fib_entry_delegate_fmt_import (const fib_entry_delegate_t *fed, |
| 200 | u8 *s) |
| 201 | { |
| 202 | s = format(s, "import:%U", fib_ae_import_format, fed->fd_index); |
| 203 | |
| 204 | return (s); |
| 205 | } |
| 206 | |
| 207 | /** |
| 208 | * Print a delegate that represents attached-export tracking |
| 209 | */ |
| 210 | static u8 * |
| 211 | fib_entry_delegate_fmt_export (const fib_entry_delegate_t *fed, |
| 212 | u8 *s) |
| 213 | { |
| 214 | s = format(s, "export:%U", fib_ae_export_format, fed->fd_index); |
| 215 | |
| 216 | return (s); |
| 217 | } |
| 218 | |
| 219 | /** |
| 220 | * Print a delegate that represents BFD tracking |
| 221 | */ |
| 222 | static u8 * |
| 223 | fib_entry_delegate_fmt_bfd (const fib_entry_delegate_t *fed, |
| 224 | u8 *s) |
| 225 | { |
| 226 | s = format(s, "BFD:%d", fed->fd_bfd_state); |
| 227 | |
| 228 | return (s); |
| 229 | } |
| 230 | |
| 231 | /** |
| 232 | * A delegate type to formatter map |
| 233 | */ |
| 234 | static fib_entry_delegate_format_t fed_formatters[] = |
| 235 | { |
| 236 | [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP4] = fib_entry_delegate_fmt_fwd_chain, |
| 237 | [FIB_ENTRY_DELEGATE_CHAIN_UNICAST_IP6] = fib_entry_delegate_fmt_fwd_chain, |
| 238 | [FIB_ENTRY_DELEGATE_CHAIN_MPLS_EOS] = fib_entry_delegate_fmt_fwd_chain, |
| 239 | [FIB_ENTRY_DELEGATE_CHAIN_MPLS_NON_EOS] = fib_entry_delegate_fmt_fwd_chain, |
| 240 | [FIB_ENTRY_DELEGATE_CHAIN_ETHERNET] = fib_entry_delegate_fmt_fwd_chain, |
| 241 | [FIB_ENTRY_DELEGATE_CHAIN_NSH] = fib_entry_delegate_fmt_fwd_chain, |
| 242 | [FIB_ENTRY_DELEGATE_COVERED] = fib_entry_delegate_fmt_covered, |
| 243 | [FIB_ENTRY_DELEGATE_ATTACHED_IMPORT] = fib_entry_delegate_fmt_import, |
| 244 | [FIB_ENTRY_DELEGATE_ATTACHED_EXPORT] = fib_entry_delegate_fmt_export, |
| 245 | [FIB_ENTRY_DELEGATE_BFD] = fib_entry_delegate_fmt_bfd, |
| 246 | }; |
| 247 | |
| 248 | u8 * |
| 249 | format_fib_entry_deletegate (u8 * s, va_list * args) |
| 250 | { |
| 251 | fib_entry_delegate_t *fed; |
| 252 | |
| 253 | fed = va_arg (*args, fib_entry_delegate_t *); |
| 254 | |
| 255 | return (fed_formatters[fed->fd_type](fed, s)); |
| 256 | } |