Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [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/adj/adj_delegate.h> |
| 17 | #include <vnet/adj/adj.h> |
| 18 | #include <vnet/adj/adj_internal.h> |
| 19 | |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 20 | /* |
| 21 | * The per-type vector of virtual function tables |
| 22 | */ |
| 23 | static adj_delegate_vft_t *ad_vfts; |
| 24 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 25 | /** |
Paul Vinciguerra | 8feeaff | 2019-03-27 11:25:48 -0700 | [diff] [blame] | 26 | * The value of the last dynamically allocated delegate value |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 27 | */ |
Neale Ranns | 4c3ba81 | 2019-03-26 07:02:58 +0000 | [diff] [blame] | 28 | static adj_delegate_type_t ad_max_id = ADJ_DELEGATE_LAST; |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 29 | |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 30 | static adj_delegate_t * |
| 31 | adj_delegate_find_i (const ip_adjacency_t *adj, |
| 32 | adj_delegate_type_t type, |
| 33 | u32 *index) |
| 34 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 35 | adj_delegate_t *delegate; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 36 | int ii; |
| 37 | |
| 38 | ii = 0; |
| 39 | vec_foreach(delegate, adj->ia_delegates) |
| 40 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 41 | if (delegate->ad_type == type) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 42 | { |
| 43 | if (NULL != index) |
| 44 | *index = ii; |
| 45 | |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 46 | return (delegate); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 47 | } |
| 48 | else |
| 49 | { |
| 50 | ii++; |
| 51 | } |
| 52 | } |
| 53 | |
| 54 | return (NULL); |
| 55 | } |
| 56 | |
| 57 | adj_delegate_t * |
| 58 | adj_delegate_get (const ip_adjacency_t *adj, |
| 59 | adj_delegate_type_t type) |
| 60 | { |
| 61 | return (adj_delegate_find_i(adj, type, NULL)); |
| 62 | } |
| 63 | |
| 64 | void |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 65 | adj_delegate_remove (adj_index_t ai, |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 66 | adj_delegate_type_t type) |
| 67 | { |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 68 | ip_adjacency_t *adj; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 69 | adj_delegate_t *aed; |
| 70 | u32 index = ~0; |
| 71 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 72 | adj = adj_get(ai); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 73 | aed = adj_delegate_find_i(adj, type, &index); |
| 74 | |
| 75 | ASSERT(NULL != aed); |
| 76 | |
| 77 | vec_del1(adj->ia_delegates, index); |
| 78 | } |
| 79 | |
| 80 | static int |
| 81 | adj_delegate_cmp_for_sort (void * v1, |
| 82 | void * v2) |
| 83 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 84 | adj_delegate_t *aed1 = v1, *aed2 = v2; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 85 | |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 86 | return (aed1->ad_type - aed2->ad_type); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 87 | } |
| 88 | |
| 89 | static void |
| 90 | adj_delegate_init (ip_adjacency_t *adj, |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 91 | adj_delegate_type_t adt, |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 92 | index_t adi) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 93 | |
| 94 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 95 | adj_delegate_t aed = { |
| 96 | .ad_adj_index = adj_get_index(adj), |
| 97 | .ad_type = adt, |
| 98 | .ad_index = adi, |
| 99 | }; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 100 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 101 | vec_add1(adj->ia_delegates, aed); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 102 | vec_sort_with_function(adj->ia_delegates, |
| 103 | adj_delegate_cmp_for_sort); |
| 104 | } |
| 105 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 106 | int |
| 107 | adj_delegate_add (ip_adjacency_t *adj, |
| 108 | adj_delegate_type_t adt, |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 109 | index_t adi) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 110 | { |
| 111 | adj_delegate_t *delegate; |
| 112 | |
| 113 | delegate = adj_delegate_get(adj, adt); |
| 114 | |
| 115 | if (NULL == delegate) |
| 116 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 117 | adj_delegate_init(adj, adt, adi); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 118 | } |
| 119 | else |
| 120 | { |
| 121 | return (-1); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 122 | } |
| 123 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 124 | return (0); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 125 | } |
| 126 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 127 | void |
Neale Ranns | 4ec36c5 | 2020-03-31 09:21:29 -0400 | [diff] [blame] | 128 | adj_delegate_adj_modified (ip_adjacency_t *adj) |
| 129 | { |
| 130 | adj_delegate_t *aed; |
| 131 | |
| 132 | vec_foreach(aed, adj->ia_delegates) |
| 133 | { |
| 134 | if (ad_vfts[aed->ad_type].adv_adj_modified) |
| 135 | { |
| 136 | ad_vfts[aed->ad_type].adv_adj_modified(aed); |
| 137 | } |
| 138 | } |
| 139 | } |
| 140 | |
| 141 | void |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 142 | adj_delegate_adj_deleted (ip_adjacency_t *adj) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 143 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 144 | adj_delegate_t *aed; |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 145 | |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 146 | vec_foreach(aed, adj->ia_delegates) |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 147 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 148 | if (ad_vfts[aed->ad_type].adv_adj_deleted) |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 149 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 150 | ad_vfts[aed->ad_type].adv_adj_deleted(aed); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 151 | } |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 152 | } |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 153 | |
| 154 | vec_reset_length(adj->ia_delegates); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 155 | } |
| 156 | |
Neale Ranns | 77cfc01 | 2019-12-15 22:26:37 +0000 | [diff] [blame] | 157 | void |
| 158 | adj_delegate_adj_created (ip_adjacency_t *adj) |
| 159 | { |
| 160 | adj_delegate_vft_t *vft; |
| 161 | |
| 162 | vec_foreach(vft, ad_vfts) |
| 163 | { |
| 164 | if (vft->adv_adj_created) |
| 165 | { |
| 166 | vft->adv_adj_created(adj_get_index(adj)); |
| 167 | } |
| 168 | } |
| 169 | } |
| 170 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 171 | u8* |
| 172 | adj_delegate_format (u8* s, ip_adjacency_t *adj) |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 173 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 174 | adj_delegate_t *aed; |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 175 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 176 | vec_foreach(aed, adj->ia_delegates) |
| 177 | { |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 178 | if (ad_vfts[aed->ad_type].adv_format) |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 179 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 180 | s = format(s, "\n {"); |
Neale Ranns | 76447a7 | 2018-02-20 06:25:02 -0800 | [diff] [blame] | 181 | s = ad_vfts[aed->ad_type].adv_format(aed, s); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 182 | s = format(s, "}"); |
| 183 | } |
| 184 | else |
| 185 | { |
Neale Ranns | 14053c9 | 2019-12-29 23:55:18 +0000 | [diff] [blame] | 186 | s = format(s, "\n {unknown delegate}"); |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 187 | } |
| 188 | } |
| 189 | |
| 190 | return (s); |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 191 | } |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 192 | |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 193 | /** |
| 194 | * adj_delegate_register_type |
| 195 | * |
| 196 | * Register the function table for a given type |
| 197 | */ |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 198 | void |
| 199 | adj_delegate_register_type (adj_delegate_type_t type, |
| 200 | const adj_delegate_vft_t *vft) |
| 201 | { |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 202 | /* |
| 203 | * assert that one only registration is made per-node type |
| 204 | */ |
| 205 | if (vec_len(ad_vfts) > type) |
| 206 | ASSERT(NULL == ad_vfts[type].adv_adj_deleted); |
Ole Troan | 793c7fe | 2018-02-15 16:14:56 +0100 | [diff] [blame] | 207 | |
Neale Ranns | d79a43c | 2018-02-19 02:36:19 -0800 | [diff] [blame] | 208 | vec_validate(ad_vfts, type); |
| 209 | ad_vfts[type] = *vft; |
| 210 | } |
| 211 | |
| 212 | /** |
| 213 | * adj_delegate_register_new_type |
| 214 | * |
| 215 | * Register the function table for a new type |
| 216 | */ |
| 217 | adj_delegate_type_t |
| 218 | adj_delegate_register_new_type (const adj_delegate_vft_t *vft) |
| 219 | { |
| 220 | adj_delegate_type_t type; |
| 221 | |
| 222 | type = ++ad_max_id; |
| 223 | |
| 224 | vec_validate(ad_vfts, type); |
| 225 | ad_vfts[type] = *vft; |
| 226 | |
| 227 | return (type); |
Neale Ranns | 88fc83e | 2017-04-05 08:11:14 -0700 | [diff] [blame] | 228 | } |