source: mainline/uspace/lib/net/netif/netif.c@ 849ed54

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

Networking work:
Split the networking stack into end-user library (libsocket) and two helper libraries (libnet and libnetif).
Don't use over-the-hand compiling and linking, but rather separation of conserns.
There might be still some issues and the non-modular networking architecture is currently broken, but this will be fixed soon.

  • Property mode set to 100644
File size: 8.2 KB
Line 
1/*
2 * Copyright (c) 2009 Lukas Mejdrech
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 netif
30 * @{
31 */
32
33/** @file
34 * Network interface module skeleton implementation.
35 * @see netif.h
36 */
37
38#include <async.h>
39#include <mem.h>
40#include <fibril_synch.h>
41#include <stdio.h>
42
43#include <ipc/ipc.h>
44#include <ipc/services.h>
45
46#include <net_err.h>
47#include <net_messages.h>
48#include <net_modules.h>
49#include <packet/packet.h>
50#include <packet/packet_client.h>
51#include <adt/measured_strings.h>
52#include <net_device.h>
53#include <netif_interface.h>
54#include <nil_interface.h>
55#include <netif.h>
56#include <netif_messages.h>
57#include <netif_module.h>
58
59DEVICE_MAP_IMPLEMENT(device_map, device_t)
60
61/** Network interface global data.
62 */
63netif_globals_t netif_globals;
64
65int netif_probe_req(int netif_phone, device_id_t device_id, int irq, int io){
66 int result;
67
68 fibril_rwlock_write_lock(&netif_globals.lock);
69 result = netif_probe_message(device_id, irq, io);
70 fibril_rwlock_write_unlock(&netif_globals.lock);
71 return result;
72}
73
74int netif_send_msg(int netif_phone, device_id_t device_id, packet_t packet, services_t sender){
75 int result;
76
77 fibril_rwlock_write_lock(&netif_globals.lock);
78 result = netif_send_message(device_id, packet, sender);
79 fibril_rwlock_write_unlock(&netif_globals.lock);
80 return result;
81}
82
83int netif_start_req(int netif_phone, device_id_t device_id){
84 ERROR_DECLARE;
85
86 device_ref device;
87 int result;
88 int phone;
89
90 fibril_rwlock_write_lock(&netif_globals.lock);
91 if(ERROR_OCCURRED(find_device(device_id, &device))){
92 fibril_rwlock_write_unlock(&netif_globals.lock);
93 return ERROR_CODE;
94 }
95 result = netif_start_message(device);
96 if(result > NETIF_NULL){
97 phone = device->nil_phone;
98 fibril_rwlock_write_unlock(&netif_globals.lock);
99 nil_device_state_msg(phone, device_id, result);
100 return EOK;
101 }else{
102 fibril_rwlock_write_unlock(&netif_globals.lock);
103 }
104 return result;
105}
106
107int netif_stop_req(int netif_phone, device_id_t device_id){
108 ERROR_DECLARE;
109
110 device_ref device;
111 int result;
112 int phone;
113
114 fibril_rwlock_write_lock(&netif_globals.lock);
115 if(ERROR_OCCURRED(find_device(device_id, &device))){
116 fibril_rwlock_write_unlock(&netif_globals.lock);
117 return ERROR_CODE;
118 }
119 result = netif_stop_message(device);
120 if(result > NETIF_NULL){
121 phone = device->nil_phone;
122 fibril_rwlock_write_unlock(&netif_globals.lock);
123 nil_device_state_msg(phone, device_id, result);
124 return EOK;
125 }else{
126 fibril_rwlock_write_unlock(&netif_globals.lock);
127 }
128 return result;
129}
130
131int netif_stats_req(int netif_phone, device_id_t device_id, device_stats_ref stats){
132 int res;
133
134 fibril_rwlock_read_lock(&netif_globals.lock);
135 res = netif_get_device_stats(device_id, stats);
136 fibril_rwlock_read_unlock(&netif_globals.lock);
137 return res;
138}
139
140int netif_get_addr_req(int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data){
141 ERROR_DECLARE;
142
143 measured_string_t translation;
144
145 if(!(address && data)){
146 return EBADMEM;
147 }
148 fibril_rwlock_read_lock(&netif_globals.lock);
149 if(! ERROR_OCCURRED(netif_get_addr_message(device_id, &translation))){
150 *address = measured_string_copy(&translation);
151 ERROR_CODE = (*address) ? EOK : ENOMEM;
152 }
153 fibril_rwlock_read_unlock(&netif_globals.lock);
154 *data = (** address).value;
155 return ERROR_CODE;
156}
157
158int netif_bind_service(services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver){
159 return EOK;
160}
161
162int find_device(device_id_t device_id, device_ref * device){
163 if(! device){
164 return EBADMEM;
165 }
166 *device = device_map_find(&netif_globals.device_map, device_id);
167 if(! * device){
168 return ENOENT;
169 }
170 if((** device).state == NETIF_NULL) return EPERM;
171 return EOK;
172}
173
174void null_device_stats(device_stats_ref stats){
175 bzero(stats, sizeof(device_stats_t));
176}
177
178/** Registers the device notification receiver, the network interface layer module.
179 * @param[in] device_id The device identifier.
180 * @param[in] phone The network interface layer module phone.
181 * @returns EOK on success.
182 * @returns ENOENT if there is no such device.
183 * @returns ELIMIT if there is another module registered.
184 */
185static int register_message(device_id_t device_id, int phone){
186 ERROR_DECLARE;
187
188 device_ref device;
189
190 ERROR_PROPAGATE(find_device(device_id, &device));
191 if(device->nil_phone > 0){
192 return ELIMIT;
193 }
194 device->nil_phone = phone;
195 printf("New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone);
196 return EOK;
197}
198
199int netif_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
200 ERROR_DECLARE;
201
202 size_t length;
203 device_stats_t stats;
204 packet_t packet;
205 measured_string_t address;
206
207// printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NETIF_FIRST);
208 *answer_count = 0;
209 switch(IPC_GET_METHOD(*call)){
210 case IPC_M_PHONE_HUNGUP:
211 return EOK;
212 case NET_NETIF_PROBE:
213 return netif_probe_req(0, IPC_GET_DEVICE(call), NETIF_GET_IRQ(call), NETIF_GET_IO(call));
214 case IPC_M_CONNECT_TO_ME:
215 fibril_rwlock_write_lock(&netif_globals.lock);
216 ERROR_CODE = register_message(IPC_GET_DEVICE(call), IPC_GET_PHONE(call));
217 fibril_rwlock_write_unlock(&netif_globals.lock);
218 return ERROR_CODE;
219 case NET_NETIF_SEND:
220 ERROR_PROPAGATE(packet_translate(netif_globals.net_phone, &packet, IPC_GET_PACKET(call)));
221 return netif_send_msg(0, IPC_GET_DEVICE(call), packet, IPC_GET_SENDER(call));
222 case NET_NETIF_START:
223 return netif_start_req(0, IPC_GET_DEVICE(call));
224 case NET_NETIF_STATS:
225 fibril_rwlock_read_lock(&netif_globals.lock);
226 if(! ERROR_OCCURRED(async_data_read_receive(&callid, &length))){
227 if(length < sizeof(device_stats_t)){
228 ERROR_CODE = EOVERFLOW;
229 }else{
230 if(! ERROR_OCCURRED(netif_get_device_stats(IPC_GET_DEVICE(call), &stats))){
231 ERROR_CODE = async_data_read_finalize(callid, &stats, sizeof(device_stats_t));
232 }
233 }
234 }
235 fibril_rwlock_read_unlock(&netif_globals.lock);
236 return ERROR_CODE;
237 case NET_NETIF_STOP:
238 return netif_stop_req(0, IPC_GET_DEVICE(call));
239 case NET_NETIF_GET_ADDR:
240 fibril_rwlock_read_lock(&netif_globals.lock);
241 if(! ERROR_OCCURRED(netif_get_addr_message(IPC_GET_DEVICE(call), &address))){
242 ERROR_CODE = measured_strings_reply(&address, 1);
243 }
244 fibril_rwlock_read_unlock(&netif_globals.lock);
245 return ERROR_CODE;
246 }
247 return netif_specific_message(callid, call, answer, answer_count);
248}
249
250int netif_init_module(async_client_conn_t client_connection){
251 ERROR_DECLARE;
252
253 async_set_client_connection(client_connection);
254 netif_globals.net_phone = connect_to_service(SERVICE_NETWORKING);
255 device_map_initialize(&netif_globals.device_map);
256 ERROR_PROPAGATE(pm_init());
257 fibril_rwlock_initialize(&netif_globals.lock);
258 if(ERROR_OCCURRED(netif_initialize())){
259 pm_destroy();
260 return ERROR_CODE;
261 }
262 return EOK;
263}
264
265int netif_run_module(void){
266 async_manager();
267
268 pm_destroy();
269 return EOK;
270}
271
272void netif_pq_release(packet_id_t packet_id){
273 pq_release(netif_globals.net_phone, packet_id);
274}
275
276packet_t netif_packet_get_1(size_t content){
277 return packet_get_1(netif_globals.net_phone, content);
278}
279
280/** @}
281 */
Note: See TracBrowser for help on using the repository browser.