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-14 20:04:21 -04:00
|
|
|
* Version: @(#)net_slirp.c 1.0.9 2017/10/14
|
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-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
|
|
|
|
|
|
|
|
|
|
|
|
|
static queueADT slirpq; /* SLiRP library handle */
|
|
|
|
|
static thread_t *poll_tid;
|
|
|
|
|
static NETRXCB poll_rx; /* network RX function to call */
|
|
|
|
|
static void *poll_arg; /* network RX function arg */
|
2017-10-14 20:04:21 -04:00
|
|
|
static mutex_t *slirpMutex;
|
2017-10-14 07:03:19 +02:00
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
startslirp(void)
|
|
|
|
|
{
|
|
|
|
|
thread_wait_mutex(slirpMutex);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
endslirp(void)
|
|
|
|
|
{
|
|
|
|
|
thread_release_mutex(slirpMutex);
|
|
|
|
|
}
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Instead of calling this and crashing some times
|
|
|
|
|
or experencing jitter, this is called by the
|
|
|
|
|
60Hz clock which seems to do the job. */
|
|
|
|
|
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-09-08 16:35:14 +02:00
|
|
|
static struct
|
|
|
|
|
{
|
|
|
|
|
int busy;
|
|
|
|
|
int queue_in_use;
|
|
|
|
|
|
|
|
|
|
event_t *wake_poll_thread;
|
|
|
|
|
event_t *poll_complete;
|
|
|
|
|
event_t *queue_not_in_use;
|
|
|
|
|
} poll_data;
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void network_slirp_wait_for_poll()
|
|
|
|
|
{
|
|
|
|
|
while (poll_data.busy)
|
|
|
|
|
thread_wait_event(poll_data.poll_complete, -1);
|
|
|
|
|
thread_reset_event(poll_data.poll_complete);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-22 01:08:10 -04:00
|
|
|
/* Handle the receiving of frames. */
|
2017-05-12 05:05:20 -04:00
|
|
|
static void
|
|
|
|
|
poll_thread(void *arg)
|
|
|
|
|
{
|
|
|
|
|
struct queuepacket *qp;
|
|
|
|
|
event_t *evt;
|
|
|
|
|
|
|
|
|
|
pclog("SLiRP: polling thread started, arg %08lx\n", arg);
|
|
|
|
|
|
|
|
|
|
/* Create a waitable event. */
|
|
|
|
|
evt = thread_create_event();
|
|
|
|
|
|
|
|
|
|
while (slirpq != NULL) {
|
2017-09-08 16:35:14 +02:00
|
|
|
startslirp();
|
|
|
|
|
|
|
|
|
|
network_slirp_wait_for_poll();
|
|
|
|
|
|
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. */
|
|
|
|
|
if (QueuePeek(slirpq) == 0) {
|
|
|
|
|
/* If we did not get anything, wait a while. */
|
|
|
|
|
thread_wait_event(evt, 10);
|
|
|
|
|
continue;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* Grab a packet from the queue. */
|
|
|
|
|
qp = QueueDelete(slirpq);
|
2017-05-22 01:08:10 -04:00
|
|
|
#if 0
|
2017-05-12 05:05:20 -04:00
|
|
|
pclog("SLiRP: inQ:%d got a %dbyte packet @%08lx\n",
|
|
|
|
|
QueuePeek(slirpq), qp->len, qp);
|
2017-05-22 01:08:10 -04:00
|
|
|
#endif
|
2017-05-12 05:05:20 -04:00
|
|
|
|
|
|
|
|
if (poll_rx != NULL)
|
|
|
|
|
poll_rx(poll_arg, (uint8_t *)&qp->data, qp->len);
|
|
|
|
|
|
|
|
|
|
/* Done with this one. */
|
|
|
|
|
free(qp);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
|
|
|
|
endslirp();
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
thread_destroy_event(evt);
|
2017-09-08 16:35:14 +02:00
|
|
|
evt = poll_tid = NULL;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2017-10-14 18:52:25 +02:00
|
|
|
thread_close_mutex(slirpMutex);
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
pclog("SLiRP: polling stopped.\n");
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
/* Initialize SLiRP for us. */
|
|
|
|
|
int
|
|
|
|
|
network_slirp_setup(uint8_t *mac, NETRXCB func, void *arg)
|
|
|
|
|
{
|
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();
|
|
|
|
|
pclog(" Packet queue is at %08lx\n", &slirpq);
|
|
|
|
|
|
|
|
|
|
/* Save the callback info. */
|
|
|
|
|
poll_rx = func;
|
|
|
|
|
poll_arg = arg;
|
|
|
|
|
|
2017-10-14 08:34:47 +02:00
|
|
|
slirpMutex = thread_create_mutex(L"86Box.SLiRPMutex");
|
|
|
|
|
|
2017-09-08 16:35:14 +02:00
|
|
|
poll_data.wake_poll_thread = thread_create_event();
|
|
|
|
|
poll_data.poll_complete = thread_create_event();
|
|
|
|
|
|
2017-05-22 01:08:10 -04:00
|
|
|
pclog("SLiRP: starting thread..\n");
|
2017-05-12 05:05:20 -04:00
|
|
|
poll_tid = thread_create(poll_thread, mac);
|
|
|
|
|
|
|
|
|
|
return(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
void
|
|
|
|
|
network_slirp_close(void)
|
|
|
|
|
{
|
|
|
|
|
queueADT sl;
|
|
|
|
|
|
|
|
|
|
if (slirpq != NULL) {
|
|
|
|
|
pclog("Closing SLiRP\n");
|
|
|
|
|
|
|
|
|
|
/* Tell the polling thread to shut down. */
|
|
|
|
|
sl = slirpq; slirpq = NULL;
|
|
|
|
|
#if 1
|
|
|
|
|
/* Terminate the polling thread. */
|
|
|
|
|
if (poll_tid != NULL) {
|
|
|
|
|
thread_kill(poll_tid);
|
|
|
|
|
poll_tid = NULL;
|
|
|
|
|
}
|
|
|
|
|
#else
|
|
|
|
|
/* Wait for the polling thread to shut down. */
|
|
|
|
|
while (poll_tid != NULL)
|
|
|
|
|
;
|
|
|
|
|
#endif
|
|
|
|
|
|
2017-10-14 08:34:47 +02:00
|
|
|
thread_close_mutex(slirpMutex);
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* OK, now shut down SLiRP itself. */
|
|
|
|
|
QueueDestroy(sl);
|
|
|
|
|
slirp_exit(0);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
poll_rx = NULL;
|
|
|
|
|
poll_arg = NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-08-22 02:16:15 +02:00
|
|
|
/* Test SLiRP - 1 = success, 0 = failure. */
|
|
|
|
|
int
|
|
|
|
|
network_slirp_test(void)
|
|
|
|
|
{
|
|
|
|
|
if (slirp_init() != 0) {
|
|
|
|
|
pclog("SLiRP could not be initialized!\n");
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
else
|
|
|
|
|
{
|
|
|
|
|
slirp_exit(0);
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Send a packet to the SLiRP interface. */
|
|
|
|
|
void
|
|
|
|
|
network_slirp_in(uint8_t *pkt, int pkt_len)
|
|
|
|
|
{
|
2017-09-08 16:35:14 +02:00
|
|
|
if (slirpq != NULL) {
|
|
|
|
|
poll_data.busy = 1;
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
slirp_input((const uint8_t *)pkt, pkt_len);
|
2017-09-08 16:35:14 +02:00
|
|
|
|
|
|
|
|
poll_data.busy = 0;
|
|
|
|
|
thread_set_event(poll_data.poll_complete);
|
|
|
|
|
}
|
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);
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
slirp_can_output(void)
|
|
|
|
|
{
|
|
|
|
|
return((slirpq != NULL)?1:0);
|
|
|
|
|
}
|