source: mainline/uspace/srv/net/netif/lo/lo.c@ b6c7da6

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

do not intermix low-level IPC methods with async framework methods

  • Property mode set to 100644
File size: 5.7 KB
RevLine 
[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#include <ipc/services.h>
[f5a3479]42#include <ipc/nil.h>
[c7a8442]43#include <net/modules.h>
[849ed54]44#include <adt/measured_strings.h>
[0a866eeb]45#include <packet_client.h>
[e526f08]46#include <net/device.h>
[774e6d1a]47#include <netif_skel.h>
[fe8dfa6]48#include <nil_remote.h>
[21580dd]49
[c4fbf7fd]50/** Default address length. */
[61bfc370]51#define DEFAULT_ADDR_LEN 6
[21580dd]52
[c4fbf7fd]53/** Loopback module name. */
[24ab58b3]54#define NAME "lo"
[21580dd]55
[fe8dfa6]56static uint8_t default_addr[DEFAULT_ADDR_LEN] =
57 {0, 0, 0, 0, 0, 0};
[21580dd]58
[fb04cba8]59int netif_specific_message(ipc_callid_t callid, ipc_call_t *call,
[774e6d1a]60 ipc_call_t *answer, size_t *count)
[c4fbf7fd]61{
[21580dd]62 return ENOTSUP;
63}
64
[4eca056]65int netif_get_addr_message(device_id_t device_id, measured_string_t *address)
[c4fbf7fd]66{
67 if (!address)
[aadf01e]68 return EBADMEM;
[61bfc370]69
[fe8dfa6]70 address->value = default_addr;
[21580dd]71 address->length = DEFAULT_ADDR_LEN;
[61bfc370]72
[21580dd]73 return EOK;
74}
75
[f772bc55]76int netif_get_device_stats(device_id_t device_id, device_stats_t *stats)
[c4fbf7fd]77{
78 if (!stats)
[aadf01e]79 return EBADMEM;
[fe8dfa6]80
81 netif_device_t *device;
82 int rc = find_device(device_id, &device);
[2e236901]83 if (rc != EOK)
84 return rc;
[fe8dfa6]85
[f772bc55]86 memcpy(stats, (device_stats_t *) device->specific,
[c4fbf7fd]87 sizeof(device_stats_t));
[fe8dfa6]88
[21580dd]89 return EOK;
90}
91
[fe8dfa6]92/** Change the loopback state.
93 *
94 * @param[in] device The device structure.
95 * @param[in] state The new device state.
96 *
97 * @return New state if changed.
98 * @return EOK otherwise.
[c4fbf7fd]99 *
100 */
[fe8dfa6]101static void change_state_message(netif_device_t *device, device_state_t state)
[24ab58b3]102{
103 if (device->state != state) {
[21580dd]104 device->state = state;
[24ab58b3]105
[fe8dfa6]106 const char *desc;
107 switch (state) {
108 case NETIF_ACTIVE:
109 desc = "active";
110 break;
111 case NETIF_STOPPED:
112 desc = "stopped";
113 break;
114 default:
115 desc = "unknown";
116 }
[24ab58b3]117
[fe8dfa6]118 printf("%s: State changed to %s\n", NAME, desc);
[21580dd]119 }
120}
121
[fe8dfa6]122/** Create and return the loopback network interface structure.
123 *
124 * @param[in] device_id New devce identifier.
125 * @param[out] device Device structure.
126 *
127 * @return EOK on success.
128 * @return EXDEV if one loopback network interface already exists.
129 * @return ENOMEM if there is not enough memory left.
[c4fbf7fd]130 *
131 */
[fe8dfa6]132static int lo_create(device_id_t device_id, netif_device_t **device)
[c4fbf7fd]133{
134 if (netif_device_map_count(&netif_globals.device_map) > 0)
[21580dd]135 return EXDEV;
[fe8dfa6]136
[c4fbf7fd]137 *device = (netif_device_t *) malloc(sizeof(netif_device_t));
138 if (!*device)
139 return ENOMEM;
[fe8dfa6]140
[c4fbf7fd]141 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t));
142 if (!(*device)->specific) {
143 free(*device);
144 return ENOMEM;
[21580dd]145 }
[fe8dfa6]146
[f772bc55]147 null_device_stats((device_stats_t *) (*device)->specific);
[c4fbf7fd]148 (*device)->device_id = device_id;
149 (*device)->nil_phone = -1;
150 (*device)->state = NETIF_STOPPED;
[fe8dfa6]151 int index = netif_device_map_add(&netif_globals.device_map,
[c4fbf7fd]152 (*device)->device_id, *device);
[fe8dfa6]153
[c4fbf7fd]154 if (index < 0) {
155 free(*device);
156 free((*device)->specific);
157 *device = NULL;
158 return index;
159 }
160
[21580dd]161 return EOK;
162}
163
[c4fbf7fd]164int netif_initialize(void)
165{
[ffa2c8ef]166 return async_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, NULL);
[21580dd]167}
168
[774e6d1a]169int netif_probe_message(device_id_t device_id, int irq, void *io)
[c4fbf7fd]170{
[fb04cba8]171 /* Create a new device */
[fe8dfa6]172 netif_device_t *device;
173 int rc = lo_create(device_id, &device);
[2e236901]174 if (rc != EOK)
175 return rc;
[fe8dfa6]176
[24ab58b3]177 printf("%s: Device created (id: %d)\n", NAME, device->device_id);
[21580dd]178 return EOK;
179}
180
[46d4d9f]181int netif_send_message(device_id_t device_id, packet_t *packet, services_t sender)
[c4fbf7fd]182{
183 netif_device_t *device;
[fe8dfa6]184 int rc = find_device(device_id, &device);
[2e236901]185 if (rc != EOK)
186 return EOK;
[fe8dfa6]187
[c4fbf7fd]188 if (device->state != NETIF_ACTIVE) {
[aadf01e]189 netif_pq_release(packet_get_id(packet));
[21580dd]190 return EFORWARD;
191 }
[fe8dfa6]192
193 packet_t *next = packet;
[c4fbf7fd]194 do {
[f772bc55]195 ((device_stats_t *) device->specific)->send_packets++;
196 ((device_stats_t *) device->specific)->receive_packets++;
[fe8dfa6]197 size_t length = packet_get_data_length(next);
[f772bc55]198 ((device_stats_t *) device->specific)->send_bytes += length;
199 ((device_stats_t *) device->specific)->receive_bytes += length;
[aadf01e]200 next = pq_next(next);
[fe8dfa6]201 } while (next);
202
203 int phone = device->nil_phone;
[aadf01e]204 fibril_rwlock_write_unlock(&netif_globals.lock);
[fe8dfa6]205
[aadf01e]206 nil_received_msg(phone, device_id, packet, sender);
[c4fbf7fd]207
[fe8dfa6]208 fibril_rwlock_write_lock(&netif_globals.lock);
[21580dd]209 return EOK;
210}
211
[c4fbf7fd]212int netif_start_message(netif_device_t *device)
213{
[fe8dfa6]214 change_state_message(device, NETIF_ACTIVE);
215 return device->state;
[21580dd]216}
217
[c4fbf7fd]218int netif_stop_message(netif_device_t *device)
219{
[fe8dfa6]220 change_state_message(device, NETIF_STOPPED);
221 return device->state;
[21580dd]222}
223
[849ed54]224int main(int argc, char *argv[])
225{
226 /* Start the module */
[774e6d1a]227 return netif_module_start();
[849ed54]228}
229
[21580dd]230/** @}
231 */
Note: See TracBrowser for help on using the repository browser.