[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 lo
|
---|
| 30 | * @{
|
---|
| 31 | */
|
---|
| 32 |
|
---|
| 33 | /** @file
|
---|
| 34 | * Loopback network interface implementation.
|
---|
| 35 | */
|
---|
| 36 |
|
---|
| 37 | #include <async.h>
|
---|
| 38 | #include <errno.h>
|
---|
[c5b59ce] | 39 | #include <err.h>
|
---|
[21580dd] | 40 | #include <stdio.h>
|
---|
[19f857a] | 41 | #include <str.h>
|
---|
[21580dd] | 42 |
|
---|
| 43 | #include <ipc/ipc.h>
|
---|
| 44 | #include <ipc/services.h>
|
---|
| 45 |
|
---|
[849ed54] | 46 | #include <net_messages.h>
|
---|
[c7a8442] | 47 | #include <net/modules.h>
|
---|
[849ed54] | 48 | #include <adt/measured_strings.h>
|
---|
[0a866eeb] | 49 | #include <packet_client.h>
|
---|
[e526f08] | 50 | #include <net/device.h>
|
---|
[849ed54] | 51 | #include <nil_interface.h>
|
---|
| 52 | #include <nil_messages.h>
|
---|
[14f1db0] | 53 | #include <netif_interface.h>
|
---|
| 54 | #include <netif_local.h>
|
---|
[21580dd] | 55 |
|
---|
| 56 | /** Default hardware address.
|
---|
| 57 | */
|
---|
| 58 | #define DEFAULT_ADDR "\0\0\0\0\0\0"
|
---|
| 59 |
|
---|
| 60 | /** Default address length.
|
---|
| 61 | */
|
---|
[aadf01e] | 62 | #define DEFAULT_ADDR_LEN (sizeof(DEFAULT_ADDR) / sizeof(char))
|
---|
[21580dd] | 63 |
|
---|
| 64 | /** Loopback module name.
|
---|
| 65 | */
|
---|
[24ab58b3] | 66 | #define NAME "lo"
|
---|
[21580dd] | 67 |
|
---|
| 68 | /** Network interface global data.
|
---|
| 69 | */
|
---|
| 70 | netif_globals_t netif_globals;
|
---|
| 71 |
|
---|
| 72 | /** Changes the loopback state.
|
---|
| 73 | * @param[in] device The device structure.
|
---|
| 74 | * @param[in] state The new device state.
|
---|
| 75 | * @returns The new state if changed.
|
---|
| 76 | * @returns EOK otherwise.
|
---|
| 77 | */
|
---|
[14f1db0] | 78 | int change_state_message(netif_device_t * device, device_state_t state);
|
---|
[21580dd] | 79 |
|
---|
| 80 | /** Creates and returns the loopback network interface structure.
|
---|
| 81 | * @param[in] device_id The new devce identifier.
|
---|
| 82 | * @param[out] device The device structure.
|
---|
| 83 | * @returns EOK on success.
|
---|
| 84 | * @returns EXDEV if one loopback network interface already exists.
|
---|
| 85 | * @returns ENOMEM if there is not enough memory left.
|
---|
| 86 | */
|
---|
[14f1db0] | 87 | int create(device_id_t device_id, netif_device_t * * device);
|
---|
[21580dd] | 88 |
|
---|
[aadf01e] | 89 | int netif_specific_message(ipc_callid_t callid, ipc_call_t * call, ipc_call_t * answer, int * answer_count){
|
---|
[21580dd] | 90 | return ENOTSUP;
|
---|
| 91 | }
|
---|
| 92 |
|
---|
[aadf01e] | 93 | int netif_get_addr_message(device_id_t device_id, measured_string_ref address){
|
---|
| 94 | if(! address){
|
---|
| 95 | return EBADMEM;
|
---|
| 96 | }
|
---|
[a000878c] | 97 | address->value = str_dup(DEFAULT_ADDR);
|
---|
[21580dd] | 98 | address->length = DEFAULT_ADDR_LEN;
|
---|
| 99 | return EOK;
|
---|
| 100 | }
|
---|
| 101 |
|
---|
[aadf01e] | 102 | int netif_get_device_stats(device_id_t device_id, device_stats_ref stats){
|
---|
[21580dd] | 103 | ERROR_DECLARE;
|
---|
| 104 |
|
---|
[14f1db0] | 105 | netif_device_t * device;
|
---|
[21580dd] | 106 |
|
---|
[aadf01e] | 107 | if(! stats){
|
---|
| 108 | return EBADMEM;
|
---|
| 109 | }
|
---|
| 110 | ERROR_PROPAGATE(find_device(device_id, &device));
|
---|
| 111 | memcpy(stats, (device_stats_ref) device->specific, sizeof(device_stats_t));
|
---|
[21580dd] | 112 | return EOK;
|
---|
| 113 | }
|
---|
| 114 |
|
---|
[14f1db0] | 115 | int change_state_message(netif_device_t * device, device_state_t state)
|
---|
[24ab58b3] | 116 | {
|
---|
| 117 | if (device->state != state) {
|
---|
[21580dd] | 118 | device->state = state;
|
---|
[24ab58b3] | 119 |
|
---|
| 120 | printf("%s: State changed to %s\n", NAME,
|
---|
| 121 | (state == NETIF_ACTIVE) ? "active" : "stopped");
|
---|
| 122 |
|
---|
[21580dd] | 123 | return state;
|
---|
| 124 | }
|
---|
[24ab58b3] | 125 |
|
---|
[21580dd] | 126 | return EOK;
|
---|
| 127 | }
|
---|
| 128 |
|
---|
[14f1db0] | 129 | int create(device_id_t device_id, netif_device_t * * device){
|
---|
[aadf01e] | 130 | int index;
|
---|
[21580dd] | 131 |
|
---|
[14f1db0] | 132 | if(netif_device_map_count(&netif_globals.device_map) > 0){
|
---|
[21580dd] | 133 | return EXDEV;
|
---|
| 134 | }else{
|
---|
[14f1db0] | 135 | *device = (netif_device_t *) malloc(sizeof(netif_device_t));
|
---|
[aadf01e] | 136 | if(!(*device)){
|
---|
| 137 | return ENOMEM;
|
---|
| 138 | }
|
---|
| 139 | (** device).specific = malloc(sizeof(device_stats_t));
|
---|
| 140 | if(! (** device).specific){
|
---|
| 141 | free(*device);
|
---|
[21580dd] | 142 | return ENOMEM;
|
---|
| 143 | }
|
---|
[aadf01e] | 144 | null_device_stats((device_stats_ref)(** device).specific);
|
---|
| 145 | (** device).device_id = device_id;
|
---|
| 146 | (** device).nil_phone = -1;
|
---|
| 147 | (** device).state = NETIF_STOPPED;
|
---|
[14f1db0] | 148 | index = netif_device_map_add(&netif_globals.device_map, (** device).device_id, * device);
|
---|
[aadf01e] | 149 | if(index < 0){
|
---|
| 150 | free(*device);
|
---|
| 151 | free((** device).specific);
|
---|
| 152 | *device = NULL;
|
---|
[21580dd] | 153 | return index;
|
---|
| 154 | }
|
---|
| 155 | }
|
---|
| 156 | return EOK;
|
---|
| 157 | }
|
---|
| 158 |
|
---|
[aadf01e] | 159 | int netif_initialize(void){
|
---|
| 160 | ipcarg_t phonehash;
|
---|
[21580dd] | 161 |
|
---|
[aadf01e] | 162 | return REGISTER_ME(SERVICE_LO, &phonehash);
|
---|
[21580dd] | 163 | }
|
---|
| 164 |
|
---|
[aadf01e] | 165 | int netif_probe_message(device_id_t device_id, int irq, uintptr_t io){
|
---|
[21580dd] | 166 | ERROR_DECLARE;
|
---|
| 167 |
|
---|
[14f1db0] | 168 | netif_device_t * device;
|
---|
[21580dd] | 169 |
|
---|
| 170 | // create a new device
|
---|
[aadf01e] | 171 | ERROR_PROPAGATE(create(device_id, &device));
|
---|
[21580dd] | 172 | // print the settings
|
---|
[24ab58b3] | 173 | printf("%s: Device created (id: %d)\n", NAME, device->device_id);
|
---|
[21580dd] | 174 | return EOK;
|
---|
| 175 | }
|
---|
| 176 |
|
---|
[aadf01e] | 177 | int netif_send_message(device_id_t device_id, packet_t packet, services_t sender){
|
---|
[21580dd] | 178 | ERROR_DECLARE;
|
---|
| 179 |
|
---|
[14f1db0] | 180 | netif_device_t * device;
|
---|
[aadf01e] | 181 | size_t length;
|
---|
| 182 | packet_t next;
|
---|
| 183 | int phone;
|
---|
[21580dd] | 184 |
|
---|
[aadf01e] | 185 | ERROR_PROPAGATE(find_device(device_id, &device));
|
---|
| 186 | if(device->state != NETIF_ACTIVE){
|
---|
| 187 | netif_pq_release(packet_get_id(packet));
|
---|
[21580dd] | 188 | return EFORWARD;
|
---|
| 189 | }
|
---|
| 190 | next = packet;
|
---|
| 191 | do{
|
---|
[aadf01e] | 192 | ++ ((device_stats_ref) device->specific)->send_packets;
|
---|
| 193 | ++ ((device_stats_ref) device->specific)->receive_packets;
|
---|
| 194 | length = packet_get_data_length(next);
|
---|
| 195 | ((device_stats_ref) device->specific)->send_bytes += length;
|
---|
| 196 | ((device_stats_ref) device->specific)->receive_bytes += length;
|
---|
| 197 | next = pq_next(next);
|
---|
| 198 | }while(next);
|
---|
[21580dd] | 199 | phone = device->nil_phone;
|
---|
[aadf01e] | 200 | fibril_rwlock_write_unlock(&netif_globals.lock);
|
---|
| 201 | nil_received_msg(phone, device_id, packet, sender);
|
---|
| 202 | fibril_rwlock_write_lock(&netif_globals.lock);
|
---|
[21580dd] | 203 | return EOK;
|
---|
| 204 | }
|
---|
| 205 |
|
---|
[14f1db0] | 206 | int netif_start_message(netif_device_t * device){
|
---|
[aadf01e] | 207 | return change_state_message(device, NETIF_ACTIVE);
|
---|
[21580dd] | 208 | }
|
---|
| 209 |
|
---|
[14f1db0] | 210 | int netif_stop_message(netif_device_t * device){
|
---|
[aadf01e] | 211 | return change_state_message(device, NETIF_STOPPED);
|
---|
[21580dd] | 212 | }
|
---|
| 213 |
|
---|
[849ed54] | 214 | /** Default thread for new connections.
|
---|
| 215 | *
|
---|
[14f1db0] | 216 | * @param[in] iid The initial message identifier.
|
---|
| 217 | * @param[in] icall The initial message call structure.
|
---|
[849ed54] | 218 | *
|
---|
| 219 | */
|
---|
[14f1db0] | 220 | static void netif_client_connection(ipc_callid_t iid, ipc_call_t *icall)
|
---|
[849ed54] | 221 | {
|
---|
| 222 | /*
|
---|
| 223 | * Accept the connection
|
---|
| 224 | * - Answer the first IPC_M_CONNECT_ME_TO call.
|
---|
| 225 | */
|
---|
| 226 | ipc_answer_0(iid, EOK);
|
---|
| 227 |
|
---|
| 228 | while(true) {
|
---|
| 229 | ipc_call_t answer;
|
---|
| 230 | int answer_count;
|
---|
| 231 |
|
---|
| 232 | /* Clear the answer structure */
|
---|
| 233 | refresh_answer(&answer, &answer_count);
|
---|
| 234 |
|
---|
| 235 | /* Fetch the next message */
|
---|
| 236 | ipc_call_t call;
|
---|
| 237 | ipc_callid_t callid = async_get_call(&call);
|
---|
| 238 |
|
---|
| 239 | /* Process the message */
|
---|
[24ab58b3] | 240 | int res = netif_module_message(NAME, callid, &call, &answer,
|
---|
| 241 | &answer_count);
|
---|
[849ed54] | 242 |
|
---|
| 243 | /* End if said to either by the message or the processing result */
|
---|
| 244 | if ((IPC_GET_METHOD(call) == IPC_M_PHONE_HUNGUP) || (res == EHANGUP))
|
---|
| 245 | return;
|
---|
| 246 |
|
---|
| 247 | /* Answer the message */
|
---|
| 248 | answer_call(callid, res, &answer, answer_count);
|
---|
| 249 | }
|
---|
| 250 | }
|
---|
| 251 |
|
---|
| 252 | int main(int argc, char *argv[])
|
---|
| 253 | {
|
---|
| 254 | ERROR_DECLARE;
|
---|
| 255 |
|
---|
| 256 | /* Start the module */
|
---|
[24ab58b3] | 257 | if (ERROR_OCCURRED(netif_module_start(netif_client_connection)))
|
---|
[849ed54] | 258 | return ERROR_CODE;
|
---|
| 259 |
|
---|
| 260 | return EOK;
|
---|
| 261 | }
|
---|
| 262 |
|
---|
[21580dd] | 263 | /** @}
|
---|
| 264 | */
|
---|