source: mainline/uspace/srv/ethip/ethip.c@ 1493811

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

Ethernet PDU encoding and decoding.

  • Property mode set to 100644
File size: 5.3 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 IP link provider for Ethernet
35 *
36 * Based on the IETF RFC 894 standard.
37 */
38
39#include <async.h>
40#include <errno.h>
41#include <inet/iplink_srv.h>
42#include <io/log.h>
43#include <loc.h>
44#include <stdio.h>
45#include <stdlib.h>
46
47#include "ethip.h"
48#include "ethip_nic.h"
49#include "pdu.h"
50#include "std.h"
51
52#define NAME "eth"
53
54static int ethip_open(iplink_conn_t *conn);
55static int ethip_close(iplink_conn_t *conn);
56static int ethip_send(iplink_conn_t *conn, iplink_srv_sdu_t *sdu);
57static int ethip_get_mtu(iplink_conn_t *conn, size_t *mtu);
58
59static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg);
60
61static iplink_ops_t ethip_iplink_ops = {
62 .open = ethip_open,
63 .close = ethip_close,
64 .send = ethip_send,
65 .get_mtu = ethip_get_mtu
66};
67
68static int ethip_init(void)
69{
70 int rc;
71
72 async_set_client_connection(ethip_client_conn);
73
74 rc = loc_server_register(NAME);
75 if (rc != EOK) {
76 log_msg(LVL_ERROR, "Failed registering server.");
77 return rc;
78 }
79
80 rc = ethip_nic_discovery_start();
81 if (rc != EOK)
82 return rc;
83
84 return EOK;
85}
86
87int ethip_iplink_init(ethip_nic_t *nic)
88{
89 int rc;
90 service_id_t sid;
91 category_id_t iplink_cat;
92 static unsigned link_num = 0;
93 char *svc_name = NULL;
94
95 log_msg(LVL_DEBUG, "ethip_iplink_init()");
96
97 nic->iplink.ops = &ethip_iplink_ops;
98 nic->iplink.arg = nic;
99
100 rc = asprintf(&svc_name, "net/eth%u", ++link_num);
101 if (rc < 0) {
102 log_msg(LVL_ERROR, "Out of memory.");
103 goto error;
104 }
105
106 rc = loc_service_register(svc_name, &sid);
107 if (rc != EOK) {
108 log_msg(LVL_ERROR, "Failed registering service %s.", svc_name);
109 goto error;
110 }
111
112 nic->iplink_sid = sid;
113
114 rc = loc_category_get_id("iplink", &iplink_cat, IPC_FLAG_BLOCKING);
115 if (rc != EOK) {
116 log_msg(LVL_ERROR, "Failed resolving category 'iplink'.");
117 goto error;
118 }
119
120 rc = loc_service_add_to_cat(sid, iplink_cat);
121 if (rc != EOK) {
122 log_msg(LVL_ERROR, "Failed adding %s to category.", svc_name);
123 goto error;
124 }
125
126 return EOK;
127
128error:
129 if (svc_name != NULL)
130 free(svc_name);
131 return rc;
132}
133
134static void ethip_client_conn(ipc_callid_t iid, ipc_call_t *icall, void *arg)
135{
136 ethip_nic_t *nic;
137 service_id_t sid;
138
139 sid = (service_id_t)IPC_GET_ARG1(*icall);
140 log_msg(LVL_DEBUG, "ethip_client_conn(%u)", (unsigned)sid);
141 nic = ethip_nic_find_by_iplink_sid(sid);
142 if (nic == NULL) {
143 log_msg(LVL_WARN, "Uknown service ID.");
144 return;
145 }
146
147 iplink_conn(iid, icall, &nic->iplink);
148}
149
150static int ethip_open(iplink_conn_t *conn)
151{
152 log_msg(LVL_DEBUG, "ethip_open()");
153 return EOK;
154}
155
156static int ethip_close(iplink_conn_t *conn)
157{
158 log_msg(LVL_DEBUG, "ethip_open()");
159 return EOK;
160}
161
162static int ethip_send(iplink_conn_t *conn, iplink_srv_sdu_t *sdu)
163{
164 ethip_nic_t *nic = (ethip_nic_t *)conn->srv->arg;
165 eth_frame_t frame;
166 void *data;
167 size_t size;
168 int rc;
169
170 log_msg(LVL_DEBUG, "ethip_send()");
171
172 frame.dest.addr = 0xdeeedeeedeee;
173 frame.src.addr = 0xaafeedfaceee;
174 frame.etype_len = ETYPE_IP;
175 frame.data = sdu->data;
176 frame.size = sdu->size;
177
178 rc = eth_pdu_encode(&frame, &data, &size);
179 if (rc != EOK)
180 return rc;
181
182 rc = ethip_nic_send(nic, data, size);
183 free(data);
184
185 return rc;
186}
187
188int ethip_received(iplink_srv_t *srv, void *data, size_t size)
189{
190 eth_frame_t frame;
191 iplink_srv_sdu_t sdu;
192 int rc;
193
194 rc = eth_pdu_decode(data, size, &frame);
195 if (rc != EOK)
196 return rc;
197
198 sdu.data = frame.data;
199 sdu.size = frame.size;
200 (void) sdu;
201 //rc = iplink_ev_recv(conn, &sdu);
202
203 free(frame.data);
204 return rc;
205}
206
207static int ethip_get_mtu(iplink_conn_t *conn, size_t *mtu)
208{
209 log_msg(LVL_DEBUG, "ethip_get_mtu()");
210 *mtu = 1500;
211 return EOK;
212}
213
214int main(int argc, char *argv[])
215{
216 int rc;
217
218 printf(NAME ": HelenOS IP over Ethernet service\n");
219
220 if (log_init(NAME, LVL_DEBUG) != EOK) {
221 printf(NAME ": Failed to initialize logging.\n");
222 return 1;
223 }
224
225 rc = ethip_init();
226 if (rc != EOK)
227 return 1;
228
229 printf(NAME ": Accepting connections.\n");
230 task_retval(0);
231 async_manager();
232
233 /* Not reached */
234 return 0;
235}
236
237/** @}
238 */
Note: See TracBrowser for help on using the repository browser.