2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* 86Box A hypervisor and IBM PC system emulator that specializes in
|
|
|
|
|
* running old operating systems and software designed for IBM
|
|
|
|
|
* PC systems and compatibles from 1981 through fairly recent
|
|
|
|
|
* system designs based on the PCI bus.
|
|
|
|
|
*
|
|
|
|
|
* This file is part of the 86Box distribution.
|
|
|
|
|
*
|
|
|
|
|
* Handle SLiRP library processing.
|
|
|
|
|
*
|
2017-10-29 04:20:20 -05:00
|
|
|
* Version: @(#)net_slirp.c 1.0.11 2017/10/28
|
2017-05-12 05:05:20 -04:00
|
|
|
*
|
|
|
|
|
* Author: Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-11 05:40:44 -04:00
|
|
|
*
|
|
|
|
|
* Copyright 2017 Fred N. van Kempen.
|
2017-05-12 05:05:20 -04:00
|
|
|
*/
|
|
|
|
|
#include <stdio.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdint.h>
|
2017-05-12 05:05:20 -04:00
|
|
|
#include <string.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wchar.h>
|
2017-08-27 04:33:47 +01:00
|
|
|
#include "slirp/slirp.h"
|
|
|
|
|
#include "slirp/queue.h"
|
2017-10-17 01:59:09 -04:00
|
|
|
#include "../86box.h"
|
2017-06-14 07:21:01 +02:00
|
|
|
#include "../ibm.h"
|
|
|
|
|
#include "../config.h"
|
|
|
|
|
#include "../device.h"
|
2017-10-11 05:40:44 -04:00
|
|
|
#include "../plat.h"
|
2017-10-10 03:07:29 -04:00
|
|
|
#include "network.h"
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
static volatile queueADT slirpq; /* SLiRP library handle */
|
|
|
|
|
static volatile thread_t *poll_tid;
|
|
|
|
|
static netcard_t *poll_card; /* netcard attached to us */
|
|
|
|
|
static event_t *poll_state;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
static void
|
|
|
|
|
slirp_tic(void)
|
|
|
|
|
{
|
2017-05-18 01:57:16 -04:00
|
|
|
int ret2, nfds;
|
2017-05-12 05:05:20 -04:00
|
|
|
struct timeval tv;
|
|
|
|
|
fd_set rfds, wfds, xfds;
|
|
|
|
|
int tmo;
|
|
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Let SLiRP create a list of all open sockets. */
|
|
|
|
|
nfds = -1;
|
2017-05-12 05:05:20 -04:00
|
|
|
FD_ZERO(&rfds);
|
|
|
|
|
FD_ZERO(&wfds);
|
|
|
|
|
FD_ZERO(&xfds);
|
|
|
|
|
tmo = slirp_select_fill(&nfds, &rfds, &wfds, &xfds); /* this can crash */
|
2017-05-18 01:57:16 -04:00
|
|
|
if (tmo < 0)
|
2017-05-12 05:05:20 -04:00
|
|
|
tmo = 500;
|
|
|
|
|
|
|
|
|
|
tv.tv_sec = 0;
|
2017-05-18 01:57:16 -04:00
|
|
|
tv.tv_usec = tmo;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Now wait for something to happen, or at most 'tmo' usec. */
|
2017-05-12 05:05:20 -04:00
|
|
|
ret2 = select(nfds+1, &rfds, &wfds, &xfds, &tv);
|
2017-05-18 01:57:16 -04:00
|
|
|
|
|
|
|
|
/* If something happened, let SLiRP handle it. */
|
|
|
|
|
if (ret2 >= 0)
|
2017-05-12 05:05:20 -04:00
|
|
|
slirp_select_poll(&rfds, &wfds, &xfds);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-22 01:08:10 -04:00
|
|
|
/* Handle the receiving of frames. */
|
2017-05-12 05:05:20 -04:00
|
|
|
static void
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_thread(UNUSED(void *arg))
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
|
|
|
|
struct queuepacket *qp;
|
|
|
|
|
event_t *evt;
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
pclog("SLiRP: polling started.\n");
|
|
|
|
|
thread_set_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
/* Create a waitable event. */
|
|
|
|
|
evt = thread_create_event();
|
|
|
|
|
|
|
|
|
|
while (slirpq != NULL) {
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Request ownership of the queue. */
|
|
|
|
|
network_wait(1);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Wait for a poll request. */
|
|
|
|
|
network_poll();
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* See if there is any work. */
|
|
|
|
|
slirp_tic();
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
/* Wait for the next packet to arrive. */
|
2017-10-29 04:20:20 -05:00
|
|
|
if (QueuePeek(slirpq) != 0) {
|
|
|
|
|
/* Grab a packet from the queue. */
|
|
|
|
|
qp = QueueDelete(slirpq);
|
2017-05-22 01:08:10 -04:00
|
|
|
#if 0
|
2017-10-29 04:20:20 -05:00
|
|
|
pclog("SLiRP: inQ:%d got a %dbyte packet @%08lx\n",
|
2017-05-12 05:05:20 -04:00
|
|
|
QueuePeek(slirpq), qp->len, qp);
|
2017-05-22 01:08:10 -04:00
|
|
|
#endif
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_card->rx(poll_card->priv, (uint8_t *)qp->data, qp->len);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Done with this one. */
|
|
|
|
|
free(qp);
|
|
|
|
|
} else {
|
|
|
|
|
/* If we did not get anything, wait a while. */
|
|
|
|
|
thread_wait_event(evt, 10);
|
|
|
|
|
}
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Release ownership of the queue. */
|
|
|
|
|
network_wait(0);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* No longer needed. */
|
2017-05-12 05:05:20 -04:00
|
|
|
thread_destroy_event(evt);
|
|
|
|
|
|
|
|
|
|
pclog("SLiRP: polling stopped.\n");
|
2017-10-29 04:20:20 -05:00
|
|
|
thread_set_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Initialize SLiRP for use. */
|
2017-05-12 05:05:20 -04:00
|
|
|
int
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_init(void)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
2017-05-22 01:08:10 -04:00
|
|
|
pclog("SLiRP: initializing..\n");
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
if (slirp_init() != 0) {
|
|
|
|
|
pclog("SLiRP could not be initialized!\n");
|
|
|
|
|
return(-1);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
slirpq = QueueCreate();
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_tid = NULL;
|
|
|
|
|
poll_state = NULL;
|
|
|
|
|
poll_card = NULL;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
return(0);
|
|
|
|
|
}
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-28 07:33:08 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Initialize SLiRP for use. */
|
|
|
|
|
int
|
|
|
|
|
net_slirp_reset(netcard_t *card)
|
|
|
|
|
{
|
|
|
|
|
/* Save the callback info. */
|
|
|
|
|
poll_card = card;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
pclog("SLiRP: creating thread..\n");
|
|
|
|
|
poll_state = thread_create_event();
|
|
|
|
|
poll_tid = thread_create(poll_thread, card->mac);
|
|
|
|
|
thread_wait_event(poll_state, -1);
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_close(void)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
|
|
|
|
queueADT sl;
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
if (slirpq == NULL) return;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
pclog("SLiRP: closing.\n");
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Tell the polling thread to shut down. */
|
|
|
|
|
sl = slirpq; slirpq = NULL;
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Tell the thread to terminate. */
|
|
|
|
|
if (poll_tid != NULL) {
|
|
|
|
|
network_busy(0);
|
2017-10-19 23:55:51 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Wait for the thread to finish. */
|
|
|
|
|
pclog("SLiRP: waiting for thread to end...\n");
|
|
|
|
|
thread_wait_event(poll_state, -1);
|
|
|
|
|
pclog("SLiRP: thread ended\n");
|
|
|
|
|
thread_destroy_event(poll_state);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
poll_tid = NULL;
|
|
|
|
|
poll_state = NULL;
|
|
|
|
|
poll_card = NULL;
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* OK, now shut down SLiRP itself. */
|
|
|
|
|
QueueDestroy(sl);
|
|
|
|
|
slirp_exit(0);
|
2017-08-22 02:16:15 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Send a packet to the SLiRP interface. */
|
|
|
|
|
void
|
2017-10-29 04:20:20 -05:00
|
|
|
net_slirp_in(uint8_t *pkt, int pkt_len)
|
2017-05-12 05:05:20 -04:00
|
|
|
{
|
2017-10-29 04:20:20 -05:00
|
|
|
if (slirpq == NULL) return;
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
network_busy(1);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
slirp_input((const uint8_t *)pkt, pkt_len);
|
|
|
|
|
|
|
|
|
|
network_busy(0);
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Needed by SLiRP library. */
|
2017-05-12 05:05:20 -04:00
|
|
|
void
|
|
|
|
|
slirp_output(const uint8_t *pkt, int pkt_len)
|
|
|
|
|
{
|
|
|
|
|
struct queuepacket *qp;
|
|
|
|
|
|
|
|
|
|
if (slirpq != NULL) {
|
|
|
|
|
qp = (struct queuepacket *)malloc(sizeof(struct queuepacket));
|
|
|
|
|
qp->len = pkt_len;
|
|
|
|
|
memcpy(qp->data, pkt, pkt_len);
|
|
|
|
|
QueueEnter(slirpq, qp);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
/* Needed by SLiRP library. */
|
2017-05-12 05:05:20 -04:00
|
|
|
int
|
|
|
|
|
slirp_can_output(void)
|
|
|
|
|
{
|
|
|
|
|
return((slirpq != NULL)?1:0);
|
|
|
|
|
}
|