source: mainline/uspace/lib/nic/src/nic_wol_virtues.c@ 0ca7286

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 0ca7286 was 0ca7286, checked in by Adam Hraska <adam.hraska+hos@…>, 13 years ago

Added resizing to user space (single-threaded) hash_table. Resizes in a way to mitigate effects of bad hash functions. Change of interface affected many files.

  • Property mode set to 100644
File size: 7.9 KB
Line 
1/*
2 * Copyright (c) 2011 Radim Vansa
3 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 *
9 * - Redistributions of source code must retain the above copyright
10 * notice, this list of conditions and the following disclaimer.
11 * - Redistributions in binary form must reproduce the above copyright
12 * notice, this list of conditions and the following disclaimer in the
13 * documentation and/or other materials provided with the distribution.
14 * - The name of the author may not be used to endorse or promote products
15 * derived from this software without specific prior written permission.
16 *
17 * THIS SOFTWARE IS PROVIDED BY THE AUTHOR ``AS IS'' AND ANY EXPRESS OR
18 * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES
19 * OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED.
20 * IN NO EVENT SHALL THE AUTHOR BE LIABLE FOR ANY DIRECT, INDIRECT,
21 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT
22 * NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
23 * DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
24 * THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
25 * (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF
26 * THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
27 */
28
29/**
30 * @addtogroup libnic
31 * @{
32 */
33/**
34 * @file
35 * @brief Wake-on-LAN support
36 */
37
38#include "nic_wol_virtues.h"
39#include <assert.h>
40#include <errno.h>
41
42
43/*
44 * Hash table helper functions
45 */
46
47static size_t nic_wv_key_hash(unsigned long keys[])
48{
49 return keys[0];
50}
51
52static size_t nic_wv_hash(const link_t *item)
53{
54 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
55 unsigned long key = virtue->id;
56 return nic_wv_key_hash(&key);
57}
58
59static bool nic_wv_match(unsigned long key[], size_t keys, const link_t *item)
60{
61 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *) item;
62 return (virtue->id == (nic_wv_id_t) key[0]);
63}
64
65/**
66 * Initializes the WOL virtues structure
67 *
68 * @param wvs
69 *
70 * @return EOK On success
71 * @return ENOMEM On not enough memory
72 */
73int nic_wol_virtues_init(nic_wol_virtues_t *wvs)
74{
75 bzero(wvs, sizeof(nic_wol_virtues_t));
76 wvs->table_operations.hash = nic_wv_hash;
77 wvs->table_operations.key_hash = nic_wv_key_hash;
78 wvs->table_operations.match = nic_wv_match;
79 wvs->table_operations.equal = 0;
80 wvs->table_operations.remove_callback = 0;
81
82 if (!hash_table_create(&wvs->table, 0, 1, &wvs->table_operations)) {
83 return ENOMEM;
84 }
85 size_t i;
86 for (i = 0; i < NIC_WV_MAX; ++i) {
87 wvs->caps_max[i] = -1;
88 }
89 wvs->next_id = 0;
90 return EOK;
91}
92
93/**
94 * Reinitializes the structure, destroying all virtues. The next_id is not
95 * changed (some apps could still hold the filter IDs).
96 *
97 * @param wvs
98 */
99void nic_wol_virtues_clear(nic_wol_virtues_t *wvs)
100{
101 hash_table_clear(&wvs->table);
102 nic_wv_type_t type;
103 for (type = NIC_WV_NONE; type < NIC_WV_MAX; ++type) {
104 nic_wol_virtue_t *virtue = wvs->lists[type];
105 while (virtue != NULL) {
106 nic_wol_virtue_t *next = virtue->next;
107 free(virtue->data);
108 free(virtue);
109 virtue = next;
110 }
111 wvs->lists_sizes[type] = 0;
112 }
113}
114
115/**
116 * Verifies that the arguments for the WOL virtues are correct.
117 *
118 * @param type Type of the virtue
119 * @param data Data argument for the virtue
120 * @param length Length of the data
121 *
122 * @return EOK The arguments are correct
123 * @return EINVAL The arguments are incorrect
124 * @return ENOTSUP This type is unknown
125 */
126int nic_wol_virtues_verify(nic_wv_type_t type, const void *data, size_t length)
127{
128 switch (type) {
129 case NIC_WV_ARP_REQUEST:
130 case NIC_WV_BROADCAST:
131 case NIC_WV_LINK_CHANGE:
132 return EOK;
133 case NIC_WV_DESTINATION:
134 return length == sizeof (nic_address_t) ? EOK : EINVAL;
135 case NIC_WV_DIRECTED_IPV4:
136 return length == sizeof (nic_wv_ipv4_data_t) ? EOK : EINVAL;
137 case NIC_WV_DIRECTED_IPV6:
138 return length == sizeof (nic_wv_ipv6_data_t) ? EOK : EINVAL;
139 case NIC_WV_FULL_MATCH:
140 return length % 2 == 0 ? EOK : EINVAL;
141 case NIC_WV_MAGIC_PACKET:
142 return data == NULL || length == sizeof (nic_wv_magic_packet_data_t) ?
143 EOK : EINVAL;
144 default:
145 return ENOTSUP;
146 }
147}
148
149/**
150 * Adds the virtue to the list of known virtues, activating it.
151 *
152 * @param wvs
153 * @param virtue The virtue structure
154 *
155 * @return EOK On success
156 * @return ENOTSUP If the virtue type is not supported
157 * @return EINVAL If the virtue type is a single-filter and there's already
158 * a virtue of this type defined, or there is something wrong
159 * with the data
160 * @return ENOMEM Not enough memory to activate the virtue
161 */
162int nic_wol_virtues_add(nic_wol_virtues_t *wvs, nic_wol_virtue_t *virtue)
163{
164 if (!nic_wv_is_multi(virtue->type) &&
165 wvs->lists[virtue->type] != NULL) {
166 return EINVAL;
167 }
168 do {
169 virtue->id = wvs->next_id++;
170 } while (NULL !=
171 hash_table_find(&wvs->table, (unsigned long *) &virtue->id));
172 hash_table_insert(&wvs->table, &virtue->item);
173 virtue->next = wvs->lists[virtue->type];
174 wvs->lists[virtue->type] = virtue;
175 wvs->lists_sizes[virtue->type]++;
176 return EOK;
177}
178
179/**
180 * Removes the virtue from the list of virtues, but NOT deallocating the
181 * nic_wol_virtue structure.
182 *
183 * @param wvs
184 * @param id Identifier of the removed virtue
185 *
186 * @return Removed virtue structure or NULL if not found.
187 */
188nic_wol_virtue_t *nic_wol_virtues_remove(nic_wol_virtues_t *wvs, nic_wv_id_t id)
189{
190 nic_wol_virtue_t *virtue = (nic_wol_virtue_t *)
191 hash_table_find(&wvs->table, (unsigned long *) &id);
192 if (virtue == NULL) {
193 return NULL;
194 }
195
196 /* Remove from filter_table */
197 hash_table_remove(&wvs->table, (unsigned long *) &id, 1);
198
199 /* Remove from filter_types */
200 assert(wvs->lists[virtue->type] != NULL);
201 if (wvs->lists[virtue->type] == virtue) {
202 wvs->lists[virtue->type] = virtue->next;
203 } else {
204 nic_wol_virtue_t *wv = wvs->lists[virtue->type];
205 while (wv->next != virtue) {
206 wv = wv->next;
207 assert(wv != NULL);
208 }
209 wv->next = virtue->next;
210 }
211 wvs->lists_sizes[virtue->type]--;
212
213 virtue->next = NULL;
214 return virtue;
215}
216
217
218/**
219 * Searches the filters table for a filter with specified ID
220 *
221 * @param wvs
222 * @param id Identifier of the searched virtue
223 *
224 * @return Requested filter or NULL if not found.
225 */
226const nic_wol_virtue_t *nic_wol_virtues_find(const nic_wol_virtues_t *wvs,
227 nic_wv_id_t id)
228{
229 /*
230 * The hash_table_find cannot be const, because it would require the
231 * returned link to be const as well. But in this case, when we're returning
232 * constant virtue the retyping is correct.
233 */
234 link_t *virtue = hash_table_find(
235 &((nic_wol_virtues_t *) wvs)->table, (unsigned long *) &id);
236 return (const nic_wol_virtue_t *) virtue;
237}
238
239/**
240 * Fill identifiers of current wol virtues of the specified type into the list.
241 * If the type is set to NIC_WV_NONE, all virtues are used.
242 *
243 * @param wvs
244 * @param[in] type Type of the virtues or NIC_WV_NONE
245 * @param[out] id_list The new vector of filter IDs. Can be NULL.
246 * @param[out] count Number of IDs in the filter_list. Can be NULL.
247 *
248 * @return EOK If it completes successfully
249 * @return EINVAL If the filter type is invalid
250 */
251int nic_wol_virtues_list(const nic_wol_virtues_t *wvs, nic_wv_type_t type,
252 size_t max_count, nic_wv_id_t *id_list, size_t *id_count)
253{
254 size_t count = 0;
255 if (type == NIC_WV_NONE) {
256 size_t i;
257 for (i = NIC_WV_NONE; i < NIC_WV_MAX; ++i) {
258 if (id_list != NULL) {
259 nic_wol_virtue_t *virtue = wvs->lists[i];
260 while (virtue != NULL) {
261 if (count < max_count) {
262 id_list[count] = virtue->id;
263 }
264 ++count;
265 virtue = virtue->next;
266 }
267 } else {
268 count += wvs->lists_sizes[i];
269 }
270 }
271 } else if (type >= NIC_WV_MAX) {
272 return EINVAL;
273 } else {
274 if (id_list != NULL) {
275 nic_wol_virtue_t *virtue = wvs->lists[type];
276 while (virtue != NULL) {
277 if (count < max_count) {
278 id_list[count] = virtue->id;
279 }
280 ++count;
281 virtue = virtue->next;
282 }
283 } else {
284 count = wvs->lists_sizes[type];
285 }
286 }
287 if (id_count != NULL) {
288 *id_count = count;
289 }
290 return EOK;
291}
292
293/** @}
294 */
Note: See TracBrowser for help on using the repository browser.