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