[21580dd] | 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 |
|
---|
[849ed54] | 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>
|
---|
[21580dd] | 58 |
|
---|
[aadf01e] | 59 | DEVICE_MAP_IMPLEMENT(device_map, device_t)
|
---|
[21580dd] | 60 |
|
---|
[849ed54] | 61 | /** Network interface global data.
|
---|
[21580dd] | 62 | */
|
---|
[849ed54] | 63 | netif_globals_t netif_globals;
|
---|
[21580dd] | 64 |
|
---|
[aadf01e] | 65 | int netif_probe_req(int netif_phone, device_id_t device_id, int irq, int io){
|
---|
| 66 | int result;
|
---|
[21580dd] | 67 |
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 71 | return result;
|
---|
| 72 | }
|
---|
| 73 |
|
---|
[aadf01e] | 74 | int netif_send_msg(int netif_phone, device_id_t device_id, packet_t packet, services_t sender){
|
---|
| 75 | int result;
|
---|
[21580dd] | 76 |
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 80 | return result;
|
---|
| 81 | }
|
---|
| 82 |
|
---|
[aadf01e] | 83 | int netif_start_req(int netif_phone, device_id_t device_id){
|
---|
[21580dd] | 84 | ERROR_DECLARE;
|
---|
| 85 |
|
---|
[aadf01e] | 86 | device_ref device;
|
---|
| 87 | int result;
|
---|
| 88 | int phone;
|
---|
[21580dd] | 89 |
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 93 | return ERROR_CODE;
|
---|
| 94 | }
|
---|
[aadf01e] | 95 | result = netif_start_message(device);
|
---|
| 96 | if(result > NETIF_NULL){
|
---|
[21580dd] | 97 | phone = device->nil_phone;
|
---|
[aadf01e] | 98 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 99 | nil_device_state_msg(phone, device_id, result);
|
---|
[21580dd] | 100 | return EOK;
|
---|
| 101 | }else{
|
---|
[aadf01e] | 102 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 103 | }
|
---|
| 104 | return result;
|
---|
| 105 | }
|
---|
| 106 |
|
---|
[aadf01e] | 107 | int netif_stop_req(int netif_phone, device_id_t device_id){
|
---|
[21580dd] | 108 | ERROR_DECLARE;
|
---|
| 109 |
|
---|
[aadf01e] | 110 | device_ref device;
|
---|
| 111 | int result;
|
---|
| 112 | int phone;
|
---|
[21580dd] | 113 |
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 117 | return ERROR_CODE;
|
---|
| 118 | }
|
---|
[aadf01e] | 119 | result = netif_stop_message(device);
|
---|
| 120 | if(result > NETIF_NULL){
|
---|
[21580dd] | 121 | phone = device->nil_phone;
|
---|
[aadf01e] | 122 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 123 | nil_device_state_msg(phone, device_id, result);
|
---|
[21580dd] | 124 | return EOK;
|
---|
| 125 | }else{
|
---|
[aadf01e] | 126 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
[21580dd] | 127 | }
|
---|
| 128 | return result;
|
---|
| 129 | }
|
---|
| 130 |
|
---|
[aadf01e] | 131 | int netif_stats_req(int netif_phone, device_id_t device_id, device_stats_ref stats){
|
---|
[21580dd] | 132 | int res;
|
---|
| 133 |
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 137 | return res;
|
---|
| 138 | }
|
---|
| 139 |
|
---|
[aadf01e] | 140 | int netif_get_addr_req(int netif_phone, device_id_t device_id, measured_string_ref * address, char ** data){
|
---|
[21580dd] | 141 | ERROR_DECLARE;
|
---|
| 142 |
|
---|
[aadf01e] | 143 | measured_string_t translation;
|
---|
[21580dd] | 144 |
|
---|
[aadf01e] | 145 | if(!(address && data)){
|
---|
| 146 | return EBADMEM;
|
---|
[21580dd] | 147 | }
|
---|
[aadf01e] | 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;
|
---|
[21580dd] | 155 | return ERROR_CODE;
|
---|
| 156 | }
|
---|
| 157 |
|
---|
[aadf01e] | 158 | int netif_bind_service(services_t service, device_id_t device_id, services_t me, async_client_conn_t receiver){
|
---|
[21580dd] | 159 | return EOK;
|
---|
| 160 | }
|
---|
| 161 |
|
---|
[aadf01e] | 162 | int 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;
|
---|
[21580dd] | 171 | return EOK;
|
---|
| 172 | }
|
---|
| 173 |
|
---|
[aadf01e] | 174 | void null_device_stats(device_stats_ref stats){
|
---|
| 175 | bzero(stats, sizeof(device_stats_t));
|
---|
[21580dd] | 176 | }
|
---|
| 177 |
|
---|
[849ed54] | 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 | */
|
---|
| 185 | static int register_message(device_id_t device_id, int phone){
|
---|
[21580dd] | 186 | ERROR_DECLARE;
|
---|
| 187 |
|
---|
[aadf01e] | 188 | device_ref device;
|
---|
[21580dd] | 189 |
|
---|
[aadf01e] | 190 | ERROR_PROPAGATE(find_device(device_id, &device));
|
---|
| 191 | if(device->nil_phone > 0){
|
---|
| 192 | return ELIMIT;
|
---|
| 193 | }
|
---|
[21580dd] | 194 | device->nil_phone = phone;
|
---|
[aadf01e] | 195 | printf("New receiver of the device %d registered:\n\tphone\t= %d\n", device->device_id, device->nil_phone);
|
---|
[21580dd] | 196 | return EOK;
|
---|
| 197 | }
|
---|
| 198 |
|
---|
[aadf01e] | 199 | int netif_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 200 | ERROR_DECLARE;
|
---|
| 201 |
|
---|
[aadf01e] | 202 | size_t length;
|
---|
| 203 | device_stats_t stats;
|
---|
| 204 | packet_t packet;
|
---|
| 205 | measured_string_t address;
|
---|
[21580dd] | 206 |
|
---|
[aadf01e] | 207 | // printf("message %d - %d\n", IPC_GET_METHOD(*call), NET_NETIF_FIRST);
|
---|
| 208 | *answer_count = 0;
|
---|
| 209 | switch(IPC_GET_METHOD(*call)){
|
---|
[21580dd] | 210 | case IPC_M_PHONE_HUNGUP:
|
---|
| 211 | return EOK;
|
---|
| 212 | case NET_NETIF_PROBE:
|
---|
[aadf01e] | 213 | return netif_probe_req(0, IPC_GET_DEVICE(call), NETIF_GET_IRQ(call), NETIF_GET_IO(call));
|
---|
[21580dd] | 214 | case IPC_M_CONNECT_TO_ME:
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 218 | return ERROR_CODE;
|
---|
| 219 | case NET_NETIF_SEND:
|
---|
[aadf01e] | 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));
|
---|
[21580dd] | 222 | case NET_NETIF_START:
|
---|
[aadf01e] | 223 | return netif_start_req(0, IPC_GET_DEVICE(call));
|
---|
[21580dd] | 224 | case NET_NETIF_STATS:
|
---|
[aadf01e] | 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)){
|
---|
[21580dd] | 228 | ERROR_CODE = EOVERFLOW;
|
---|
| 229 | }else{
|
---|
[aadf01e] | 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));
|
---|
[21580dd] | 232 | }
|
---|
| 233 | }
|
---|
| 234 | }
|
---|
[aadf01e] | 235 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[21580dd] | 236 | return ERROR_CODE;
|
---|
| 237 | case NET_NETIF_STOP:
|
---|
[aadf01e] | 238 | return netif_stop_req(0, IPC_GET_DEVICE(call));
|
---|
[21580dd] | 239 | case NET_NETIF_GET_ADDR:
|
---|
[aadf01e] | 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);
|
---|
[21580dd] | 243 | }
|
---|
[aadf01e] | 244 | fibril_rwlock_read_unlock(&netif_globals.lock);
|
---|
[21580dd] | 245 | return ERROR_CODE;
|
---|
| 246 | }
|
---|
[aadf01e] | 247 | return netif_specific_message(callid, call, answer, answer_count);
|
---|
[21580dd] | 248 | }
|
---|
| 249 |
|
---|
[aadf01e] | 250 | int netif_init_module(async_client_conn_t client_connection){
|
---|
[21580dd] | 251 | ERROR_DECLARE;
|
---|
| 252 |
|
---|
[aadf01e] | 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())){
|
---|
[21580dd] | 259 | pm_destroy();
|
---|
| 260 | return ERROR_CODE;
|
---|
| 261 | }
|
---|
| 262 | return EOK;
|
---|
| 263 | }
|
---|
| 264 |
|
---|
[aadf01e] | 265 | int netif_run_module(void){
|
---|
[21580dd] | 266 | async_manager();
|
---|
| 267 |
|
---|
| 268 | pm_destroy();
|
---|
| 269 | return EOK;
|
---|
| 270 | }
|
---|
| 271 |
|
---|
[aadf01e] | 272 | void netif_pq_release(packet_id_t packet_id){
|
---|
| 273 | pq_release(netif_globals.net_phone, packet_id);
|
---|
[21580dd] | 274 | }
|
---|
| 275 |
|
---|
[aadf01e] | 276 | packet_t netif_packet_get_1(size_t content){
|
---|
| 277 | return packet_get_1(netif_globals.net_phone, content);
|
---|
[21580dd] | 278 | }
|
---|
| 279 |
|
---|
| 280 | /** @}
|
---|
| 281 | */
|
---|