etherdevs -> ptr_vector
This commit is contained in:
parent
e0f7cea9c9
commit
9a6881f3b9
70
ether.c
70
ether.c
@ -1,3 +1,4 @@
|
||||
#include "pvec.h"
|
||||
#include "arp.h"
|
||||
#include "ether.h"
|
||||
#include "ipv4.h"
|
||||
@ -5,6 +6,7 @@
|
||||
#include "memory.h"
|
||||
#include "virtio.h"
|
||||
|
||||
|
||||
struct ipv4_header_t {
|
||||
unsigned char vhl;
|
||||
unsigned char tos;
|
||||
@ -20,17 +22,10 @@ struct ipv4_header_t {
|
||||
}__attribute__((packed));
|
||||
|
||||
|
||||
|
||||
struct ether_t **etherdevs;
|
||||
unsigned int etherdev_count = 0;
|
||||
struct ptr_vector ether_devs;
|
||||
|
||||
static int add_ether(struct ether_t *card) {
|
||||
if (!etherdev_count) {
|
||||
etherdevs = (struct ether_t **)malloc(sizeof(struct ether_t *));
|
||||
} else {
|
||||
etherdevs = (struct ether_t **)realloc(etherdevs, sizeof(struct ether_t *) * (etherdev_count + 1));
|
||||
}
|
||||
etherdevs[etherdev_count++] = card;
|
||||
ptr_vector_append(ðer_devs, card);
|
||||
return 1;
|
||||
}
|
||||
|
||||
@ -65,65 +60,72 @@ int ether_receive(struct ether_t *ether, unsigned char *packet, int len) {
|
||||
}
|
||||
|
||||
int ether_get_mac(int ether, unsigned char *dest) {
|
||||
if (ether < 0 || ether >= etherdev_count) {
|
||||
struct ether_t *card = ptr_vector_get(ðer_devs, ether);
|
||||
|
||||
if (card == NULL) {
|
||||
return -1;
|
||||
}
|
||||
dest[0] = etherdevs[ether]->mac[0];
|
||||
dest[1] = etherdevs[ether]->mac[1];
|
||||
dest[2] = etherdevs[ether]->mac[2];
|
||||
dest[3] = etherdevs[ether]->mac[3];
|
||||
dest[4] = etherdevs[ether]->mac[4];
|
||||
dest[5] = etherdevs[ether]->mac[5];
|
||||
dest[0] = card->mac[0];
|
||||
dest[1] = card->mac[1];
|
||||
dest[2] = card->mac[2];
|
||||
dest[3] = card->mac[3];
|
||||
dest[4] = card->mac[4];
|
||||
dest[5] = card->mac[5];
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
int ether_enable(int ether, unsigned int ipv4, unsigned int mask) {
|
||||
if (ether < 0 || ether >= etherdev_count) {
|
||||
struct ether_t *card = ptr_vector_get(ðer_devs, ether);
|
||||
if (card == NULL) {
|
||||
return -1;
|
||||
}
|
||||
etherdevs[ether]->ipv4 = ipv4;
|
||||
etherdevs[ether]->mask = mask;
|
||||
card->ipv4 = ipv4;
|
||||
card->mask = mask;
|
||||
|
||||
if (etherdevs[ether]->arp_initialized == 0) {
|
||||
init_arp(etherdevs[ether]);
|
||||
if (card->arp_initialized == 0) {
|
||||
init_arp(card);
|
||||
}
|
||||
|
||||
if (etherdevs[ether]->state == 0) {
|
||||
if (etherdevs[ether]->type == 1) {
|
||||
i825xx_enable(etherdevs[ether]);
|
||||
if (card->state == 0) {
|
||||
if (card->type == 1) {
|
||||
i825xx_enable(card);
|
||||
}
|
||||
if (etherdevs[ether]->type == 2) {
|
||||
virtnet_enable(etherdevs[ether]);
|
||||
if (card->type == 2) {
|
||||
virtnet_enable(card);
|
||||
}
|
||||
}
|
||||
return etherdevs[ether]->state;
|
||||
return card->state;
|
||||
}
|
||||
|
||||
int ether_add_default_gw(int ether, unsigned int ipv4) {
|
||||
if (ether < 0 || ether >= etherdev_count) {
|
||||
struct ether_t *card = ptr_vector_get(ðer_devs, ether);
|
||||
if (card == NULL) {
|
||||
return -1;
|
||||
}
|
||||
etherdevs[ether]->default_gw = ipv4;
|
||||
card->default_gw = ipv4;
|
||||
|
||||
return 0;
|
||||
}
|
||||
|
||||
struct ether_t *ether_find_from_ipv4(unsigned int ipv4) {
|
||||
int i;
|
||||
struct ether_t *card;
|
||||
|
||||
for (i=0;i<etherdev_count;i++) {
|
||||
if ((ipv4 & etherdevs[i]->mask) == (etherdevs[i]->ipv4 & etherdevs[i]->mask) || etherdevs[i]->default_gw != 0) {
|
||||
return etherdevs[i];
|
||||
for (i=0;i<ptr_vector_len(ðer_devs);i++) {
|
||||
card = ptr_vector_get(ðer_devs, i);
|
||||
if ((ipv4 & card->mask) == (card->ipv4 & card->mask) || card->default_gw != 0) {
|
||||
return card;
|
||||
}
|
||||
}
|
||||
return (void *)0;
|
||||
return NULL;
|
||||
}
|
||||
|
||||
void init_ether() {
|
||||
struct ether_t *ether;
|
||||
int count;
|
||||
etherdev_count = 0;
|
||||
|
||||
init_ptr_vector(ðer_devs);
|
||||
|
||||
count = 0;
|
||||
ether = init_i825xx(count);
|
||||
|
15
i825xx.c
15
i825xx.c
@ -18,7 +18,7 @@
|
||||
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
|
||||
*/
|
||||
|
||||
|
||||
#include "pvec.h"
|
||||
#include "i825xx.h"
|
||||
#include "ether.h"
|
||||
#include "pci.h"
|
||||
@ -28,8 +28,8 @@
|
||||
#include "interrupts.h"
|
||||
#include "io.h"
|
||||
|
||||
extern struct ether_t **etherdevs;
|
||||
extern unsigned int etherdev_count;
|
||||
|
||||
extern struct ptr_vector ether_devs;
|
||||
|
||||
//Registers
|
||||
#define REG_CTRL 0x00000 //Control Register
|
||||
@ -235,10 +235,11 @@ static void i825xx_poll(struct i825xx_device_t *i825xx_device) {
|
||||
static void i825xx_isr(struct regs *r) {
|
||||
struct i825xx_device_t *i825xx_device;
|
||||
int i;
|
||||
|
||||
for (i=0;i<etherdev_count;i++) {
|
||||
if (etherdevs[i]->type == 1) {
|
||||
i825xx_device = (struct i825xx_device_t *)etherdevs[i]->data;
|
||||
|
||||
for (i=0;i<ptr_vector_len(ðer_devs);i++) {
|
||||
struct ether_t *card = ptr_vector_get(ðer_devs, i);
|
||||
if (card->type == 1) {
|
||||
i825xx_device = (struct i825xx_device_t *)card->data;
|
||||
//mmio_write(i825xx_device, REG_IMS, 0x1);
|
||||
//mmio_write(i825xx_device, REG_IMC, 0xffffffff);
|
||||
unsigned int icr = mmio_read(i825xx_device, REG_ICR);
|
||||
|
7
socket.c
7
socket.c
@ -11,8 +11,7 @@
|
||||
|
||||
struct ptr_vector sockets;
|
||||
|
||||
extern struct ether_t **etherdevs;
|
||||
extern unsigned int etherdev_count;
|
||||
extern struct ptr_vector ether_devs;
|
||||
|
||||
static unsigned char port_bitmap[8192];
|
||||
|
||||
@ -293,8 +292,8 @@ int socket_bind(struct socket_t* sock, unsigned int dest_ip, unsigned short dest
|
||||
|
||||
sock->ether = ether_find_from_ipv4(dest_ip);
|
||||
if (!sock->ether) {
|
||||
if (etherdev_count > 0) {
|
||||
sock->ether = etherdevs[0];
|
||||
if (ptr_vector_len(ðer_devs) > 0) {
|
||||
sock->ether = ptr_vector_get(ðer_devs, 0);
|
||||
} else {
|
||||
return -1;
|
||||
}
|
||||
|
12
virtnet.c
12
virtnet.c
@ -1,3 +1,4 @@
|
||||
#include "pvec.h"
|
||||
#include "ether.h"
|
||||
#include "virtio.h"
|
||||
#include "pci.h"
|
||||
@ -19,8 +20,7 @@ static __inline__ unsigned long round_up_to_page(unsigned long addr) {
|
||||
}
|
||||
|
||||
|
||||
extern struct ether_t **etherdevs;
|
||||
extern unsigned int etherdev_count;
|
||||
extern struct ptr_vector ether_devs;
|
||||
|
||||
void virtnet_poll(struct virtio_device_info *dev)
|
||||
{
|
||||
@ -120,9 +120,11 @@ int virtnet_send(struct virtio_device_info *dev, char *packet, int len) {
|
||||
void virtnet_isr(struct regs *r)
|
||||
{
|
||||
unsigned char v;
|
||||
for (int i=0;i<etherdev_count;i++) {
|
||||
if (etherdevs[i]->type == 2) {
|
||||
struct virtio_device_info* virtio_dev = (struct virtio_device_info *)etherdevs[i]->data;
|
||||
for (int i=0;i<ptr_vector_len(ðer_devs);i++) {
|
||||
struct ether_t *card = ptr_vector_get(ðer_devs, i);
|
||||
|
||||
if (card->type == 2) {
|
||||
struct virtio_device_info* virtio_dev = (struct virtio_device_info *)card->data;
|
||||
if (virtio_dev->iobase==0) continue;
|
||||
|
||||
v = inportb(virtio_dev->iobase + 0x13);
|
||||
|
Loading…
x
Reference in New Issue
Block a user