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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 8ebc5b8a was f9b2cb4c, checked in by Martin Decky <martin@…>, 10 years ago

unify interface API

  • introduce new interfaces
  • unify location service clients to always expect service ID as the second argument
  • remove obsolete methods that take explicit exchange management arguments (first phase)
  • use interfaces in device drivers, devman, location service, logger, inet
  • Property mode set to 100644
File size: 11.5 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 <nic_iface.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(sid, INTERFACE_DDF, 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 uint8_t *addr;
234 size_t size;
235 int rc;
236
237 rc = async_data_write_accept((void **)&addr, false, 0, 0, 0, &size);
238 if (rc != EOK) {
239 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
240 return;
241 }
242
243 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed(): "
244 "new addr=%02x:%02x:%02x:%02x:%02x:%02x",
245 addr[0], addr[1], addr[2], addr[3], addr[4], addr[5]);
246
247 memcpy(&nic->mac_addr, addr, sizeof(nic->mac_addr));
248
249 rc = iplink_ev_change_addr(&nic->iplink, &nic->mac_addr);
250 if (rc != EOK) {
251 log_msg(LOG_DEFAULT, LVL_DEBUG, "iplink_ev_change_addr() failed");
252 return;
253 }
254
255 free(addr);
256 async_answer_0(callid, EOK);
257}
258
259static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
260 ipc_call_t *call)
261{
262 int rc;
263 void *data;
264 size_t size;
265
266 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
267
268 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
269 if (rc != EOK) {
270 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
271 return;
272 }
273
274 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
275 size);
276
277 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
278 rc = ethip_received(&nic->iplink, data, size);
279 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
280 free(data);
281
282 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
283 async_answer_0(callid, rc);
284}
285
286static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
287 ipc_call_t *call)
288{
289 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
290 async_answer_0(callid, ENOTSUP);
291}
292
293static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
294{
295 ethip_nic_t *nic = (ethip_nic_t *)arg;
296
297 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
298
299 while (true) {
300 ipc_call_t call;
301 ipc_callid_t callid = async_get_call(&call);
302
303 if (!IPC_GET_IMETHOD(call)) {
304 /* TODO: Handle hangup */
305 return;
306 }
307
308 switch (IPC_GET_IMETHOD(call)) {
309 case NIC_EV_ADDR_CHANGED:
310 ethip_nic_addr_changed(nic, callid, &call);
311 break;
312 case NIC_EV_RECEIVED:
313 ethip_nic_received(nic, callid, &call);
314 break;
315 case NIC_EV_DEVICE_STATE:
316 ethip_nic_device_state(nic, callid, &call);
317 break;
318 default:
319 log_msg(LOG_DEFAULT, LVL_DEBUG, "unknown IPC method: %" PRIun, IPC_GET_IMETHOD(call));
320 async_answer_0(callid, ENOTSUP);
321 }
322 }
323}
324
325int ethip_nic_discovery_start(void)
326{
327 int rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
328 if (rc != EOK) {
329 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
330 "discovery (%d).", rc);
331 return rc;
332 }
333
334 return ethip_nic_check_new();
335}
336
337ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
338{
339 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
340 (unsigned) iplink_sid);
341
342 list_foreach(ethip_nic_list, link, ethip_nic_t, nic) {
343 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
344 if (nic->iplink_sid == iplink_sid) {
345 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
346 return nic;
347 }
348 }
349
350 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
351 return NULL;
352}
353
354int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
355{
356 int rc;
357 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
358 rc = nic_send_frame(nic->sess, data, size);
359 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %d", rc);
360 return rc;
361}
362
363/** Setup accepted multicast addresses
364 *
365 * Currently the set of accepted multicast addresses is
366 * determined only based on IPv6 addresses.
367 *
368 */
369static int ethip_nic_setup_multicast(ethip_nic_t *nic)
370{
371 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_setup_multicast()");
372
373 /* Count the number of multicast addresses */
374
375 size_t count = 0;
376
377 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
378 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, NULL);
379 if (ver == ip_v6)
380 count++;
381 }
382
383 if (count == 0)
384 return nic_multicast_set_mode(nic->sess, NIC_MULTICAST_BLOCKED,
385 NULL, 0);
386
387 nic_address_t *mac_list = calloc(count, sizeof(nic_address_t));
388 if (mac_list == NULL)
389 return ENOMEM;
390
391 /* Create the multicast MAC list */
392
393 size_t i = 0;
394
395 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
396 addr128_t v6;
397 ip_ver_t ver = inet_addr_get(&laddr->addr, NULL, &v6);
398 if (ver != ip_v6)
399 continue;
400
401 assert(i < count);
402
403 addr48_t mac;
404 addr48_solicited_node(v6, mac);
405
406 /* Avoid duplicate addresses in the list */
407
408 bool found = false;
409
410 for (size_t j = 0; j < i; j++) {
411 if (addr48_compare(mac_list[j].address, mac)) {
412 found = true;
413 break;
414 }
415 }
416
417 if (!found) {
418 addr48(mac, mac_list[i].address);
419 i++;
420 } else
421 count--;
422 }
423
424 /* Setup the multicast MAC list */
425
426 int rc = nic_multicast_set_mode(nic->sess, NIC_MULTICAST_LIST,
427 mac_list, count);
428
429 free(mac_list);
430 return rc;
431}
432
433int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
434{
435 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
436
437 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
438 if (laddr == NULL)
439 return ENOMEM;
440
441 list_append(&laddr->link, &nic->addr_list);
442
443 return ethip_nic_setup_multicast(nic);
444}
445
446int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
447{
448 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
449
450 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
451 if (laddr == NULL)
452 return ENOENT;
453
454 list_remove(&laddr->link);
455 ethip_link_addr_delete(laddr);
456
457 return ethip_nic_setup_multicast(nic);
458}
459
460ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
461 inet_addr_t *addr)
462{
463 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
464
465 list_foreach(nic->addr_list, link, ethip_link_addr_t, laddr) {
466 if (inet_addr_compare(addr, &laddr->addr))
467 return laddr;
468 }
469
470 return NULL;
471}
472
473/** @}
474 */
Note: See TracBrowser for help on using the repository browser.