Changeset 7a725b13 in mainline for uspace/srv/net/netif/lo/lo.c
- Timestamp:
- 2011-01-14T14:23:33Z (14 years ago)
- Branches:
- lfn, master, serial, ticket/834-toolchain-update, topic/msim-upgrade, topic/simplify-dev-export
- Children:
- b65ca41d
- Parents:
- f40a1e2 (diff), 2f60e57d (diff)
Note: this is a merge changeset, the changes displayed below correspond to the merge itself.
Use the(diff)
links above to see all the changes relative to each parent. - File:
-
- 1 edited
Legend:
- Unmodified
- Added
- Removed
-
uspace/srv/net/netif/lo/lo.c
rf40a1e2 r7a725b13 48 48 #include <packet_client.h> 49 49 #include <net/device.h> 50 #include <nil_interface.h>51 50 #include <netif_skel.h> 52 53 /** Default hardware address. */ 54 #define DEFAULT_ADDR 0 51 #include <nil_remote.h> 55 52 56 53 /** Default address length. */ … … 60 57 #define NAME "lo" 61 58 62 /** Network interface global data. */ 63 netif_globals_t netif_globals;59 static uint8_t default_addr[DEFAULT_ADDR_LEN] = 60 {0, 0, 0, 0, 0, 0}; 64 61 65 62 int netif_specific_message(ipc_callid_t callid, ipc_call_t *call, … … 74 71 return EBADMEM; 75 72 76 uint8_t *addr = (uint8_t *) malloc(DEFAULT_ADDR_LEN); 77 memset(addr, DEFAULT_ADDR, DEFAULT_ADDR_LEN); 78 79 address->value = addr; 73 address->value = default_addr; 80 74 address->length = DEFAULT_ADDR_LEN; 81 75 … … 85 79 int netif_get_device_stats(device_id_t device_id, device_stats_t *stats) 86 80 { 87 netif_device_t *device;88 int rc;89 90 81 if (!stats) 91 82 return EBADMEM; 92 93 rc = find_device(device_id, &device); 83 84 netif_device_t *device; 85 int rc = find_device(device_id, &device); 94 86 if (rc != EOK) 95 87 return rc; 96 88 97 89 memcpy(stats, (device_stats_t *) device->specific, 98 90 sizeof(device_stats_t)); 99 100 return EOK; 101 } 102 103 /** Changes the loopback state. 104 * 105 * @param[in] device The device structure. 106 * @param[in] state The new device state. 107 * @return The new state if changed. 108 * @return EOK otherwise. 109 */ 110 static int change_state_message(netif_device_t *device, device_state_t state) 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) 111 105 { 112 106 if (device->state != state) { 113 107 device->state = state; 114 108 115 printf("%s: State changed to %s\n", NAME, 116 (state == NETIF_ACTIVE) ? "active" : "stopped"); 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 } 117 120 118 return state; 119 } 120 121 return EOK; 122 } 123 124 /** Creates and returns the loopback network interface structure. 125 * 126 * @param[in] device_id The new devce identifier. 127 * @param[out] device The device structure. 128 * @return EOK on success. 129 * @return EXDEV if one loopback network interface already exists. 130 * @return ENOMEM if there is not enough memory left. 131 */ 132 static int create(device_id_t device_id, netif_device_t **device) 133 { 134 int index; 135 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 { 136 137 if (netif_device_map_count(&netif_globals.device_map) > 0) 137 138 return EXDEV; 138 139 139 140 *device = (netif_device_t *) malloc(sizeof(netif_device_t)); 140 141 if (!*device) 141 142 return ENOMEM; 142 143 143 144 (*device)->specific = (device_stats_t *) malloc(sizeof(device_stats_t)); 144 145 if (!(*device)->specific) { … … 146 147 return ENOMEM; 147 148 } 148 149 149 150 null_device_stats((device_stats_t *) (*device)->specific); 150 151 (*device)->device_id = device_id; 151 152 (*device)->nil_phone = -1; 152 153 (*device)->state = NETIF_STOPPED; 153 in dex = netif_device_map_add(&netif_globals.device_map,154 int index = netif_device_map_add(&netif_globals.device_map, 154 155 (*device)->device_id, *device); 155 156 156 157 if (index < 0) { 157 158 free(*device); … … 167 168 { 168 169 sysarg_t phonehash; 169 170 170 return ipc_connect_to_me(PHONE_NS, SERVICE_LO, 0, 0, &phonehash); 171 171 } … … 173 173 int netif_probe_message(device_id_t device_id, int irq, void *io) 174 174 { 175 /* Create a new device */ 175 176 netif_device_t *device; 176 int rc; 177 178 /* Create a new device */ 179 rc = create(device_id, &device); 177 int rc = lo_create(device_id, &device); 180 178 if (rc != EOK) 181 179 return rc; 182 183 /* Print the settings */ 180 184 181 printf("%s: Device created (id: %d)\n", NAME, device->device_id); 185 186 182 return EOK; 187 183 } … … 190 186 { 191 187 netif_device_t *device; 192 size_t length; 193 packet_t *next; 194 int phone; 195 int rc; 196 197 rc = find_device(device_id, &device); 188 int rc = find_device(device_id, &device); 198 189 if (rc != EOK) 199 190 return EOK; 200 191 201 192 if (device->state != NETIF_ACTIVE) { 202 193 netif_pq_release(packet_get_id(packet)); 203 194 return EFORWARD; 204 195 } 205 206 next = packet;196 197 packet_t *next = packet; 207 198 do { 208 199 ((device_stats_t *) device->specific)->send_packets++; 209 200 ((device_stats_t *) device->specific)->receive_packets++; 210 length = packet_get_data_length(next);201 size_t length = packet_get_data_length(next); 211 202 ((device_stats_t *) device->specific)->send_bytes += length; 212 203 ((device_stats_t *) device->specific)->receive_bytes += length; 213 204 next = pq_next(next); 214 } while (next);215 216 phone = device->nil_phone;205 } while (next); 206 207 int phone = device->nil_phone; 217 208 fibril_rwlock_write_unlock(&netif_globals.lock); 209 218 210 nil_received_msg(phone, device_id, packet, sender); 211 219 212 fibril_rwlock_write_lock(&netif_globals.lock); 220 221 213 return EOK; 222 214 } … … 224 216 int netif_start_message(netif_device_t *device) 225 217 { 226 return change_state_message(device, NETIF_ACTIVE); 218 change_state_message(device, NETIF_ACTIVE); 219 return device->state; 227 220 } 228 221 229 222 int netif_stop_message(netif_device_t *device) 230 223 { 231 return change_state_message(device, NETIF_STOPPED); 224 change_state_message(device, NETIF_STOPPED); 225 return device->state; 232 226 } 233 227
Note:
See TracChangeset
for help on using the changeset viewer.