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