blob: 9f6205de4199fec33e745f1d572d84f8abbd5201 [file] [log] [blame]
Neale Ranns32e1c012016-11-22 17:07:28 +00001/*
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
17#include <vnet/vnet.h>
18#include <vnet/mfib/mfib_signal.h>
19#include <vppinfra/dlist.h>
20
21/**
22 * @brief Pool of signals
23 */
24static mfib_signal_t *mfib_signal_pool;
25
26/**
27 * @brief pool of dlist elements
28 */
29static dlist_elt_t *mfib_signal_dlist_pool;
30
31/**
32 * the list/set of interfaces with signals pending
33 */
34typedef struct mfib_signal_q_t_
35{
36 /**
37 * the dlist indext that is the head of the list
38 */
39 u32 mip_head;
40
41 /**
42 * Spin lock to protect the list
43 */
44 int mip_lock;
45} mfib_signal_q_t;
46
47/**
48 * @brief The pending queue of signals to deliver to the control plane
49 */
50static mfib_signal_q_t mfib_signal_pending ;
51
52static void
53mfib_signal_list_init (void)
54{
55 dlist_elt_t *head;
56 u32 hi;
57
58 pool_get(mfib_signal_dlist_pool, head);
59 hi = head - mfib_signal_dlist_pool;
60
61 mfib_signal_pending.mip_head = hi;
62 clib_dlist_init(mfib_signal_dlist_pool, hi);
63}
64
65void
66mfib_signal_module_init (void)
67{
68 mfib_signal_list_init();
69}
70
71int
72mfib_signal_send_one (struct _unix_shared_memory_queue *q,
73 u32 context)
74{
75 u32 li, si;
76
77 /*
78 * with the lock held, pop a signal from the q.
79 */
80 while (__sync_lock_test_and_set (&mfib_signal_pending.mip_lock, 1))
81 ;
82 {
83 li = clib_dlist_remove_head(mfib_signal_dlist_pool,
84 mfib_signal_pending.mip_head);
85 }
86 mfib_signal_pending.mip_lock = 0;
87
88 if (~0 != li)
89 {
90 mfib_signal_t *mfs;
91 mfib_itf_t *mfi;
92 dlist_elt_t *elt;
93
94 elt = pool_elt_at_index(mfib_signal_dlist_pool, li);
95 si = elt->value;
96
97 mfs = pool_elt_at_index(mfib_signal_pool, si);
98 mfi = mfib_itf_get(mfs->mfs_itf);
99 mfi->mfi_si = INDEX_INVALID;
100 __sync_fetch_and_and(&mfi->mfi_flags,
101 ~MFIB_ITF_FLAG_SIGNAL_PRESENT);
102
103
104 vl_mfib_signal_send_one(q, context, mfs);
105
106 /*
107 * with the lock held, return the resoruces of the signals posted
108 */
109 while (__sync_lock_test_and_set(&mfib_signal_pending.mip_lock, 1))
110 ;
111 {
112 pool_put_index(mfib_signal_pool, si);
113 pool_put_index(mfib_signal_dlist_pool, li);
114 }
115 mfib_signal_pending.mip_lock = 0;
116
117 return (1);
118 }
119 return (0);
120}
121
122void
123mfib_signal_push (const mfib_entry_t *mfe,
124 mfib_itf_t *mfi,
125 vlib_buffer_t *b0)
126{
127 mfib_signal_t *mfs;
128 dlist_elt_t *elt;
129 u32 si, li;
130
131 while (__sync_lock_test_and_set (&mfib_signal_pending.mip_lock, 1))
132 ;
133 {
134 pool_get(mfib_signal_pool, mfs);
135 pool_get(mfib_signal_dlist_pool, elt);
136
137 si = mfs - mfib_signal_pool;
138 li = elt - mfib_signal_dlist_pool;
139
140 elt->value = si;
141 mfi->mfi_si = li;
142
143 clib_dlist_addhead(mfib_signal_dlist_pool,
144 mfib_signal_pending.mip_head,
145 li);
146 }
147 mfib_signal_pending.mip_lock = 0;
148
149 mfs->mfs_entry = mfib_entry_get_index(mfe);
150 mfs->mfs_itf = mfib_itf_get_index(mfi);
151
152 if (NULL != b0)
153 {
154 mfs->mfs_buffer_len = b0->current_length;
155 memcpy(mfs->mfs_buffer,
156 vlib_buffer_get_current(b0),
157 (mfs->mfs_buffer_len > MFIB_SIGNAL_BUFFER_SIZE ?
158 MFIB_SIGNAL_BUFFER_SIZE :
159 mfs->mfs_buffer_len));
160 }
161 else
162 {
163 mfs->mfs_buffer_len = 0;
164 }
165}
166
167void
168mfib_signal_remove_itf (const mfib_itf_t *mfi)
169{
170 u32 li;
171
172 /*
173 * lock the queue to prevent further additions while we fiddle.
174 */
175 li = mfi->mfi_si;
176
177 if (INDEX_INVALID != li)
178 {
179 /*
180 * it's in the pending q
181 */
182 while (__sync_lock_test_and_set (&mfib_signal_pending.mip_lock, 1))
183 ;
184 {
185 dlist_elt_t *elt;
186
187 /*
188 * with the lock held;
189 * - remove the signal from the pending list
190 * - free up the signal and list entry obejcts
191 */
192 clib_dlist_remove(mfib_signal_dlist_pool, li);
193
194 elt = pool_elt_at_index(mfib_signal_dlist_pool, li);
195 pool_put_index(mfib_signal_pool, elt->value);
196 pool_put(mfib_signal_dlist_pool, elt);
197 }
198
199 mfib_signal_pending.mip_lock = 0;
200 }
201}