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