2017-05-09 22:09:55 -04:00
|
|
|
/*
|
2022-10-29 10:52:49 -04:00
|
|
|
* VARCem Virtual ARchaeological Computer EMulator.
|
|
|
|
|
* An emulator of (mostly) x86-based PC systems and devices,
|
|
|
|
|
* using the ISA,EISA,VLB,MCA and PCI system buses, roughly
|
|
|
|
|
* spanning the era between 1981 and 1995.
|
2017-05-09 22:09:55 -04:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* Implementation of the network module.
|
2017-05-09 22:09:55 -04:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* NOTE The definition of the netcard_t is currently not optimal;
|
|
|
|
|
* it should be malloc'ed and then linked to the NETCARD def.
|
|
|
|
|
* Will be done later.
|
2017-05-12 05:05:20 -04:00
|
|
|
*
|
2020-03-25 00:46:02 +02:00
|
|
|
*
|
2017-05-09 22:09:55 -04:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* Authors: Fred N. van Kempen, <decwiz@yahoo.com>
|
2017-10-10 03:07:29 -04:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* Copyright 2017-2019 Fred N. van Kempen.
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* Redistribution and use in source and binary forms, with
|
|
|
|
|
* or without modification, are permitted provided that the
|
|
|
|
|
* following conditions are met:
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* 1. Redistributions of source code must retain the entire
|
|
|
|
|
* above notice, this list of conditions and the following
|
|
|
|
|
* disclaimer.
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* 2. Redistributions in binary form must reproduce the above
|
|
|
|
|
* copyright notice, this list of conditions and the
|
|
|
|
|
* following disclaimer in the documentation and/or other
|
|
|
|
|
* materials provided with the distribution.
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
2022-10-27 17:08:58 -04:00
|
|
|
* 3. Neither the name of the copyright holder nor the names
|
|
|
|
|
* of its contributors may be used to endorse or promote
|
|
|
|
|
* products derived from this software without specific
|
|
|
|
|
* prior written permission.
|
2018-03-19 01:02:04 +01:00
|
|
|
*
|
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A
|
|
|
|
|
* PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT
|
|
|
|
|
* HOLDER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL,
|
|
|
|
|
* SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT
|
|
|
|
|
* LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE,
|
|
|
|
|
* DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY
|
|
|
|
|
* THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT
|
|
|
|
|
* (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
|
|
|
|
|
* OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
|
2017-05-09 22:09:55 -04:00
|
|
|
*/
|
2018-05-21 19:04:05 +02:00
|
|
|
#include <stdarg.h>
|
2021-12-17 18:48:13 +01:00
|
|
|
#include <stdatomic.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdint.h>
|
2018-05-21 19:04:05 +02:00
|
|
|
#include <stdio.h>
|
2017-05-06 17:48:33 +02:00
|
|
|
#include <string.h>
|
2017-09-25 04:31:20 -04:00
|
|
|
#include <stdlib.h>
|
|
|
|
|
#include <wchar.h>
|
2020-12-16 20:35:07 +01:00
|
|
|
#include <time.h>
|
2022-10-27 11:20:31 -04:00
|
|
|
#ifndef _WIN32
|
|
|
|
|
#include <sys/time.h>
|
|
|
|
|
#endif /* _WIN32 */
|
2022-08-21 16:55:47 +02:00
|
|
|
#include <stdbool.h>
|
2018-05-21 19:04:05 +02:00
|
|
|
#define HAVE_STDARG_H
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/86box.h>
|
|
|
|
|
#include <86box/device.h>
|
2020-04-29 23:39:54 +02:00
|
|
|
#include <86box/timer.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/plat.h>
|
2022-04-19 23:06:39 +02:00
|
|
|
#include <86box/thread.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/ui.h>
|
2022-08-21 16:55:47 +02:00
|
|
|
#include <86box/timer.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/network.h>
|
|
|
|
|
#include <86box/net_3c503.h>
|
|
|
|
|
#include <86box/net_ne2000.h>
|
|
|
|
|
#include <86box/net_pcnet.h>
|
2020-10-17 01:21:46 -03:00
|
|
|
#include <86box/net_plip.h>
|
2020-03-29 14:24:42 +02:00
|
|
|
#include <86box/net_wd8003.h>
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
#ifdef _WIN32
|
2022-09-18 17:18:20 -04:00
|
|
|
# define WIN32_LEAN_AND_MEAN
|
|
|
|
|
# include <windows.h>
|
|
|
|
|
# include <winsock2.h>
|
2022-08-21 16:55:47 +02:00
|
|
|
#endif
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-02-02 17:48:04 -05:00
|
|
|
static const device_t net_none_device = {
|
2022-09-18 17:18:20 -04:00
|
|
|
.name = "None",
|
2022-03-13 09:50:25 -04:00
|
|
|
.internal_name = "none",
|
2022-09-18 17:18:20 -04:00
|
|
|
.flags = 0,
|
|
|
|
|
.local = NET_TYPE_NONE,
|
|
|
|
|
.init = NULL,
|
|
|
|
|
.close = NULL,
|
|
|
|
|
.reset = NULL,
|
2022-03-13 09:50:25 -04:00
|
|
|
{ .available = NULL },
|
|
|
|
|
.speed_changed = NULL,
|
2022-09-18 17:18:20 -04:00
|
|
|
.force_redraw = NULL,
|
|
|
|
|
.config = NULL
|
2022-02-02 17:48:04 -05:00
|
|
|
};
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
static const device_t *net_cards[] = {
|
|
|
|
|
&net_none_device,
|
|
|
|
|
&threec503_device,
|
|
|
|
|
&pcnet_am79c960_device,
|
|
|
|
|
&pcnet_am79c961_device,
|
|
|
|
|
&ne1000_device,
|
|
|
|
|
&ne2000_device,
|
|
|
|
|
&pcnet_am79c960_eb_device,
|
|
|
|
|
&rtl8019as_device,
|
|
|
|
|
&wd8003e_device,
|
|
|
|
|
&wd8003eb_device,
|
|
|
|
|
&wd8013ebt_device,
|
|
|
|
|
&plip_device,
|
|
|
|
|
ðernext_mc_device,
|
|
|
|
|
&wd8003eta_device,
|
|
|
|
|
&wd8003ea_device,
|
|
|
|
|
&wd8013epa_device,
|
|
|
|
|
&pcnet_am79c973_device,
|
|
|
|
|
&pcnet_am79c970a_device,
|
|
|
|
|
&rtl8029as_device,
|
|
|
|
|
&pcnet_am79c960_vlb_device,
|
|
|
|
|
NULL
|
2017-05-12 05:05:20 -04:00
|
|
|
};
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
netcard_conf_t net_cards_conf[NET_CARD_MAX];
|
2022-09-18 17:18:20 -04:00
|
|
|
int net_card_current = 0;
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2017-05-18 01:57:16 -04:00
|
|
|
/* Global variables. */
|
2022-09-18 17:18:20 -04:00
|
|
|
int network_ndev;
|
|
|
|
|
netdev_t network_devs[NET_HOST_INTF_MAX];
|
2020-04-30 02:12:59 +02:00
|
|
|
|
|
|
|
|
/* Local variables. */
|
2017-10-16 21:19:51 +02:00
|
|
|
|
2022-10-27 11:20:31 -04:00
|
|
|
#if defined ENABLE_NETWORK_LOG && !defined(_WIN32)
|
2022-09-18 17:18:20 -04:00
|
|
|
int network_do_log = ENABLE_NETWORK_LOG;
|
|
|
|
|
static FILE *network_dump = NULL;
|
2020-10-17 20:28:30 -03:00
|
|
|
static mutex_t *network_dump_mutex;
|
2018-03-19 01:02:04 +01:00
|
|
|
|
|
|
|
|
static void
|
2018-10-19 00:37:25 +02:00
|
|
|
network_log(const char *fmt, ...)
|
2018-03-19 01:02:04 +01:00
|
|
|
{
|
2018-05-21 19:04:05 +02:00
|
|
|
va_list ap;
|
2018-03-19 01:02:04 +01:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
if (network_do_log) {
|
2022-09-18 17:18:20 -04:00
|
|
|
va_start(ap, fmt);
|
|
|
|
|
pclog_ex(fmt, ap);
|
|
|
|
|
va_end(ap);
|
2018-03-19 01:02:04 +01:00
|
|
|
}
|
2018-05-21 19:04:05 +02:00
|
|
|
}
|
2020-10-17 01:21:46 -03:00
|
|
|
|
|
|
|
|
static void
|
|
|
|
|
network_dump_packet(netpkt_t *pkt)
|
|
|
|
|
{
|
|
|
|
|
if (!network_dump)
|
2022-09-18 17:18:20 -04:00
|
|
|
return;
|
2020-10-17 01:21:46 -03:00
|
|
|
|
|
|
|
|
struct timeval tv;
|
|
|
|
|
gettimeofday(&tv, NULL);
|
|
|
|
|
struct {
|
2022-09-18 17:18:20 -04:00
|
|
|
uint32_t ts_sec, ts_usec, incl_len, orig_len;
|
2020-10-17 01:21:46 -03:00
|
|
|
} pcap_packet_hdr = {
|
2022-09-18 17:18:20 -04:00
|
|
|
tv.tv_sec, tv.tv_usec, pkt->len, pkt->len
|
2020-10-17 01:21:46 -03:00
|
|
|
};
|
|
|
|
|
|
2020-10-17 20:28:30 -03:00
|
|
|
if (network_dump_mutex)
|
2022-09-18 17:18:20 -04:00
|
|
|
thread_wait_mutex(network_dump_mutex);
|
2020-10-17 20:28:30 -03:00
|
|
|
|
|
|
|
|
size_t written;
|
|
|
|
|
if ((written = fwrite(&pcap_packet_hdr, 1, sizeof(pcap_packet_hdr), network_dump)) < sizeof(pcap_packet_hdr)) {
|
2022-09-18 17:18:20 -04:00
|
|
|
network_log("NETWORK: failed to write dump packet header\n");
|
|
|
|
|
fseek(network_dump, -written, SEEK_CUR);
|
2020-10-17 20:28:30 -03:00
|
|
|
} else {
|
2022-09-18 17:18:20 -04:00
|
|
|
if ((written = fwrite(pkt->data, 1, pkt->len, network_dump)) < pkt->len) {
|
|
|
|
|
network_log("NETWORK: failed to write dump packet data\n");
|
|
|
|
|
fseek(network_dump, -written - sizeof(pcap_packet_hdr), SEEK_CUR);
|
|
|
|
|
}
|
|
|
|
|
fflush(network_dump);
|
2020-10-17 20:28:30 -03:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (network_dump_mutex)
|
2022-09-18 17:18:20 -04:00
|
|
|
thread_release_mutex(network_dump_mutex);
|
2020-10-17 01:21:46 -03:00
|
|
|
}
|
2018-10-19 00:37:25 +02:00
|
|
|
#else
|
2022-09-18 17:18:20 -04:00
|
|
|
# define network_log(fmt, ...)
|
|
|
|
|
# define network_dump_packet(pkt)
|
2018-10-19 00:37:25 +02:00
|
|
|
#endif
|
2018-03-19 01:02:04 +01:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
static void
|
|
|
|
|
network_winsock_clean(void)
|
2017-10-16 21:19:51 +02:00
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
WSACleanup();
|
2017-10-16 21:19:51 +02:00
|
|
|
}
|
2022-08-21 16:55:47 +02:00
|
|
|
#endif
|
2017-10-16 21:19:51 +02:00
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* Initialize the configured network cards.
|
|
|
|
|
*
|
|
|
|
|
* This function gets called only once, from the System
|
|
|
|
|
* Platform initialization code (currently in pc.c) to
|
|
|
|
|
* set our local stuff to a known state.
|
|
|
|
|
*/
|
|
|
|
|
void
|
|
|
|
|
network_init(void)
|
|
|
|
|
{
|
2017-05-22 00:21:22 -04:00
|
|
|
int i;
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
#ifdef _WIN32
|
|
|
|
|
WSADATA Data;
|
|
|
|
|
WSAStartup(MAKEWORD(2, 0), &Data);
|
|
|
|
|
atexit(network_winsock_clean);
|
|
|
|
|
#endif
|
2017-05-18 01:57:16 -04:00
|
|
|
|
2017-05-22 00:21:22 -04:00
|
|
|
/* Create a first device entry that's always there, as needed by UI. */
|
|
|
|
|
strcpy(network_devs[0].device, "none");
|
|
|
|
|
strcpy(network_devs[0].description, "None");
|
|
|
|
|
network_ndev = 1;
|
|
|
|
|
|
|
|
|
|
/* Initialize the Pcap system module, if present. */
|
2017-10-29 04:20:20 -05:00
|
|
|
i = net_pcap_prepare(&network_devs[network_ndev]);
|
2017-05-22 00:21:22 -04:00
|
|
|
if (i > 0)
|
2022-09-18 17:18:20 -04:00
|
|
|
network_ndev += i;
|
2020-10-17 01:21:46 -03:00
|
|
|
|
2022-10-27 11:20:31 -04:00
|
|
|
#if defined ENABLE_NETWORK_LOG && !defined(_WIN32)
|
2020-10-17 01:21:46 -03:00
|
|
|
/* Start packet dump. */
|
|
|
|
|
network_dump = fopen("network.pcap", "wb");
|
|
|
|
|
|
|
|
|
|
struct {
|
2022-09-18 17:18:20 -04:00
|
|
|
uint32_t magic_number;
|
|
|
|
|
uint16_t version_major, version_minor;
|
|
|
|
|
int32_t thiszone;
|
|
|
|
|
uint32_t sigfigs, snaplen, network;
|
2020-10-17 01:21:46 -03:00
|
|
|
} pcap_hdr = {
|
2022-09-18 17:18:20 -04:00
|
|
|
0xa1b2c3d4,
|
|
|
|
|
2, 4,
|
|
|
|
|
0,
|
|
|
|
|
0, 65535, 1
|
2020-10-17 01:21:46 -03:00
|
|
|
};
|
|
|
|
|
fwrite(&pcap_hdr, sizeof(pcap_hdr), 1, network_dump);
|
|
|
|
|
fflush(network_dump);
|
|
|
|
|
#endif
|
2017-05-12 05:05:20 -04:00
|
|
|
}
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
void
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_init(netqueue_t *queue)
|
2020-04-29 23:39:54 +02:00
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
queue->head = queue->tail = 0;
|
2022-09-18 17:18:20 -04:00
|
|
|
for (int i = 0; i < NET_QUEUE_LEN; i++) {
|
2022-08-21 16:55:47 +02:00
|
|
|
queue->packets[i].data = calloc(1, NET_MAX_FRAME);
|
2022-09-18 17:18:20 -04:00
|
|
|
queue->packets[i].len = 0;
|
2022-02-20 21:04:30 +01:00
|
|
|
}
|
2020-04-29 23:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
static bool
|
|
|
|
|
network_queue_full(netqueue_t *queue)
|
2020-04-29 23:39:54 +02:00
|
|
|
{
|
2022-08-27 16:52:33 +02:00
|
|
|
return ((queue->head + 1) & NET_QUEUE_LEN_MASK) == queue->tail;
|
2022-08-21 16:55:47 +02:00
|
|
|
}
|
2020-04-29 23:39:54 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
static bool
|
|
|
|
|
network_queue_empty(netqueue_t *queue)
|
|
|
|
|
{
|
|
|
|
|
return (queue->head == queue->tail);
|
|
|
|
|
}
|
2020-04-29 23:39:54 +02:00
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
static inline void
|
|
|
|
|
network_swap_packet(netpkt_t *pkt1, netpkt_t *pkt2)
|
|
|
|
|
{
|
|
|
|
|
netpkt_t tmp = *pkt2;
|
2022-09-18 17:18:20 -04:00
|
|
|
*pkt2 = *pkt1;
|
|
|
|
|
*pkt1 = tmp;
|
2022-08-27 16:52:33 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
int
|
|
|
|
|
network_queue_put(netqueue_t *queue, uint8_t *data, int len)
|
|
|
|
|
{
|
2022-08-27 16:52:33 +02:00
|
|
|
if (len == 0 || len > NET_MAX_FRAME || network_queue_full(queue)) {
|
2022-08-21 16:55:47 +02:00
|
|
|
return 0;
|
2022-02-20 21:04:30 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
netpkt_t *pkt = &queue->packets[queue->head];
|
|
|
|
|
memcpy(pkt->data, data, len);
|
2022-09-18 17:18:20 -04:00
|
|
|
pkt->len = len;
|
2022-08-27 16:52:33 +02:00
|
|
|
queue->head = (queue->head + 1) & NET_QUEUE_LEN_MASK;
|
|
|
|
|
return 1;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
network_queue_put_swap(netqueue_t *queue, netpkt_t *src_pkt)
|
|
|
|
|
{
|
|
|
|
|
if (src_pkt->len == 0 || src_pkt->len > NET_MAX_FRAME || network_queue_full(queue)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
netpkt_t *dst_pkt = &queue->packets[queue->head];
|
|
|
|
|
network_swap_packet(src_pkt, dst_pkt);
|
|
|
|
|
|
|
|
|
|
queue->head = (queue->head + 1) & NET_QUEUE_LEN_MASK;
|
2022-08-21 16:55:47 +02:00
|
|
|
return 1;
|
2020-04-29 23:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
static int
|
2022-09-18 17:18:20 -04:00
|
|
|
network_queue_get_swap(netqueue_t *queue, netpkt_t *dst_pkt)
|
|
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
if (network_queue_empty(queue))
|
|
|
|
|
return 0;
|
2020-04-29 23:39:54 +02:00
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
netpkt_t *src_pkt = &queue->packets[queue->tail];
|
|
|
|
|
network_swap_packet(src_pkt, dst_pkt);
|
|
|
|
|
queue->tail = (queue->tail + 1) & NET_QUEUE_LEN_MASK;
|
2022-08-21 16:55:47 +02:00
|
|
|
return 1;
|
|
|
|
|
}
|
2022-02-20 21:04:30 +01:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
static int
|
|
|
|
|
network_queue_move(netqueue_t *dst_q, netqueue_t *src_q)
|
|
|
|
|
{
|
|
|
|
|
if (network_queue_empty(src_q))
|
|
|
|
|
return 0;
|
2022-02-20 21:04:30 +01:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
if (network_queue_full(dst_q)) {
|
|
|
|
|
return 0;
|
|
|
|
|
}
|
2022-02-20 21:04:30 +01:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
netpkt_t *src_pkt = &src_q->packets[src_q->tail];
|
|
|
|
|
netpkt_t *dst_pkt = &dst_q->packets[dst_q->head];
|
2022-02-20 21:04:30 +01:00
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
network_swap_packet(src_pkt, dst_pkt);
|
|
|
|
|
dst_q->head = (dst_q->head + 1) & NET_QUEUE_LEN_MASK;
|
|
|
|
|
src_q->tail = (src_q->tail + 1) & NET_QUEUE_LEN_MASK;
|
2022-02-20 21:04:30 +01:00
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
return dst_pkt->len;
|
2022-02-20 21:04:30 +01:00
|
|
|
}
|
|
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
void
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_clear(netqueue_t *queue)
|
2020-04-29 23:39:54 +02:00
|
|
|
{
|
2022-09-18 17:18:20 -04:00
|
|
|
for (int i = 0; i < NET_QUEUE_LEN; i++) {
|
2022-08-21 16:55:47 +02:00
|
|
|
free(queue->packets[i].data);
|
|
|
|
|
queue->packets[i].len = 0;
|
|
|
|
|
}
|
|
|
|
|
queue->tail = queue->head = 0;
|
2020-04-29 23:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
static void
|
2020-04-30 02:12:59 +02:00
|
|
|
network_rx_queue(void *priv)
|
2020-04-29 23:39:54 +02:00
|
|
|
{
|
2022-09-18 17:18:20 -04:00
|
|
|
netcard_t *card = (netcard_t *) priv;
|
2020-11-16 00:01:21 +01:00
|
|
|
|
2022-08-27 19:08:28 +02:00
|
|
|
uint32_t new_link_state = net_cards_conf[card->card_num].link_state;
|
|
|
|
|
if (new_link_state != card->link_state) {
|
|
|
|
|
if (card->set_link_state)
|
|
|
|
|
card->set_link_state(card->card_drv, new_link_state);
|
|
|
|
|
card->link_state = new_link_state;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 16:52:33 +02:00
|
|
|
uint32_t rx_bytes = 0;
|
|
|
|
|
for (int i = 0; i < NET_QUEUE_LEN; i++) {
|
|
|
|
|
if (card->queued_pkt.len == 0) {
|
|
|
|
|
thread_wait_mutex(card->rx_mutex);
|
|
|
|
|
int res = network_queue_get_swap(&card->queues[NET_QUEUE_RX], &card->queued_pkt);
|
|
|
|
|
thread_release_mutex(card->rx_mutex);
|
|
|
|
|
if (!res)
|
|
|
|
|
break;
|
|
|
|
|
}
|
2022-08-21 16:55:47 +02:00
|
|
|
|
|
|
|
|
network_dump_packet(&card->queued_pkt);
|
2022-08-27 16:52:33 +02:00
|
|
|
int res = card->rx(card->card_drv, card->queued_pkt.data, card->queued_pkt.len);
|
|
|
|
|
if (!res)
|
|
|
|
|
break;
|
|
|
|
|
rx_bytes += card->queued_pkt.len;
|
2022-08-21 16:55:47 +02:00
|
|
|
card->queued_pkt.len = 0;
|
2022-02-20 21:04:30 +01:00
|
|
|
}
|
2020-04-29 23:39:54 +02:00
|
|
|
|
2022-02-20 13:58:38 +01:00
|
|
|
/* Transmission. */
|
2022-08-27 16:52:33 +02:00
|
|
|
uint32_t tx_bytes = 0;
|
2022-08-21 16:55:47 +02:00
|
|
|
thread_wait_mutex(card->tx_mutex);
|
2022-08-27 16:52:33 +02:00
|
|
|
for (int i = 0; i < NET_QUEUE_LEN; i++) {
|
|
|
|
|
uint32_t bytes = network_queue_move(&card->queues[NET_QUEUE_TX_HOST], &card->queues[NET_QUEUE_TX_VM]);
|
|
|
|
|
if (!bytes)
|
|
|
|
|
break;
|
|
|
|
|
tx_bytes += bytes;
|
|
|
|
|
}
|
2022-08-21 16:55:47 +02:00
|
|
|
thread_release_mutex(card->tx_mutex);
|
2022-08-27 16:52:33 +02:00
|
|
|
if (tx_bytes) {
|
2022-08-21 16:55:47 +02:00
|
|
|
/* Notify host that a packet is available in the TX queue */
|
|
|
|
|
card->host_drv.notify_in(card->host_drv.priv);
|
|
|
|
|
}
|
2022-02-20 01:30:37 +01:00
|
|
|
|
2022-08-27 17:08:50 +02:00
|
|
|
double timer_period = card->byte_period * (rx_bytes > tx_bytes ? rx_bytes : tx_bytes);
|
2022-08-27 16:52:33 +02:00
|
|
|
if (timer_period < 200)
|
|
|
|
|
timer_period = 200;
|
|
|
|
|
|
|
|
|
|
timer_on_auto(&card->timer, timer_period);
|
|
|
|
|
|
|
|
|
|
bool activity = rx_bytes || tx_bytes;
|
2022-09-18 17:18:20 -04:00
|
|
|
bool led_on = card->led_timer & 0x80000000;
|
2022-08-27 19:08:28 +02:00
|
|
|
if ((activity && !led_on) || (card->led_timer & 0x7fffffff) >= 150000) {
|
|
|
|
|
ui_sb_update_icon(SB_NETWORK | card->card_num, activity);
|
|
|
|
|
card->led_timer = 0 | (activity << 31);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
card->led_timer += timer_period;
|
2020-04-29 23:39:54 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* Attach a network card to the system.
|
|
|
|
|
*
|
|
|
|
|
* This function is called by a hardware driver ("card") after it has
|
|
|
|
|
* finished initializing itself, to link itself to the platform support
|
|
|
|
|
* modules.
|
|
|
|
|
*/
|
2022-08-21 16:55:47 +02:00
|
|
|
netcard_t *
|
2022-08-27 19:08:28 +02:00
|
|
|
network_attach(void *card_drv, uint8_t *mac, NETRXCB rx, NETSETLINKSTATE set_link_state)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2022-09-18 17:18:20 -04:00
|
|
|
netcard_t *card = calloc(1, sizeof(netcard_t));
|
2022-08-21 16:55:47 +02:00
|
|
|
card->queued_pkt.data = calloc(1, NET_MAX_FRAME);
|
2022-09-18 17:18:20 -04:00
|
|
|
card->card_drv = card_drv;
|
|
|
|
|
card->rx = rx;
|
|
|
|
|
card->set_link_state = set_link_state;
|
|
|
|
|
card->tx_mutex = thread_create_mutex();
|
|
|
|
|
card->rx_mutex = thread_create_mutex();
|
|
|
|
|
card->card_num = net_card_current;
|
|
|
|
|
card->byte_period = NET_PERIOD_10M;
|
|
|
|
|
|
|
|
|
|
for (int i = 0; i < 3; i++) {
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_init(&card->queues[i]);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
2020-04-29 23:39:54 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
switch (net_cards_conf[net_card_current].net_type) {
|
|
|
|
|
case NET_TYPE_SLIRP:
|
|
|
|
|
default:
|
2022-09-18 17:18:20 -04:00
|
|
|
card->host_drv = net_slirp_drv;
|
2022-08-21 16:55:47 +02:00
|
|
|
card->host_drv.priv = card->host_drv.init(card, mac, NULL);
|
|
|
|
|
break;
|
|
|
|
|
|
|
|
|
|
case NET_TYPE_PCAP:
|
2022-09-18 17:18:20 -04:00
|
|
|
card->host_drv = net_pcap_drv;
|
2022-08-21 16:55:47 +02:00
|
|
|
card->host_drv.priv = card->host_drv.init(card, mac, net_cards_conf[net_card_current].host_dev_name);
|
|
|
|
|
break;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
if (!card->host_drv.priv) {
|
|
|
|
|
thread_close_mutex(card->tx_mutex);
|
|
|
|
|
thread_close_mutex(card->rx_mutex);
|
2022-09-18 17:18:20 -04:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_clear(&card->queues[i]);
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
free(card->queued_pkt.data);
|
|
|
|
|
free(card);
|
|
|
|
|
return NULL;
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
timer_add(&card->timer, network_rx_queue, card, 0);
|
2022-08-27 17:08:50 +02:00
|
|
|
timer_on_auto(&card->timer, 100);
|
2020-07-12 20:20:06 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
return card;
|
|
|
|
|
}
|
2020-07-12 20:20:06 +02:00
|
|
|
|
|
|
|
|
void
|
2022-08-21 16:55:47 +02:00
|
|
|
netcard_close(netcard_t *card)
|
2020-07-12 20:20:06 +02:00
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
timer_stop(&card->timer);
|
|
|
|
|
card->host_drv.close(card->host_drv.priv);
|
|
|
|
|
|
|
|
|
|
thread_close_mutex(card->tx_mutex);
|
|
|
|
|
thread_close_mutex(card->rx_mutex);
|
2022-09-18 17:18:20 -04:00
|
|
|
for (int i = 0; i < 3; i++) {
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_clear(&card->queues[i]);
|
2020-07-12 20:20:06 +02:00
|
|
|
}
|
2022-08-21 16:55:47 +02:00
|
|
|
|
|
|
|
|
free(card->queued_pkt.data);
|
|
|
|
|
free(card);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* Stop any network activity. */
|
2017-05-06 17:48:33 +02:00
|
|
|
void
|
2017-05-12 05:05:20 -04:00
|
|
|
network_close(void)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2022-10-27 11:20:31 -04:00
|
|
|
#if defined ENABLE_NETWORK_LOG && !defined(_WIN32)
|
2020-10-17 20:28:30 -03:00
|
|
|
thread_close_mutex(network_dump_mutex);
|
|
|
|
|
network_dump_mutex = NULL;
|
|
|
|
|
#endif
|
2017-10-29 04:20:20 -05:00
|
|
|
|
2018-05-21 19:04:05 +02:00
|
|
|
network_log("NETWORK: closed.\n");
|
2017-05-09 22:09:55 -04:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/*
|
|
|
|
|
* Reset the network card(s).
|
|
|
|
|
*
|
|
|
|
|
* This function is called each time the system is reset,
|
|
|
|
|
* either a hard reset (including power-up) or a soft reset
|
|
|
|
|
* including C-A-D reset.) It is responsible for connecting
|
|
|
|
|
* everything together.
|
|
|
|
|
*/
|
2017-05-09 22:09:55 -04:00
|
|
|
void
|
|
|
|
|
network_reset(void)
|
|
|
|
|
{
|
2017-10-29 04:20:20 -05:00
|
|
|
int i = -1;
|
|
|
|
|
|
|
|
|
|
ui_sb_update_icon(SB_NETWORK, 0);
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2022-10-27 11:20:31 -04:00
|
|
|
#if defined ENABLE_NETWORK_LOG && !defined(_WIN32)
|
2020-10-17 20:28:30 -03:00
|
|
|
network_dump_mutex = thread_create_mutex();
|
|
|
|
|
#endif
|
2017-10-29 04:20:20 -05:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
for (i = 0; i < NET_CARD_MAX; i++) {
|
2022-09-18 17:18:20 -04:00
|
|
|
if (!network_dev_available(i)) {
|
|
|
|
|
continue;
|
|
|
|
|
}
|
2017-05-12 17:33:28 -04:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
net_card_current = i;
|
|
|
|
|
device_add_inst(net_cards[net_cards_conf[i].device_num], i + 1);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
2020-04-30 02:12:59 +02:00
|
|
|
/* Queue a packet for transmission to one of the network providers. */
|
2017-05-06 17:48:33 +02:00
|
|
|
void
|
2022-08-21 16:55:47 +02:00
|
|
|
network_tx(netcard_t *card, uint8_t *bufp, int len)
|
2017-05-06 17:48:33 +02:00
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
network_queue_put(&card->queues[NET_QUEUE_TX_VM], bufp, len);
|
|
|
|
|
}
|
2017-10-29 04:20:20 -05:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
int
|
|
|
|
|
network_tx_pop(netcard_t *card, netpkt_t *out_pkt)
|
2022-08-21 16:55:47 +02:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
2017-05-12 05:05:20 -04:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
thread_wait_mutex(card->tx_mutex);
|
2022-08-27 16:52:33 +02:00
|
|
|
ret = network_queue_get_swap(&card->queues[NET_QUEUE_TX_HOST], out_pkt);
|
2022-08-21 16:55:47 +02:00
|
|
|
thread_release_mutex(card->tx_mutex);
|
2020-04-30 02:12:59 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
return ret;
|
|
|
|
|
}
|
2020-04-30 02:12:59 +02:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
int
|
|
|
|
|
network_tx_popv(netcard_t *card, netpkt_t *pkt_vec, int vec_size)
|
2022-08-27 16:52:33 +02:00
|
|
|
{
|
|
|
|
|
int pkt_count = 0;
|
|
|
|
|
|
|
|
|
|
netqueue_t *queue = &card->queues[NET_QUEUE_TX_HOST];
|
|
|
|
|
thread_wait_mutex(card->tx_mutex);
|
|
|
|
|
for (int i = 0; i < vec_size; i++) {
|
|
|
|
|
if (!network_queue_get_swap(queue, pkt_vec))
|
|
|
|
|
break;
|
|
|
|
|
pkt_count++;
|
|
|
|
|
pkt_vec++;
|
|
|
|
|
}
|
|
|
|
|
thread_release_mutex(card->tx_mutex);
|
|
|
|
|
|
|
|
|
|
return pkt_count;
|
|
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
int
|
|
|
|
|
network_rx_put(netcard_t *card, uint8_t *bufp, int len)
|
2020-04-30 02:12:59 +02:00
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
int ret = 0;
|
2020-04-30 02:12:59 +02:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
thread_wait_mutex(card->rx_mutex);
|
|
|
|
|
ret = network_queue_put(&card->queues[NET_QUEUE_RX], bufp, len);
|
|
|
|
|
thread_release_mutex(card->rx_mutex);
|
2020-11-16 00:01:21 +01:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
return ret;
|
2017-10-29 04:20:20 -05:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
int
|
|
|
|
|
network_rx_put_pkt(netcard_t *card, netpkt_t *pkt)
|
2022-08-27 16:52:33 +02:00
|
|
|
{
|
|
|
|
|
int ret = 0;
|
|
|
|
|
|
|
|
|
|
thread_wait_mutex(card->rx_mutex);
|
|
|
|
|
ret = network_queue_put_swap(&card->queues[NET_QUEUE_RX], pkt);
|
|
|
|
|
thread_release_mutex(card->rx_mutex);
|
|
|
|
|
|
|
|
|
|
return ret;
|
|
|
|
|
}
|
|
|
|
|
|
2022-08-27 19:08:28 +02:00
|
|
|
void
|
|
|
|
|
network_connect(int id, int connect)
|
|
|
|
|
{
|
|
|
|
|
if (id >= NET_CARD_MAX)
|
|
|
|
|
return;
|
|
|
|
|
|
|
|
|
|
if (connect) {
|
|
|
|
|
net_cards_conf[id].link_state &= ~NET_LINK_DOWN;
|
|
|
|
|
} else {
|
|
|
|
|
net_cards_conf[id].link_state |= NET_LINK_DOWN;
|
|
|
|
|
}
|
|
|
|
|
}
|
|
|
|
|
|
|
|
|
|
int
|
|
|
|
|
network_is_connected(int id)
|
|
|
|
|
{
|
|
|
|
|
if (id >= NET_CARD_MAX)
|
|
|
|
|
return 0;
|
|
|
|
|
|
|
|
|
|
return !(net_cards_conf[id].link_state & NET_LINK_DOWN);
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
int
|
|
|
|
|
network_dev_to_id(char *devname)
|
|
|
|
|
{
|
|
|
|
|
int i = 0;
|
|
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
for (i = 0; i < network_ndev; i++) {
|
|
|
|
|
if (!strcmp((char *) network_devs[i].device, devname)) {
|
|
|
|
|
return (i);
|
|
|
|
|
}
|
2017-10-29 04:20:20 -05:00
|
|
|
}
|
|
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
return (-1);
|
2017-10-29 04:20:20 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
/* UI */
|
2022-08-27 19:08:28 +02:00
|
|
|
int
|
|
|
|
|
network_dev_available(int id)
|
|
|
|
|
{
|
|
|
|
|
int available = (net_cards_conf[id].device_num > 0) && (net_cards_conf[id].net_type != NET_TYPE_NONE);
|
|
|
|
|
|
|
|
|
|
if ((net_cards_conf[id].net_type == NET_TYPE_PCAP && (network_dev_to_id(net_cards_conf[id].host_dev_name) <= 0)))
|
|
|
|
|
available = 0;
|
|
|
|
|
|
|
|
|
|
return available;
|
|
|
|
|
}
|
|
|
|
|
|
2017-10-29 04:20:20 -05:00
|
|
|
int
|
|
|
|
|
network_available(void)
|
|
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
int available = 0;
|
2017-10-29 04:20:20 -05:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
for (int i = 0; i < NET_CARD_MAX; i++) {
|
2022-08-27 19:08:28 +02:00
|
|
|
available |= network_dev_available(i);
|
2022-08-21 16:55:47 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
|
|
return available;
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_available(int card)
|
|
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
if (net_cards[card])
|
2022-09-18 17:18:20 -04:00
|
|
|
return (device_available(net_cards[card]));
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
return (1);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2018-03-19 01:02:04 +01:00
|
|
|
const device_t *
|
2017-05-06 17:48:33 +02:00
|
|
|
network_card_getdevice(int card)
|
|
|
|
|
{
|
2022-09-18 17:18:20 -04:00
|
|
|
return (net_cards[card]);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_has_config(int card)
|
|
|
|
|
{
|
2022-09-18 17:18:20 -04:00
|
|
|
if (!net_cards[card])
|
|
|
|
|
return (0);
|
2017-05-06 17:48:33 +02:00
|
|
|
|
2022-09-18 17:18:20 -04:00
|
|
|
return (device_has_config(net_cards[card]) ? 1 : 0);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
char *
|
|
|
|
|
network_card_get_internal_name(int card)
|
|
|
|
|
{
|
2022-08-21 16:55:47 +02:00
|
|
|
return device_get_internal_name(net_cards[card]);
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
|
|
|
|
|
2017-05-12 05:05:20 -04:00
|
|
|
/* UI */
|
2017-05-06 17:48:33 +02:00
|
|
|
int
|
|
|
|
|
network_card_get_from_internal_name(char *s)
|
|
|
|
|
{
|
|
|
|
|
int c = 0;
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-08-21 16:55:47 +02:00
|
|
|
while (net_cards[c] != NULL) {
|
2022-09-18 17:18:20 -04:00
|
|
|
if (!strcmp((char *) net_cards[c]->internal_name, s))
|
|
|
|
|
return (c);
|
|
|
|
|
c++;
|
2017-05-06 17:48:33 +02:00
|
|
|
}
|
2022-02-20 02:26:27 -05:00
|
|
|
|
2022-02-03 03:10:06 +01:00
|
|
|
return 0;
|
2022-08-21 16:55:47 +02:00
|
|
|
}
|