source: mainline/uspace/srv/net/ethip/ethip_nic.c@ f1d04b2

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since f1d04b2 was 5a324d99, checked in by Jiri Svoboda <jiri@…>, 12 years ago

Remove include where not needed anymore.

  • Property mode set to 100644
File size: 10.9 KB
Line 
1/*
2 * Copyright (c) 2012 Jiri Svoboda
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/** @addtogroup ethip
30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
37#include <adt/list.h>
38#include <async.h>
39#include <stdbool.h>
40#include <errno.h>
41#include <fibril_synch.h>
42#include <inet/iplink_srv.h>
43#include <io/log.h>
44#include <loc.h>
45#include <device/nic.h>
46#include <stdlib.h>
47#include <mem.h>
48#include "ethip.h"
49#include "ethip_nic.h"
50#include "pdu.h"
51
52static int ethip_nic_open(service_id_t sid);
53static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
54
55static LIST_INITIALIZE(ethip_nic_list);
56static FIBRIL_MUTEX_INITIALIZE(ethip_discovery_lock);
57
58static int ethip_nic_check_new(void)
59{
60 bool already_known;
61 category_id_t iplink_cat;
62 service_id_t *svcs;
63 size_t count, i;
64 int rc;
65
66 fibril_mutex_lock(&ethip_discovery_lock);
67
68 rc = loc_category_get_id("nic", &iplink_cat, IPC_FLAG_BLOCKING);
69 if (rc != EOK) {
70 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'nic'.");
71 fibril_mutex_unlock(&ethip_discovery_lock);
72 return ENOENT;
73 }
74
75 rc = loc_category_get_svcs(iplink_cat, &svcs, &count);
76 if (rc != EOK) {
77 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
78 fibril_mutex_unlock(&ethip_discovery_lock);
79 return EIO;
80 }
81
82 for (i = 0; i < count; i++) {
83 already_known = false;
84
85 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
86 if (nic->svc_id == svcs[i]) {
87 already_known = true;
88 break;
89 }
90 }
91
92 if (!already_known) {
93 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found NIC '%lu'",
94 (unsigned long) svcs[i]);
95 rc = ethip_nic_open(svcs[i]);
96 if (rc != EOK)
97 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open NIC.");
98 }
99 }
100
101 fibril_mutex_unlock(&ethip_discovery_lock);
102 return EOK;
103}
104
105static ethip_nic_t *ethip_nic_new(void)
106{
107 ethip_nic_t *nic = calloc(1, sizeof(ethip_nic_t));
108 if (nic == NULL) {
109 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC structure. "
110 "Out of memory.");
111 return NULL;
112 }
113
114 link_initialize(&nic->link);
115 list_initialize(&nic->addr_list);
116
117 return nic;
118}
119
120static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
121{
122 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
123 if (laddr == NULL) {
124 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC address structure. "
125 "Out of memory.");
126 return NULL;
127 }
128
129 link_initialize(&laddr->link);
130 laddr->addr = *addr;
131
132 return laddr;
133}
134
135static void ethip_nic_delete(ethip_nic_t *nic)
136{
137 if (nic->svc_name != NULL)
138 free(nic->svc_name);
139
140 free(nic);
141}
142
143static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
144{
145 free(laddr);
146}
147
148static int ethip_nic_open(service_id_t sid)
149{
150 bool in_list = false;
151 nic_address_t nic_address;
152
153 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_open()");
154 ethip_nic_t *nic = ethip_nic_new();
155 if (nic == NULL)
156 return ENOMEM;
157
158 int rc = loc_service_get_name(sid, &nic->svc_name);
159 if (rc != EOK) {
160 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
161 goto error;
162 }
163
164 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
165 if (nic->sess == NULL) {
166 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
167 goto error;
168 }
169
170 nic->svc_id = sid;
171
172 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
173 if (rc != EOK) {
174 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating callback connection "
175 "from '%s'", nic->svc_name);
176 goto error;
177 }
178
179 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
180 list_append(&nic->link, &ethip_nic_list);
181 in_list = true;
182
183 rc = ethip_iplink_init(nic);
184 if (rc != EOK)
185 goto error;
186
187 rc = nic_get_address(nic->sess, &nic_address);
188 if (rc != EOK) {
189 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting MAC address of NIC '%s'.",
190 nic->svc_name);
191 goto error;
192 }
193
194 addr48(nic_address.address, nic->mac_addr);
195
196 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
197 if (rc != EOK) {
198 log_msg(LOG_DEFAULT, LVL_ERROR, "Error activating NIC '%s'.",
199 nic->svc_name);
200 goto error;
201 }
202
203 rc = nic_broadcast_set_mode(nic->sess, NIC_BROADCAST_ACCEPTED);
204 if (rc != EOK) {
205 log_msg(LOG_DEFAULT, LVL_ERROR, "Error enabling "
206 "reception of broadcast frames on '%s'.", nic->svc_name);
207 goto error;
208 }
209
210 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
211
212 return EOK;
213
214error:
215 if (in_list)
216 list_remove(&nic->link);
217
218 if (nic->sess != NULL)
219 async_hangup(nic->sess);
220
221 ethip_nic_delete(nic);
222 return rc;
223}
224
225static void ethip_nic_cat_change_cb(void)
226{
227 (void) ethip_nic_check_new();
228}
229
230static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
231 ipc_call_t *call)
232{
233 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed()");
234 async_answer_0(callid, ENOTSUP);
235}
236
237static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
238 ipc_call_t *call)
239{
240 int rc;
241 void *data;
242 size_t size;
243
244 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
245
246 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
247 if (rc != EOK) {
248 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
249 return;
250 }
251
252 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
253 size);
254
255 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
256 rc = ethip_received(&nic->iplink, data, size);
257 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
258 free(data);
259
260 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
261 async_answer_0(callid, rc);
262}
263
264static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
265 ipc_call_t *call)
266{
267 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
268 async_answer_0(callid, ENOTSUP);
269}
270
271static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
272{
273 ethip_nic_t *nic = (ethip_nic_t *)arg;
274
275 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
276
277 while (true) {
278 ipc_call_t call;
279 ipc_callid_t callid = async_get_call(&call);
280
281 if (!IPC_GET_IMETHOD(call)) {
282 /* TODO: Handle hangup */
283 return;
284 }
285
286 switch (IPC_GET_IMETHOD(call)) {
287 case NIC_EV_ADDR_CHANGED:
288 ethip_nic_addr_changed(nic, callid, &call);
289 break;
290 case NIC_EV_RECEIVED:
291 ethip_nic_received(nic, callid, &call);
292 break;
293 case NIC_EV_DEVICE_STATE:
294 ethip_nic_device_state(nic, callid, &call);
295 break;
296 default:
297 async_answer_0(callid, ENOTSUP);
298 }
299 }
300}
301
302int ethip_nic_discovery_start(void)
303{
304 int rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
305 if (rc != EOK) {
306 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
307 "discovery (%d).", rc);
308 return rc;
309 }
310
311 return ethip_nic_check_new();
312}
313
314ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
315{
316 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
317 (unsigned) iplink_sid);
318
319 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
320 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
321 if (nic->iplink_sid == iplink_sid) {
322 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
323 return nic;
324 }
325 }
326
327 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
328 return NULL;
329}
330
331int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
332{
333 int rc;
334 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
335 rc = nic_send_frame(nic->sess, data, size);
336 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %d", rc);
337 return rc;
338}
339
340/** Setup accepted multicast addresses
341 *
342 * Currently the set of accepted multicast addresses is
343 * determined only based on IPv6 addresses.
344 *
345 */
346static int ethip_nic_setup_multicast(ethip_nic_t *nic)
347{
348 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
349
350 /* Count the number of multicast addresses */
351
352 size_t count = 0;
353
354 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
355 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
356 if (ver == ip_v6)
357 count++;
358 }
359
360 if (count == 0)
361 return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
362 NULL, 0);
363
364 nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
365 if (mac_list == NULL)
366 return ENOMEM;
367
368 /* Create the multicast MAC list */
369
370 size_t i = 0;
371
372 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
373 addr128_t v6;
374 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
375 if (ver != ip_v6)
376 continue;
377
378 assert(i < count);
379
380 addr48_t mac;
381 addr48_solicited_node(v6, mac);
382
383 /* Avoid duplicate addresses in the list */
384
385 bool found = false;
386
387 for (size_t j = 0; j < i; j++) {
388 if (addr48_compare(mac_list[j].address, mac)) {
389 found = true;
390 break;
391 }
392 }
393
394 if (!found) {
395 addr48(mac, mac_list[i].address);
396 i++;
397 } else
398 count--;
399 }
400
401 /* Setup the multicast MAC list */
402
403 int rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
404 mac_list, count);
405
406 free(mac_list);
407 return rc;
408}
409
410int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
411{
412 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
413
414 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
415 if (laddr == NULL)
416 return ENOMEM;
417
418 list_append(&laddr->link, &nic->addr_list);
419
420 return ethip_nic_setup_multicast(nic);
421}
422
423int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
424{
425 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
426
427 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
428 if (laddr == NULL)
429 return ENOENT;
430
431 list_remove(&laddr->link);
432 ethip_link_addr_delete(laddr);
433
434 return ethip_nic_setup_multicast(nic);
435}
436
437ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
438 inet_addr_t *addr)
439{
440 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
441
442 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
443 if (inet_addr_compare(addr, &laddr->addr))
444 return laddr;
445 }
446
447 return NULL;
448}
449
450/** @}
451 */
Note: See TracBrowser for help on using the repository browser.