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