etherdevs -> ptr_vector

This commit is contained in:
Andrew Pamment 2021-12-22 20:37:06 +10:00
parent e0f7cea9c9
commit 9a6881f3b9
4 changed files with 54 additions and 50 deletions

70
ether.c
View File

@ -1,3 +1,4 @@
#include "pvec.h"
#include "arp.h" #include "arp.h"
#include "ether.h" #include "ether.h"
#include "ipv4.h" #include "ipv4.h"
@ -5,6 +6,7 @@
#include "memory.h" #include "memory.h"
#include "virtio.h" #include "virtio.h"
struct ipv4_header_t { struct ipv4_header_t {
unsigned char vhl; unsigned char vhl;
unsigned char tos; unsigned char tos;
@ -20,17 +22,10 @@ struct ipv4_header_t {
}__attribute__((packed)); }__attribute__((packed));
struct ptr_vector ether_devs;
struct ether_t **etherdevs;
unsigned int etherdev_count = 0;
static int add_ether(struct ether_t *card) { static int add_ether(struct ether_t *card) {
if (!etherdev_count) { ptr_vector_append(&ether_devs, card);
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;
return 1; 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) { int ether_get_mac(int ether, unsigned char *dest) {
if (ether < 0 || ether >= etherdev_count) { struct ether_t *card = ptr_vector_get(&ether_devs, ether);
if (card == NULL) {
return -1; return -1;
} }
dest[0] = etherdevs[ether]->mac[0]; dest[0] = card->mac[0];
dest[1] = etherdevs[ether]->mac[1]; dest[1] = card->mac[1];
dest[2] = etherdevs[ether]->mac[2]; dest[2] = card->mac[2];
dest[3] = etherdevs[ether]->mac[3]; dest[3] = card->mac[3];
dest[4] = etherdevs[ether]->mac[4]; dest[4] = card->mac[4];
dest[5] = etherdevs[ether]->mac[5]; dest[5] = card->mac[5];
return 0; return 0;
} }
int ether_enable(int ether, unsigned int ipv4, unsigned int mask) { int ether_enable(int ether, unsigned int ipv4, unsigned int mask) {
if (ether < 0 || ether >= etherdev_count) { struct ether_t *card = ptr_vector_get(&ether_devs, ether);
if (card == NULL) {
return -1; return -1;
} }
etherdevs[ether]->ipv4 = ipv4; card->ipv4 = ipv4;
etherdevs[ether]->mask = mask; card->mask = mask;
if (etherdevs[ether]->arp_initialized == 0) { if (card->arp_initialized == 0) {
init_arp(etherdevs[ether]); init_arp(card);
} }
if (etherdevs[ether]->state == 0) { if (card->state == 0) {
if (etherdevs[ether]->type == 1) { if (card->type == 1) {
i825xx_enable(etherdevs[ether]); i825xx_enable(card);
} }
if (etherdevs[ether]->type == 2) { if (card->type == 2) {
virtnet_enable(etherdevs[ether]); virtnet_enable(card);
} }
} }
return etherdevs[ether]->state; return card->state;
} }
int ether_add_default_gw(int ether, unsigned int ipv4) { int ether_add_default_gw(int ether, unsigned int ipv4) {
if (ether < 0 || ether >= etherdev_count) { struct ether_t *card = ptr_vector_get(&ether_devs, ether);
if (card == NULL) {
return -1; return -1;
} }
etherdevs[ether]->default_gw = ipv4; card->default_gw = ipv4;
return 0; return 0;
} }
struct ether_t *ether_find_from_ipv4(unsigned int ipv4) { struct ether_t *ether_find_from_ipv4(unsigned int ipv4) {
int i; int i;
struct ether_t *card;
for (i=0;i<etherdev_count;i++) { for (i=0;i<ptr_vector_len(&ether_devs);i++) {
if ((ipv4 & etherdevs[i]->mask) == (etherdevs[i]->ipv4 & etherdevs[i]->mask) || etherdevs[i]->default_gw != 0) { card = ptr_vector_get(&ether_devs, i);
return etherdevs[i]; if ((ipv4 & card->mask) == (card->ipv4 & card->mask) || card->default_gw != 0) {
return card;
} }
} }
return (void *)0; return NULL;
} }
void init_ether() { void init_ether() {
struct ether_t *ether; struct ether_t *ether;
int count; int count;
etherdev_count = 0;
init_ptr_vector(&ether_devs);
count = 0; count = 0;
ether = init_i825xx(count); ether = init_i825xx(count);

View File

@ -18,7 +18,7 @@
* OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE. * OR IN CONNECTION WITH THE USE OR PERFORMANCE OF THIS SOFTWARE.
*/ */
#include "pvec.h"
#include "i825xx.h" #include "i825xx.h"
#include "ether.h" #include "ether.h"
#include "pci.h" #include "pci.h"
@ -28,8 +28,8 @@
#include "interrupts.h" #include "interrupts.h"
#include "io.h" #include "io.h"
extern struct ether_t **etherdevs;
extern unsigned int etherdev_count; extern struct ptr_vector ether_devs;
//Registers //Registers
#define REG_CTRL 0x00000 //Control Register #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) { static void i825xx_isr(struct regs *r) {
struct i825xx_device_t *i825xx_device; struct i825xx_device_t *i825xx_device;
int i; int i;
for (i=0;i<etherdev_count;i++) { for (i=0;i<ptr_vector_len(&ether_devs);i++) {
if (etherdevs[i]->type == 1) { struct ether_t *card = ptr_vector_get(&ether_devs, i);
i825xx_device = (struct i825xx_device_t *)etherdevs[i]->data; if (card->type == 1) {
i825xx_device = (struct i825xx_device_t *)card->data;
//mmio_write(i825xx_device, REG_IMS, 0x1); //mmio_write(i825xx_device, REG_IMS, 0x1);
//mmio_write(i825xx_device, REG_IMC, 0xffffffff); //mmio_write(i825xx_device, REG_IMC, 0xffffffff);
unsigned int icr = mmio_read(i825xx_device, REG_ICR); unsigned int icr = mmio_read(i825xx_device, REG_ICR);

View File

@ -11,8 +11,7 @@
struct ptr_vector sockets; struct ptr_vector sockets;
extern struct ether_t **etherdevs; extern struct ptr_vector ether_devs;
extern unsigned int etherdev_count;
static unsigned char port_bitmap[8192]; 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); sock->ether = ether_find_from_ipv4(dest_ip);
if (!sock->ether) { if (!sock->ether) {
if (etherdev_count > 0) { if (ptr_vector_len(&ether_devs) > 0) {
sock->ether = etherdevs[0]; sock->ether = ptr_vector_get(&ether_devs, 0);
} else { } else {
return -1; return -1;
} }

View File

@ -1,3 +1,4 @@
#include "pvec.h"
#include "ether.h" #include "ether.h"
#include "virtio.h" #include "virtio.h"
#include "pci.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 struct ptr_vector ether_devs;
extern unsigned int etherdev_count;
void virtnet_poll(struct virtio_device_info *dev) 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) void virtnet_isr(struct regs *r)
{ {
unsigned char v; unsigned char v;
for (int i=0;i<etherdev_count;i++) { for (int i=0;i<ptr_vector_len(&ether_devs);i++) {
if (etherdevs[i]->type == 2) { struct ether_t *card = ptr_vector_get(&ether_devs, i);
struct virtio_device_info* virtio_dev = (struct virtio_device_info *)etherdevs[i]->data;
if (card->type == 2) {
struct virtio_device_info* virtio_dev = (struct virtio_device_info *)card->data;
if (virtio_dev->iobase==0) continue; if (virtio_dev->iobase==0) continue;
v = inportb(virtio_dev->iobase + 0x13); v = inportb(virtio_dev->iobase + 0x13);