Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -105,10 +105,9 @@ Xbox packages provide static libraries, headers, and pkg-config metadata.
For a local SDK checkout, set `XBOX_PACKAGE_PREFIX` to the installed package
prefix, such as `$PWD/.work/packages/opt/xbfoundry/xbox`. The SDK's
`nxdk-pkg-config` wrapper searches that prefix for Xbox packages. Makefile
projects can request package modules with `NXDK_PACKAGES`. Install `lwip`
for networking. Makefile projects can require it with `NXDK_NET=y` or by
including `lwip` in `NXDK_PACKAGES`.
projects can request package modules with `NXDK_PACKAGES`.

See the [lwIP package documentation](pkgbuilds/lwip/README.md) for Xbox networking.
See the [SDL package documentation](pkgbuilds/sdl/README.md) for the SDL samples and migration guidance.

### Set up GitHub Actions
Expand Down
3 changes: 2 additions & 1 deletion lib/net/nforceif/include/arch/sys_arch.h
Original file line number Diff line number Diff line change
Expand Up @@ -73,7 +73,8 @@ typedef struct {
/* let sys.h use binary semaphores for mutexes */
#define LWIP_COMPAT_MUTEX 1

#define SYS_MBOX_SIZE 128
/* sys_sem_new takes an eight-bit initial count. */
#define SYS_MBOX_SIZE 255

typedef struct {
volatile int first, last;
Expand Down
20 changes: 19 additions & 1 deletion lib/net/nforceif/include/lwipopts.h
Original file line number Diff line number Diff line change
Expand Up @@ -280,7 +280,11 @@
/**
* LWIP_IGMP==1: Turn on IGMP module.
*/
#define LWIP_IGMP 0
#define LWIP_IGMP 1

/* Compile support for advertising and discovering services over multicast DNS. */
#define LWIP_MDNS_RESPONDER 1
#define LWIP_MDNS_SEARCH 1

/*
----------------------------------
Expand Down Expand Up @@ -361,6 +365,10 @@
*/
#define PBUF_LINK_HLEN 16

/* Absorb bursts of UDP traffic before the receiving application can run. */
#define MEMP_NUM_PBUF 128
#define PBUF_POOL_SIZE 128

/*
------------------------------------------------
---------- Network Interfaces options ----------
Expand All @@ -378,6 +386,9 @@
*/
#define LWIP_NETIF_API 1

/* Reserve a per-interface slot for the mDNS responder. */
#define LWIP_NUM_NETIF_CLIENT_DATA 1

/*
------------------------------------
---------- LOOPIF options ----------
Expand Down Expand Up @@ -408,6 +419,10 @@
*/
#define DEFAULT_THREAD_STACKSIZE 4096

/* Keep network workers above normal-priority application threads. */
#define TCPIP_THREAD_PRIO 2
#define DEFAULT_THREAD_PRIO 1

/*
----------------------------------------------
---------- Sequential layer options ----------
Expand All @@ -429,6 +444,9 @@
*/
#define LWIP_SOCKET 1

#define LWIP_SO_RCVTIMEO 1
#define LWIP_SO_RCVBUF 1

/*
----------------------------------------
---------- Statistics options ----------
Expand Down
8 changes: 6 additions & 2 deletions lib/net/nforceif/src/sys_arch.c
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@

#include <assert.h>
#include <stdlib.h>
#include <windows.h>

#include <xboxkrnl/xboxkrnl.h>

Expand Down Expand Up @@ -66,9 +67,12 @@ sys_thread_t
sys_thread_new(const char *name, lwip_thread_fn function, void *arg, int stacksize, int prio)
{
LWIP_UNUSED_ARG(name);
LWIP_UNUSED_ARG(prio);

return CreateThread(NULL, stacksize, (void *)function, arg, 0, NULL);
HANDLE thread = CreateThread(NULL, stacksize, (void *)function, arg, 0, NULL);
if (thread != NULL && prio != 0) {
SetThreadPriority(thread, prio);
}
return thread;
}

err_t sys_mbox_new(sys_mbox_t *mb, int size)
Expand Down
9 changes: 5 additions & 4 deletions lib/net/nvnetdrv/nvnetdrv.c
Original file line number Diff line number Diff line change
Expand Up @@ -387,10 +387,11 @@ int nvnetdrv_init(size_t rx_buffer_count, nvnetdrv_rx_callback_t rx_callback, si
// Setup some fixed registers for the NIC
reg32(NvRegMacAddrA) = (g_ethAddr[0] << 0) | (g_ethAddr[1] << 8) | (g_ethAddr[2] << 16) | (g_ethAddr[3] << 24);
reg32(NvRegMacAddrB) = (g_ethAddr[4] << 0) | (g_ethAddr[5] << 8);
reg32(NvRegMulticastAddrA) = NVREG_MCASTMASKA_NONE;
reg32(NvRegMulticastAddrB) = NVREG_MCASTMASKB_NONE;
reg32(NvRegMulticastMaskA) = NVREG_MCASTMASKA_NONE;
reg32(NvRegMulticastMaskB) = NVREG_MCASTMASKB_NONE;
// Accept multicast frames so lwIP can filter them by IPv4 and IPv6 group membership.
reg32(NvRegMulticastAddrA) = NVREG_MCASTADDRA_FORCE;
reg32(NvRegMulticastAddrB) = 0;
reg32(NvRegMulticastMaskA) = 0;
reg32(NvRegMulticastMaskB) = 0;
reg32(NvRegOffloadConfig) = NVREG_OFFLOAD_NORMAL;
reg32(NvRegPacketFilterFlags) = NVREG_PFF_ALWAYS_MYADDR;
reg32(NvRegDuplexMode) = NVREG_DUPLEX_MODE_FORCEH;
Expand Down
16 changes: 13 additions & 3 deletions lib/net/nvnetdrv/nvnetdrv_lwip.c
Original file line number Diff line number Diff line change
Expand Up @@ -25,7 +25,7 @@
#define IFNAME0 'x'
#define IFNAME1 'b'
#ifndef RX_BUFF_CNT
#define RX_BUFF_CNT (64)
#define RX_BUFF_CNT PBUF_POOL_SIZE
#endif

#if RX_BUFF_CNT < TCP_WND / TCP_MSS
Expand Down Expand Up @@ -69,7 +69,12 @@ void rx_pbuf_free_callback(struct pbuf *p)
void rx_callback(void *buffer, uint16_t length)
{
rx_pbuf_t *rx_pbuf = (rx_pbuf_t *)LWIP_MEMPOOL_ALLOC(RX_POOL);
LWIP_ASSERT("RX_POOL full\n", rx_pbuf != NULL);
if (rx_pbuf == NULL) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
nvnetdrv_rx_release(buffer);
return;
}
rx_pbuf->p.custom_free_function = rx_pbuf_free_callback;
rx_pbuf->buff = buffer;
struct pbuf *p = pbuf_alloced_custom(PBUF_RAW,
Expand All @@ -80,6 +85,8 @@ void rx_callback(void *buffer, uint16_t length)
NVNET_RX_BUFF_LEN - ETH_PAD_SIZE);

if (g_pnetif->input(p, g_pnetif) != ERR_OK) {
LINK_STATS_INC(link.memerr);
LINK_STATS_INC(link.drop);
pbuf_free(p);
}
}
Expand Down Expand Up @@ -110,7 +117,10 @@ static err_t low_level_init(struct netif *netif)

/* device capabilities */
/* don't set NETIF_FLAG_ETHARP if this device is not an ethernet one */
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP;
netif->flags = NETIF_FLAG_BROADCAST | NETIF_FLAG_ETHARP | NETIF_FLAG_ETHERNET;
#if LWIP_IGMP
netif->flags |= NETIF_FLAG_IGMP;
#endif

#if LWIP_IPV6 && LWIP_IPV6_MLD
/*
Expand Down
7 changes: 4 additions & 3 deletions pkgbuilds/lwip/PKGBUILD
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
pkgname=lwip
pkgver=2.2.1
pkgrel=1
pkgrel=2
pkgdesc='Lightweight TCP/IP stack with the Xbox Ethernet integration'
arch=('xbox')
url='https://savannah.nongnu.org/projects/lwip/'
Expand Down Expand Up @@ -28,7 +28,7 @@ source=(
)
sha256sums=(
'ce0b7461c0ad9602c376f0bf07c5eb7253b48c7bf66f011c6bf3e2a96731c539'
'f2a457542b63bd8eca3a8400d431c3010829404bbb957168c9723d80fb46b671'
'9d98a0cac6f8b6c9860d320a05e71f06069c68840edf1fd05151db52d7251ce1'
'52f4934ed1f63834fd75a846349b57022f7b7eba7ed0fbbd6ad5905231175f7b'
)

Expand All @@ -40,7 +40,8 @@ build() {
check() {
local symbols symbol
symbols=$(llvm-nm --defined-only --extern-only --format=posix "$srcdir/libnxdk_net.lib")
for symbol in _tcpip_init _lwip_socket _nxNetInit _nvnetif_init; do
for symbol in _tcpip_init _lwip_socket _nxNetInit _nvnetif_init \
_igmp_start _mdns_resp_init _mdns_search_service; do
if ! grep -q "^${symbol} " <<< "$symbols"; then
printf 'lwIP archive is missing %s\n' "$symbol" >&2
return 1
Expand Down
15 changes: 15 additions & 0 deletions pkgbuilds/lwip/README.md
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
# lwIP

Install the `lwip` Xbox package to build applications that use Ethernet. Makefile
projects can require the package with `NXDK_NET=y` or by adding `lwip` to
`NXDK_PACKAGES`. Other build systems can use the `lwip` pkg-config module
through `nxdk-pkg-config`.

The package ships `libnxdk_net.lib`, including `nxNetInit()` and the Xbox Ethernet
driver. It enables IPv4 IGMP, multicast DNS responses and service discovery, and
the `SO_RCVTIMEO` and `SO_RCVBUF` socket options. Initialize the network before
starting mDNS, and hold the TCP/IP core lock while calling the mDNS APIs.

Build all lwIP consumers with the headers and `lwipopts.h` from the same
package as the archive. Compile-time options affect the layout of public lwIP
structures.
2 changes: 1 addition & 1 deletion pkgbuilds/lwip/lwip.mk
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@
include $(LWIPDIR)/Filelists.mk

XBDK_NET := $(NXDK_DIR)/lib/net
LWIP_SOURCES := $(COREFILES) $(CORE4FILES) $(CORE6FILES) $(APIFILES) \
LWIP_SOURCES := $(COREFILES) $(CORE4FILES) $(CORE6FILES) $(APIFILES) $(MDNSFILES) \
$(filter-out $(LWIPDIR)/netif/slipif.c,$(NETIFFILES))
XBOX_SOURCES := $(XBDK_NET)/nforceif/src/sys_arch.c \
$(XBDK_NET)/nvnetdrv/nvnetdrv.c \
Expand Down
124 changes: 115 additions & 9 deletions tests/lwip/smoke.cpp
Original file line number Diff line number Diff line change
@@ -1,29 +1,135 @@
#include <array>

#include <gtest/gtest.h>

#include <lwip/apps/mdns.h>
#include <lwip/igmp.h>
#include <lwip/sockets.h>
#include <lwip/tcpip.h>
#include <nxdk/net.h>
#include <xboxkrnl/xboxkrnl.h>

TEST(LwipPackage, InitializesXboxNetworkingAndBindsUdpSocket)
{
nx_net_parameters_t parameters{};
parameters.ipv4_mode = NX_NET_STATIC;
parameters.ipv4_ip = PP_HTONL(LWIP_MAKEU32(10, 0, 2, 15));
parameters.ipv4_netmask = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0));
ASSERT_EQ(nxNetInit(&parameters), 0);
namespace {

constexpr u32_t LOCAL_ADDRESS = PP_HTONL(LWIP_MAKEU32(10, 0, 2, 15));

class LwipPackage : public testing::Test {
public:
static void SetUpTestSuite()
{
nx_net_parameters_t parameters{};
parameters.ipv4_mode = NX_NET_STATIC;
parameters.ipv4_ip = LOCAL_ADDRESS;
parameters.ipv4_netmask = PP_HTONL(LWIP_MAKEU32(255, 255, 255, 0));
ASSERT_EQ(nxNetInit(&parameters), 0);
}
};

TEST_F(LwipPackage, InitializesXboxNetworkingAndBindsUdpSocket)
{
const int socket = lwip_socket(AF_INET, SOCK_DGRAM, 0);
ASSERT_GE(socket, 0);

sockaddr_in address{};
address.sin_family = AF_INET;
address.sin_addr.s_addr = parameters.ipv4_ip;
address.sin_addr.s_addr = LOCAL_ADDRESS;
const int bind_result = lwip_bind(socket, reinterpret_cast<const sockaddr *>(&address), sizeof(address));

socklen_t length = sizeof(address);
const int name_result = lwip_getsockname(socket, reinterpret_cast<sockaddr *>(&address), &length);
EXPECT_EQ(lwip_close(socket), 0);
ASSERT_EQ(bind_result, 0);
ASSERT_EQ(name_result, 0);
EXPECT_EQ(address.sin_addr.s_addr, parameters.ipv4_ip);
EXPECT_EQ(address.sin_addr.s_addr, LOCAL_ADDRESS);
EXPECT_NE(address.sin_port, 0);
}

TEST_F(LwipPackage, ConfiguresSocketReceiveOptions)
{
const int socket = lwip_socket(AF_INET, SOCK_DGRAM, 0);
ASSERT_GE(socket, 0);

const timeval timeout{0, 200000};
EXPECT_EQ(lwip_setsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &timeout, sizeof(timeout)), 0);
timeval actual_timeout{};
socklen_t length = sizeof(actual_timeout);
EXPECT_EQ(lwip_getsockopt(socket, SOL_SOCKET, SO_RCVTIMEO, &actual_timeout, &length), 0);
EXPECT_EQ(actual_timeout.tv_sec, timeout.tv_sec);
EXPECT_EQ(actual_timeout.tv_usec, timeout.tv_usec);

const int buffer = 65536;
EXPECT_EQ(lwip_setsockopt(socket, SOL_SOCKET, SO_RCVBUF, &buffer, sizeof(buffer)), 0);
int actual_buffer = 0;
length = sizeof(actual_buffer);
EXPECT_EQ(lwip_getsockopt(socket, SOL_SOCKET, SO_RCVBUF, &actual_buffer, &length), 0);
EXPECT_EQ(actual_buffer, buffer);
EXPECT_EQ(lwip_close(socket), 0);
}

TEST_F(LwipPackage, StartsMulticastDnsServiceDiscovery)
{
ASSERT_NE(netif_default, nullptr);
LOCK_TCPIP_CORE();

struct netif *network_interface = netif_default;
EXPECT_EQ(network_interface->flags & (NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP),
NETIF_FLAG_ETHERNET | NETIF_FLAG_IGMP);
EXPECT_NE(netif_igmp_data(network_interface), nullptr);

mdns_resp_init();
const err_t added = mdns_resp_add_netif(network_interface, "xbdk-package-test");
EXPECT_EQ(added, ERR_OK);
if (added == ERR_OK) {
u8_t search_id = 0;
const err_t searched = mdns_search_service(nullptr, "_nvstream", DNSSD_PROTO_TCP, network_interface, [](mdns_answer *, const char *, int, int, void *) {}, nullptr, &search_id);
EXPECT_EQ(searched, ERR_OK);
if (searched == ERR_OK) {
mdns_search_stop(search_id);
}
mdns_resp_remove_netif(network_interface);
}

UNLOCK_TCPIP_CORE();
}

TEST_F(LwipPackage, BuffersNetworkMessagesInOrderAcrossMailboxWraparound)
{
sys_mbox_t mailbox{};
ASSERT_EQ(sys_mbox_new(&mailbox, 0), ERR_OK);

std::array<char, 255> messages{};
for (int burst = 0; burst < 2; ++burst) {
for (auto &message : messages) {
ASSERT_EQ(sys_mbox_trypost(&mailbox, &message), ERR_OK);
}
EXPECT_EQ(sys_mbox_trypost(&mailbox, &messages[0]), ERR_MEM);
for (auto &message : messages) {
void *received = nullptr;
ASSERT_EQ(sys_arch_mbox_tryfetch(&mailbox, &received), 0u);
EXPECT_EQ(received, &message);
}
EXPECT_EQ(sys_arch_mbox_tryfetch(&mailbox, nullptr), SYS_MBOX_EMPTY);
}

sys_mbox_free(&mailbox);
}

TEST_F(LwipPackage, AppliesTheConfiguredTcpipThreadPriority)
{
struct PriorityProbe {
KEVENT complete;
LONG priority;
} probe{};
KeInitializeEvent(&probe.complete, SynchronizationEvent, FALSE);
ASSERT_EQ(tcpip_callback([](void *context) {
auto *result = static_cast<PriorityProbe *>(context);
result->priority = KeQueryBasePriorityThread(KeGetCurrentThread());
KeSetEvent(&result->complete, IO_NO_INCREMENT, FALSE);
},
&probe),
ERR_OK);
ASSERT_EQ(KeWaitForSingleObject(&probe.complete, Executive, KernelMode, FALSE, nullptr), STATUS_SUCCESS);
EXPECT_EQ(probe.priority, 2);
}

} // namespace
Loading