Commit 7c9d8e07 authored by bellard's avatar bellard

new network emulation


git-svn-id: svn://svn.savannah.nongnu.org/qemu/trunk@1622 c046a42c-6fe2-441c-8c8c-71466251a162
parent 868bfe2b
......@@ -704,19 +704,19 @@ void gdb_exit(CPUState *env, int code)
}
#else
static int gdb_can_read(void *opaque)
{
return 256;
}
static void gdb_read(void *opaque, const uint8_t *buf, int size)
static void gdb_read(void *opaque)
{
GDBState *s = opaque;
int i;
int i, size;
uint8_t buf[4096];
size = read(s->fd, buf, sizeof(buf));
if (size < 0)
return;
if (size == 0) {
/* end of connection */
qemu_del_vm_stop_handler(gdb_vm_stopped, s);
qemu_del_fd_read_handler(s->fd);
qemu_set_fd_handler(s->fd, NULL, NULL, NULL);
qemu_free(s);
vm_start();
} else {
......@@ -727,7 +727,7 @@ static void gdb_read(void *opaque, const uint8_t *buf, int size)
#endif
static void gdb_accept(void *opaque, const uint8_t *buf, int size)
static void gdb_accept(void *opaque)
{
GDBState *s;
struct sockaddr_in sockaddr;
......@@ -768,7 +768,7 @@ static void gdb_accept(void *opaque, const uint8_t *buf, int size)
vm_stop(EXCP_INTERRUPT);
/* start handling I/O */
qemu_add_fd_read_handler(s->fd, gdb_can_read, gdb_read, s);
qemu_set_fd_handler(s->fd, gdb_read, NULL, s);
/* when the VM is stopped, the following callback is called */
qemu_add_vm_stop_handler(gdb_vm_stopped, s);
#endif
......@@ -815,9 +815,9 @@ int gdbserver_start(int port)
return -1;
/* accept connections */
#ifdef CONFIG_USER_ONLY
gdb_accept (NULL, NULL, 0);
gdb_accept (NULL);
#else
qemu_add_fd_read_handler(gdbserver_fd, NULL, gdb_accept, NULL);
qemu_set_fd_handler(gdbserver_fd, gdb_accept, NULL, NULL);
#endif
return 0;
}
......@@ -154,7 +154,8 @@ struct lance_init_block {
#define LEDMA_MAXADDR (LEDMA_REGS * 4 - 1)
typedef struct LANCEState {
NetDriverState *nd;
VLANClientState *vc;
uint8_t macaddr[6]; /* init mac address */
uint32_t leptr;
uint16_t addr;
uint16_t regs[LE_NREGS];
......@@ -169,7 +170,7 @@ static void lance_send(void *opaque);
static void lance_reset(void *opaque)
{
LANCEState *s = opaque;
memcpy(s->phys, s->nd->macaddr, 6);
memcpy(s->phys, s->macaddr, 6);
s->rxptr = 0;
s->txptr = 0;
memset(s->regs, 0, LE_NREGS * 2);
......@@ -280,31 +281,6 @@ static CPUWriteMemoryFunc *lance_mem_write[3] = {
};
/* return the max buffer size if the LANCE can receive more data */
static int lance_can_receive(void *opaque)
{
LANCEState *s = opaque;
uint32_t dmaptr = s->leptr + s->ledmaregs[3];
struct lance_init_block *ib;
int i;
uint8_t temp8;
if ((s->regs[LE_CSR0] & LE_C0_STOP) == LE_C0_STOP)
return 0;
ib = (void *) iommu_translate(dmaptr);
for (i = 0; i < RX_RING_SIZE; i++) {
cpu_physical_memory_read((uint32_t)&ib->brx_ring[i].rmd1_bits, (void *) &temp8, 1);
if (temp8 == (LE_R1_OWN)) {
DPRINTF("can receive %d\n", RX_BUFF_SIZE);
return RX_BUFF_SIZE;
}
}
DPRINTF("cannot receive\n");
return 0;
}
#define MIN_BUF_SIZE 60
static void lance_receive(void *opaque, const uint8_t *buf, int size)
......@@ -368,7 +344,7 @@ static void lance_send(void *opaque)
temp16 = (~temp16) + 1;
cpu_physical_memory_read((uint32_t)&ib->tx_buf[i], pkt_buf, temp16);
DPRINTF("sending packet, len %d\n", temp16);
qemu_send_packet(s->nd, pkt_buf, temp16);
qemu_send_packet(s->vc, pkt_buf, temp16);
temp8 = LE_T1_POK;
cpu_physical_memory_write((uint32_t)&ib->btx_ring[i].tmd1_bits, (void *) &temp8, 1);
s->txptr = (s->txptr + 1) & TX_RING_MOD_MASK;
......@@ -443,7 +419,7 @@ static int lance_load(QEMUFile *f, void *opaque, int version_id)
return 0;
}
void lance_init(NetDriverState *nd, int irq, uint32_t leaddr, uint32_t ledaddr)
void lance_init(NICInfo *nd, int irq, uint32_t leaddr, uint32_t ledaddr)
{
LANCEState *s;
int lance_io_memory, ledma_io_memory;
......@@ -452,7 +428,6 @@ void lance_init(NetDriverState *nd, int irq, uint32_t leaddr, uint32_t ledaddr)
if (!s)
return;
s->nd = nd;
s->irq = irq;
lance_io_memory = cpu_register_io_memory(0, lance_mem_read, lance_mem_write, s);
......@@ -461,8 +436,21 @@ void lance_init(NetDriverState *nd, int irq, uint32_t leaddr, uint32_t ledaddr)
ledma_io_memory = cpu_register_io_memory(0, ledma_mem_read, ledma_mem_write, s);
cpu_register_physical_memory(ledaddr, 16, ledma_io_memory);
memcpy(s->macaddr, nd->macaddr, 6);
lance_reset(s);
qemu_add_read_packet(nd, lance_can_receive, lance_receive, s);
s->vc = qemu_new_vlan_client(nd->vlan, lance_receive, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
"lance macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
s->macaddr[0],
s->macaddr[1],
s->macaddr[2],
s->macaddr[3],
s->macaddr[4],
s->macaddr[5]);
register_savevm("lance", leaddr, 1, lance_save, lance_load, s);
qemu_register_reset(lance_reset, s);
}
......
......@@ -122,6 +122,7 @@ typedef struct NE2000State {
uint16_t rcnt;
uint32_t rsar;
uint8_t rsr;
uint8_t rxcr;
uint8_t isr;
uint8_t dcfg;
uint8_t imr;
......@@ -130,7 +131,8 @@ typedef struct NE2000State {
uint8_t mult[8]; /* multicast mask array */
int irq;
PCIDevice *pci_dev;
NetDriverState *nd;
VLANClientState *vc;
uint8_t macaddr[6];
uint8_t mem[NE2000_MEM_SIZE];
} NE2000State;
......@@ -139,7 +141,7 @@ static void ne2000_reset(NE2000State *s)
int i;
s->isr = ENISR_RESET;
memcpy(s->mem, s->nd->macaddr, 6);
memcpy(s->mem, s->macaddr, 6);
s->mem[14] = 0x57;
s->mem[15] = 0x57;
......@@ -167,6 +169,30 @@ static void ne2000_update_irq(NE2000State *s)
}
}
#define POLYNOMIAL 0x04c11db6
/* From FreeBSD */
/* XXX: optimize */
static int compute_mcast_idx(const uint8_t *ep)
{
uint32_t crc;
int carry, i, j;
uint8_t b;
crc = 0xffffffff;
for (i = 0; i < 6; i++) {
b = *ep++;
for (j = 0; j < 8; j++) {
carry = ((crc & 0x80000000L) ? 1 : 0) ^ (b & 0x01);
crc <<= 1;
b >>= 1;
if (carry)
crc = ((crc ^ POLYNOMIAL) | carry);
}
}
return (crc >> 26);
}
/* return the max buffer size if the NE2000 can receive more data */
static int ne2000_can_receive(void *opaque)
{
......@@ -192,13 +218,46 @@ static void ne2000_receive(void *opaque, const uint8_t *buf, int size)
{
NE2000State *s = opaque;
uint8_t *p;
int total_len, next, avail, len, index;
int total_len, next, avail, len, index, mcast_idx;
uint8_t buf1[60];
static const uint8_t broadcast_macaddr[6] =
{ 0xff, 0xff, 0xff, 0xff, 0xff, 0xff };
#if defined(DEBUG_NE2000)
printf("NE2000: received len=%d\n", size);
#endif
if (!ne2000_can_receive(s))
return;
/* XXX: check this */
if (s->rxcr & 0x10) {
/* promiscuous: receive all */
} else {
if (!memcmp(buf, broadcast_macaddr, 6)) {
/* broadcast address */
if (!(s->rxcr & 0x04))
return;
} else if (buf[0] & 0x01) {
/* multicast */
if (!(s->rxcr & 0x08))
return;
mcast_idx = compute_mcast_idx(buf);
if (!(s->mult[mcast_idx >> 3] & (1 << (mcast_idx & 7))))
return;
} else if (s->mem[0] == buf[0] &&
s->mem[2] == buf[1] &&
s->mem[4] == buf[2] &&
s->mem[6] == buf[3] &&
s->mem[8] == buf[4] &&
s->mem[10] == buf[5]) {
/* match */
} else {
return;
}
}
/* if too small buffer, then expand it */
if (size < MIN_BUF_SIZE) {
memcpy(buf1, buf, size);
......@@ -273,7 +332,7 @@ static void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
index -= NE2000_PMEM_SIZE;
/* fail safe: check range on the transmitted length */
if (index + s->tcnt <= NE2000_PMEM_END) {
qemu_send_packet(s->nd, s->mem + index, s->tcnt);
qemu_send_packet(s->vc, s->mem + index, s->tcnt);
}
/* signal end of transfert */
s->tsr = ENTSR_PTX;
......@@ -320,6 +379,9 @@ static void ne2000_ioport_write(void *opaque, uint32_t addr, uint32_t val)
case EN0_RCNTHI:
s->rcnt = (s->rcnt & 0x00ff) | (val << 8);
break;
case EN0_RXCR:
s->rxcr = val;
break;
case EN0_DCFG:
s->dcfg = val;
break;
......@@ -608,10 +670,10 @@ static int ne2000_load(QEMUFile* f,void* opaque,int version_id)
return 0;
}
void isa_ne2000_init(int base, int irq, NetDriverState *nd)
void isa_ne2000_init(int base, int irq, NICInfo *nd)
{
NE2000State *s;
s = qemu_mallocz(sizeof(NE2000State));
if (!s)
return;
......@@ -627,14 +689,22 @@ void isa_ne2000_init(int base, int irq, NetDriverState *nd)
register_ioport_write(base + 0x1f, 1, 1, ne2000_reset_ioport_write, s);
register_ioport_read(base + 0x1f, 1, 1, ne2000_reset_ioport_read, s);
s->irq = irq;
s->nd = nd;
memcpy(s->macaddr, nd->macaddr, 6);
ne2000_reset(s);
qemu_add_read_packet(nd, ne2000_can_receive, ne2000_receive, s);
s->vc = qemu_new_vlan_client(nd->vlan, ne2000_receive, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
"ne2000 macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
s->macaddr[0],
s->macaddr[1],
s->macaddr[2],
s->macaddr[3],
s->macaddr[4],
s->macaddr[5]);
register_savevm("ne2000", 0, 1, ne2000_save, ne2000_load, s);
}
/***********************************************************/
......@@ -665,7 +735,7 @@ static void ne2000_map(PCIDevice *pci_dev, int region_num,
register_ioport_read(addr + 0x1f, 1, 1, ne2000_reset_ioport_read, s);
}
void pci_ne2000_init(PCIBus *bus, NetDriverState *nd)
void pci_ne2000_init(PCIBus *bus, NICInfo *nd)
{
PCINE2000State *d;
NE2000State *s;
......@@ -690,10 +760,19 @@ void pci_ne2000_init(PCIBus *bus, NetDriverState *nd)
s = &d->ne2000;
s->irq = 16; // PCI interrupt
s->pci_dev = (PCIDevice *)d;
s->nd = nd;
memcpy(s->macaddr, nd->macaddr, 6);
ne2000_reset(s);
qemu_add_read_packet(nd, ne2000_can_receive, ne2000_receive, s);
s->vc = qemu_new_vlan_client(nd->vlan, ne2000_receive, s);
snprintf(s->vc->info_str, sizeof(s->vc->info_str),
"ne2000 pci macaddr=%02x:%02x:%02x:%02x:%02x:%02x",
s->macaddr[0],
s->macaddr[1],
s->macaddr[2],
s->macaddr[3],
s->macaddr[4],
s->macaddr[5]);
/* XXX: instance number ? */
register_savevm("ne2000", 0, 1, ne2000_save, ne2000_load, s);
register_savevm("ne2000_pci", 0, 1, generic_pci_save, generic_pci_load,
......
......@@ -154,27 +154,6 @@ static uint32_t parallel_ioport_read(void *opaque, uint32_t addr)
return ret;
}
static int parallel_can_receive(ParallelState *s)
{
return 0;
}
static void parallel_receive_byte(ParallelState *s, int ch)
{
}
static int parallel_can_receive1(void *opaque)
{
ParallelState *s = opaque;
return parallel_can_receive(s);
}
static void parallel_receive1(void *opaque, const uint8_t *buf, int size)
{
ParallelState *s = opaque;
parallel_receive_byte(s, buf[0]);
}
/* If fd is zero, it means that the parallel device uses the console */
ParallelState *parallel_init(int base, int irq, CharDriverState *chr)
{
......@@ -200,6 +179,5 @@ ParallelState *parallel_init(int base, int irq, CharDriverState *chr)
register_ioport_write(base, 8, 1, parallel_ioport_write, s);
register_ioport_read(base, 8, 1, parallel_ioport_read, s);
qemu_chr_add_read_handler(chr, parallel_can_receive1, parallel_receive1, s);
return s;
}
......@@ -196,23 +196,6 @@ static void do_info_version(void)
term_printf("%s\n", QEMU_VERSION);
}
static void do_info_network(void)
{
int i, j;
NetDriverState *nd;
for(i = 0; i < nb_nics; i++) {
nd = &nd_table[i];
term_printf("%d: ifname=%s macaddr=", i, nd->ifname);
for(j = 0; j < 6; j++) {
if (j > 0)
term_printf(":");
term_printf("%02x", nd->macaddr[j]);
}
term_printf("\n");
}
}
static void do_info_block(void)
{
bdrv_info();
......
This diff is collapsed.
......@@ -196,10 +196,17 @@ void kbd_put_keysym(int keysym);
typedef void IOReadHandler(void *opaque, const uint8_t *buf, int size);
typedef int IOCanRWHandler(void *opaque);
typedef void IOHandler(void *opaque);
int qemu_add_fd_read_handler(int fd, IOCanRWHandler *fd_can_read,
IOReadHandler *fd_read, void *opaque);
void qemu_del_fd_read_handler(int fd);
int qemu_set_fd_handler2(int fd,
IOCanRWHandler *fd_read_poll,
IOHandler *fd_read,
IOHandler *fd_write,
void *opaque);
int qemu_set_fd_handler(int fd,
IOHandler *fd_read,
IOHandler *fd_write,
void *opaque);
/* character device */
......@@ -270,30 +277,42 @@ extern CharDriverState *serial_hds[MAX_SERIAL_PORTS];
extern CharDriverState *parallel_hds[MAX_PARALLEL_PORTS];
/* network redirectors support */
/* VLANs support */
typedef struct VLANClientState VLANClientState;
struct VLANClientState {
IOReadHandler *fd_read;
void *opaque;
struct VLANClientState *next;
struct VLANState *vlan;
char info_str[256];
};
typedef struct VLANState {
int id;
VLANClientState *first_client;
struct VLANState *next;
} VLANState;
VLANState *qemu_find_vlan(int id);
VLANClientState *qemu_new_vlan_client(VLANState *vlan,
IOReadHandler *fd_read, void *opaque);
void qemu_send_packet(VLANClientState *vc, const uint8_t *buf, int size);
void do_info_network(void);
/* NIC info */
#define MAX_NICS 8
typedef struct NetDriverState {
int index; /* index number in QEMU */
typedef struct NICInfo {
uint8_t macaddr[6];
char ifname[16];
void (*send_packet)(struct NetDriverState *nd,
const uint8_t *buf, int size);
void (*add_read_packet)(struct NetDriverState *nd,
IOCanRWHandler *fd_can_read,
IOReadHandler *fd_read, void *opaque);
/* tun specific data */
int fd;
/* slirp specific data */
} NetDriverState;
VLANState *vlan;
} NICInfo;
extern int nb_nics;
extern NetDriverState nd_table[MAX_NICS];
void qemu_send_packet(NetDriverState *nd, const uint8_t *buf, int size);
void qemu_add_read_packet(NetDriverState *nd, IOCanRWHandler *fd_can_read,
IOReadHandler *fd_read, void *opaque);
extern NICInfo nd_table[MAX_NICS];
/* timers */
......@@ -692,8 +711,8 @@ int fdctrl_get_drive_type(fdctrl_t *fdctrl, int drive_num);
/* ne2000.c */
void isa_ne2000_init(int base, int irq, NetDriverState *nd);
void pci_ne2000_init(PCIBus *bus, NetDriverState *nd);
void isa_ne2000_init(int base, int irq, NICInfo *nd);
void pci_ne2000_init(PCIBus *bus, NICInfo *nd);
/* pckbd.c */
......@@ -781,7 +800,7 @@ void *iommu_init(uint32_t addr);
uint32_t iommu_translate_local(void *opaque, uint32_t addr);
/* lance.c */
void lance_init(NetDriverState *nd, int irq, uint32_t leaddr, uint32_t ledaddr);
void lance_init(NICInfo *nd, int irq, uint32_t leaddr, uint32_t ledaddr);
/* tcx.c */
void *tcx_init(DisplayState *ds, uint32_t addr, uint8_t *vram_base,
......
Markdown is supported
0% or .
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment