blob: 0125c89f5fb6bf427d9b7608e3915b4b3e69ac3b [file] [log] [blame]
Neale Ranns88fc83e2017-04-05 08:11:14 -07001/*
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 Troan793c7fe2018-02-15 16:14:56 +010020/*
21 * The per-type vector of virtual function tables
22 */
23static adj_delegate_vft_t *ad_vfts;
24
Neale Ranns88fc83e2017-04-05 08:11:14 -070025static adj_delegate_t *
26adj_delegate_find_i (const ip_adjacency_t *adj,
27 adj_delegate_type_t type,
28 u32 *index)
29{
30 adj_delegate_t *delegate;
31 int ii;
32
33 ii = 0;
34 vec_foreach(delegate, adj->ia_delegates)
35 {
36 if (delegate->ad_type == type)
37 {
38 if (NULL != index)
39 *index = ii;
40
41 return (delegate);
42 }
43 else
44 {
45 ii++;
46 }
47 }
48
49 return (NULL);
50}
51
52adj_delegate_t *
53adj_delegate_get (const ip_adjacency_t *adj,
54 adj_delegate_type_t type)
55{
56 return (adj_delegate_find_i(adj, type, NULL));
57}
58
59void
60adj_delegate_remove (ip_adjacency_t *adj,
61 adj_delegate_type_t type)
62{
63 adj_delegate_t *aed;
64 u32 index = ~0;
65
66 aed = adj_delegate_find_i(adj, type, &index);
67
68 ASSERT(NULL != aed);
69
70 vec_del1(adj->ia_delegates, index);
71}
72
73static int
74adj_delegate_cmp_for_sort (void * v1,
75 void * v2)
76{
77 adj_delegate_t *delegate1 = v1, *delegate2 = v2;
78
79 return (delegate1->ad_type - delegate2->ad_type);
80}
81
82static void
83adj_delegate_init (ip_adjacency_t *adj,
84 adj_delegate_type_t type)
85
86{
87 adj_delegate_t delegate = {
88 .ad_adj_index = adj_get_index(adj),
89 .ad_type = type,
90 };
91
92 vec_add1(adj->ia_delegates, delegate);
93 vec_sort_with_function(adj->ia_delegates,
94 adj_delegate_cmp_for_sort);
95}
96
97adj_delegate_t *
98adj_delegate_find_or_add (ip_adjacency_t *adj,
99 adj_delegate_type_t adt)
100{
101 adj_delegate_t *delegate;
102
103 delegate = adj_delegate_get(adj, adt);
104
105 if (NULL == delegate)
106 {
107 adj_delegate_init(adj, adt);
108 }
109
110 return (adj_delegate_get(adj, adt));
111}
112
Ole Troan793c7fe2018-02-15 16:14:56 +0100113void adj_delegate_vft_lock_gone (ip_adjacency_t *adj)
Neale Ranns88fc83e2017-04-05 08:11:14 -0700114{
Ole Troan793c7fe2018-02-15 16:14:56 +0100115 adj_delegate_t *delegate;
116 vec_foreach(delegate, adj->ia_delegates) {
117 if (ad_vfts[delegate->ad_type].adv_last_lock)
118 ad_vfts[delegate->ad_type].adv_last_lock(adj, delegate);
119 }
Neale Ranns88fc83e2017-04-05 08:11:14 -0700120}
121
Neale Ranns88fc83e2017-04-05 08:11:14 -0700122u8 *
Ole Troan793c7fe2018-02-15 16:14:56 +0100123format_adj_delegate (u8 * s, va_list * args)
Neale Ranns88fc83e2017-04-05 08:11:14 -0700124{
125 adj_delegate_t *aed;
126
127 aed = va_arg (*args, adj_delegate_t *);
Ole Troan793c7fe2018-02-15 16:14:56 +0100128 if (ad_vfts[aed->ad_type].adv_format)
129 return ad_vfts[aed->ad_type].adv_format(aed, s);
130 return format(s, "unknown delegate");
131}
Neale Ranns88fc83e2017-04-05 08:11:14 -0700132
Ole Troan793c7fe2018-02-15 16:14:56 +0100133/**
134 * adj_delegate_register_type
135 *
136 * Register the function table for a given type
137 */
138
139void
140adj_delegate_register_type (adj_delegate_type_t type,
141 const adj_delegate_vft_t *vft)
142{
143 /*
144 * assert that one only registration is made per-node type
145 */
146 if (vec_len(ad_vfts) > type)
147 ASSERT(NULL == ad_vfts[type].adv_last_lock);
148
149 vec_validate(ad_vfts, type);
150 ad_vfts[type] = *vft;
Neale Ranns88fc83e2017-04-05 08:11:14 -0700151}