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

lfn serial ticket/834-toolchain-update topic/msim-upgrade topic/simplify-dev-export
Last change on this file since 1d24ad3 was 02a09ed, checked in by Martin Decky <martin@…>, 12 years ago

add basic infrastructure for IPv6 (inactive)
make inet_addr_t a universal address type

  • Property mode set to 100644
File size: 9.4 KB
RevLine 
[06295a9]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
[1493811]29/** @addtogroup ethip
[06295a9]30 * @{
31 */
32/**
33 * @file
34 * @brief
35 */
36
[bc38578]37#include <adt/list.h>
[06295a9]38#include <async.h>
[3e6a98c5]39#include <stdbool.h>
[06295a9]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
48#include "ethip.h"
49#include "ethip_nic.h"
[56792a2]50#include "pdu.h"
[06295a9]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) {
[a1a101d]70 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed resolving category 'nic'.");
[06295a9]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) {
[a1a101d]77 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting list of IP links.");
[06295a9]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, nic_link) {
86 ethip_nic_t *nic = list_get_instance(nic_link,
87 ethip_nic_t, nic_list);
88 if (nic->svc_id == svcs[i]) {
89 already_known = true;
90 break;
91 }
92 }
93
94 if (!already_known) {
[a1a101d]95 log_msg(LOG_DEFAULT, LVL_DEBUG, "Found NIC '%lu'",
[06295a9]96 (unsigned long) svcs[i]);
97 rc = ethip_nic_open(svcs[i]);
98 if (rc != EOK)
[a1a101d]99 log_msg(LOG_DEFAULT, LVL_ERROR, "Could not open NIC.");
[06295a9]100 }
101 }
102
103 fibril_mutex_unlock(&ethip_discovery_lock);
104 return EOK;
105}
106
107static ethip_nic_t *ethip_nic_new(void)
108{
109 ethip_nic_t *nic = calloc(1, sizeof(ethip_nic_t));
110
111 if (nic == NULL) {
[a1a101d]112 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC structure. "
[06295a9]113 "Out of memory.");
114 return NULL;
115 }
116
117 link_initialize(&nic->nic_list);
[962f03b]118 list_initialize(&nic->addr_list);
[06295a9]119
120 return nic;
121}
122
[02a09ed]123static ethip_link_addr_t *ethip_nic_addr_new(inet_addr_t *addr)
[962f03b]124{
125 ethip_link_addr_t *laddr = calloc(1, sizeof(ethip_link_addr_t));
126 if (laddr == NULL) {
[a1a101d]127 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed allocating NIC address structure. "
[962f03b]128 "Out of memory.");
129 return NULL;
130 }
[a2e3ee6]131
[962f03b]132 link_initialize(&laddr->addr_list);
[02a09ed]133 laddr->addr = *addr;
[a2e3ee6]134
[962f03b]135 return laddr;
136}
137
[06295a9]138static void ethip_nic_delete(ethip_nic_t *nic)
139{
140 if (nic->svc_name != NULL)
141 free(nic->svc_name);
142 free(nic);
143}
144
[962f03b]145static void ethip_link_addr_delete(ethip_link_addr_t *laddr)
146{
147 free(laddr);
148}
149
[06295a9]150static int ethip_nic_open(service_id_t sid)
151{
152 bool in_list = false;
[56792a2]153 nic_address_t nic_address;
[1038a9c]154
[a1a101d]155 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_open()");
[1038a9c]156 ethip_nic_t *nic = ethip_nic_new();
[06295a9]157 if (nic == NULL)
158 return ENOMEM;
[1038a9c]159
160 int rc = loc_service_get_name(sid, &nic->svc_name);
[06295a9]161 if (rc != EOK) {
[a1a101d]162 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed getting service name.");
[06295a9]163 goto error;
164 }
[1038a9c]165
[06295a9]166 nic->sess = loc_service_connect(EXCHANGE_SERIALIZE, sid, 0);
167 if (nic->sess == NULL) {
[a1a101d]168 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed connecting '%s'", nic->svc_name);
[06295a9]169 goto error;
170 }
[1038a9c]171
[06295a9]172 nic->svc_id = sid;
[1038a9c]173
[06295a9]174 rc = nic_callback_create(nic->sess, ethip_nic_cb_conn, nic);
175 if (rc != EOK) {
[a1a101d]176 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed creating callback connection "
[06295a9]177 "from '%s'", nic->svc_name);
178 goto error;
179 }
180
[a1a101d]181 log_msg(LOG_DEFAULT, LVL_DEBUG, "Opened NIC '%s'", nic->svc_name);
[06295a9]182 list_append(&nic->nic_list, &ethip_nic_list);
183 in_list = true;
184
185 rc = ethip_iplink_init(nic);
186 if (rc != EOK)
187 goto error;
188
[56792a2]189 rc = nic_get_address(nic->sess, &nic_address);
190 if (rc != EOK) {
[a1a101d]191 log_msg(LOG_DEFAULT, LVL_ERROR, "Error getting MAC address of NIC '%s'.",
[56792a2]192 nic->svc_name);
193 goto error;
194 }
[02a09ed]195
196 addr48(nic_address.address, nic->mac_addr);
[56792a2]197
[4f64a523]198 rc = nic_set_state(nic->sess, NIC_STATE_ACTIVE);
199 if (rc != EOK) {
[a1a101d]200 log_msg(LOG_DEFAULT, LVL_ERROR, "Error activating NIC '%s'.",
[4f64a523]201 nic->svc_name);
202 goto error;
203 }
204
[02a09ed]205 log_msg(LOG_DEFAULT, LVL_DEBUG, "Initialized IP link service,");
[06295a9]206
207 return EOK;
208
209error:
210 if (in_list)
211 list_remove(&nic->nic_list);
212 if (nic->sess != NULL)
213 async_hangup(nic->sess);
214 ethip_nic_delete(nic);
215 return rc;
216}
217
218static void ethip_nic_cat_change_cb(void)
219{
220 (void) ethip_nic_check_new();
221}
222
223static void ethip_nic_addr_changed(ethip_nic_t *nic, ipc_callid_t callid,
224 ipc_call_t *call)
225{
[a1a101d]226 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_changed()");
[06295a9]227 async_answer_0(callid, ENOTSUP);
228}
229
230static void ethip_nic_received(ethip_nic_t *nic, ipc_callid_t callid,
231 ipc_call_t *call)
232{
[1493811]233 int rc;
234 void *data;
235 size_t size;
236
[a1a101d]237 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() nic=%p", nic);
[1493811]238
239 rc = async_data_write_accept(&data, false, 0, 0, 0, &size);
240 if (rc != EOK) {
[a1a101d]241 log_msg(LOG_DEFAULT, LVL_DEBUG, "data_write_accept() failed");
[1493811]242 return;
243 }
244
[a1a101d]245 log_msg(LOG_DEFAULT, LVL_DEBUG, "Ethernet PDU contents (%zu bytes)",
[df15e5f]246 size);
247
[a1a101d]248 log_msg(LOG_DEFAULT, LVL_DEBUG, "call ethip_received");
[1493811]249 rc = ethip_received(&nic->iplink, data, size);
[a1a101d]250 log_msg(LOG_DEFAULT, LVL_DEBUG, "free data");
[1493811]251 free(data);
252
[a1a101d]253 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_received() done, rc=%d", rc);
[1493811]254 async_answer_0(callid, rc);
[06295a9]255}
256
257static void ethip_nic_device_state(ethip_nic_t *nic, ipc_callid_t callid,
258 ipc_call_t *call)
259{
[a1a101d]260 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_device_state()");
[06295a9]261 async_answer_0(callid, ENOTSUP);
262}
263
264static void ethip_nic_cb_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
265{
[4f64a523]266 ethip_nic_t *nic = (ethip_nic_t *)arg;
[06295a9]267
[a1a101d]268 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethnip_nic_cb_conn()");
[06295a9]269
270 while (true) {
271 ipc_call_t call;
272 ipc_callid_t callid = async_get_call(&call);
273
274 if (!IPC_GET_IMETHOD(call)) {
275 /* TODO: Handle hangup */
276 return;
277 }
278
279 switch (IPC_GET_IMETHOD(call)) {
280 case NIC_EV_ADDR_CHANGED:
281 ethip_nic_addr_changed(nic, callid, &call);
282 break;
283 case NIC_EV_RECEIVED:
284 ethip_nic_received(nic, callid, &call);
285 break;
286 case NIC_EV_DEVICE_STATE:
287 ethip_nic_device_state(nic, callid, &call);
288 break;
289 default:
290 async_answer_0(callid, ENOTSUP);
291 }
292 }
293}
294
295int ethip_nic_discovery_start(void)
296{
[1038a9c]297 int rc = loc_register_cat_change_cb(ethip_nic_cat_change_cb);
[06295a9]298 if (rc != EOK) {
[a1a101d]299 log_msg(LOG_DEFAULT, LVL_ERROR, "Failed registering callback for NIC "
[06295a9]300 "discovery (%d).", rc);
301 return rc;
302 }
[1038a9c]303
[06295a9]304 return ethip_nic_check_new();
305}
306
[bc38578]307ethip_nic_t *ethip_nic_find_by_iplink_sid(service_id_t iplink_sid)
308{
[a1a101d]309 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid(%u)",
[bc38578]310 (unsigned) iplink_sid);
311
312 list_foreach(ethip_nic_list, link) {
[a1a101d]313 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - element");
[bc38578]314 ethip_nic_t *nic = list_get_instance(link, ethip_nic_t,
315 nic_list);
316
317 if (nic->iplink_sid == iplink_sid) {
[a1a101d]318 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - found %p", nic);
[bc38578]319 return nic;
320 }
321 }
322
[a1a101d]323 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_find_by_iplink_sid - not found");
[bc38578]324 return NULL;
325}
326
[1493811]327int ethip_nic_send(ethip_nic_t *nic, void *data, size_t size)
328{
[fe4310f]329 int rc;
[a1a101d]330 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_send(size=%zu)", size);
[fe4310f]331 rc = nic_send_frame(nic->sess, data, size);
[a1a101d]332 log_msg(LOG_DEFAULT, LVL_DEBUG, "nic_send_frame -> %d", rc);
[fe4310f]333 return rc;
[1493811]334}
335
[02a09ed]336int ethip_nic_addr_add(ethip_nic_t *nic, inet_addr_t *addr)
[962f03b]337{
[a1a101d]338 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_add()");
[a2e3ee6]339
340 ethip_link_addr_t *laddr = ethip_nic_addr_new(addr);
[962f03b]341 if (laddr == NULL)
342 return ENOMEM;
[a2e3ee6]343
[962f03b]344 list_append(&laddr->addr_list, &nic->addr_list);
345 return EOK;
346}
347
[02a09ed]348int ethip_nic_addr_remove(ethip_nic_t *nic, inet_addr_t *addr)
[962f03b]349{
[a1a101d]350 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_remove()");
[a2e3ee6]351
352 ethip_link_addr_t *laddr = ethip_nic_addr_find(nic, addr);
[962f03b]353 if (laddr == NULL)
354 return ENOENT;
[a2e3ee6]355
[962f03b]356 list_remove(&laddr->addr_list);
357 ethip_link_addr_delete(laddr);
358 return EOK;
359}
360
361ethip_link_addr_t *ethip_nic_addr_find(ethip_nic_t *nic,
[02a09ed]362 inet_addr_t *addr)
[962f03b]363{
[a1a101d]364 log_msg(LOG_DEFAULT, LVL_DEBUG, "ethip_nic_addr_find()");
[257feec]365
[962f03b]366 list_foreach(nic->addr_list, link) {
367 ethip_link_addr_t *laddr = list_get_instance(link,
368 ethip_link_addr_t, addr_list);
[257feec]369
[02a09ed]370 if (inet_addr_compare(addr, &laddr->addr))
[962f03b]371 return laddr;
372 }
[257feec]373
[962f03b]374 return NULL;
375}
376
[06295a9]377/** @}
378 */
Note: See TracBrowser for help on using the repository browser.