Skip to content

Commit 39a3790

Browse files
authored
Merge pull request #10 from AndreasAakesson/tcp_rcvwnd
Fix test
2 parents e3a098c + 948f230 commit 39a3790

19 files changed

Lines changed: 296 additions & 46 deletions

File tree

api/net/stream_buffer.hpp

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -22,9 +22,11 @@ namespace net {
2222

2323
void on_read(size_t, ReadCallback cb) override {
2424
m_on_read = std::move(cb);
25+
signal_data();
2526
}
2627
void on_data(DataCallback cb) override {
2728
m_on_data = std::move(cb);
29+
signal_data();
2830
}
2931
size_t next_size() override;
3032

@@ -205,7 +207,9 @@ namespace net {
205207
// Pop each time, in case callback leads to another call here.
206208
m_send_buffers.pop_front();
207209
m_on_read(buf);
208-
if (m_on_read == nullptr) { break; } //if calling m_on_read reset the callbacks exit
210+
if (m_on_read == nullptr) {
211+
break;
212+
} //if calling m_on_read reset the callbacks exit
209213
}
210214
}
211215
}

api/net/tcp/connection_states.hpp

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -352,11 +352,15 @@ class Connection::LastAck : public State {
352352
*/
353353
virtual Result handle(Connection&, Packet_view& in) override;
354354

355-
inline virtual std::string to_string() const override {
355+
std::string to_string() const override {
356356
return "LAST-ACK";
357357
};
358358

359-
inline virtual bool is_closing() const override {
359+
bool is_closing() const override {
360+
return true;
361+
}
362+
363+
bool is_closed() const override {
360364
return true;
361365
}
362366

api/util/detail/alloc_pmr.hpp

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -155,7 +155,8 @@ namespace os::mem::detail {
155155
std::size_t resource_capacity() {
156156
if (cap_suballoc_ == 0)
157157
{
158-
return cap_total_ / (used_resources_ + os::mem::Pmr_pool::resource_division_offset);
158+
auto div = cap_total_ / (used_resources_ + os::mem::Pmr_pool::resource_division_offset);
159+
return std::min(div, allocatable());
159160
}
160161
return cap_suballoc_;
161162
}
@@ -249,7 +250,7 @@ namespace os::mem {
249250
//
250251
Pmr_resource::Pmr_resource(Pool_ptr p) : pool_{p} {}
251252
std::size_t Pmr_resource::capacity() {
252-
return std::min(pool_->resource_capacity(), pool_->allocatable());
253+
return pool_->resource_capacity();
253254
}
254255
std::size_t Pmr_resource::allocatable() {
255256
auto cap = capacity();

examples/transfer/CMakeLists.txt

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
cmake_minimum_required(VERSION 2.8.9)
2+
3+
# IncludeOS install location
4+
if (NOT DEFINED ENV{INCLUDEOS_PREFIX})
5+
set(ENV{INCLUDEOS_PREFIX} /usr/local)
6+
endif()
7+
include($ENV{INCLUDEOS_PREFIX}/includeos/pre.service.cmake)
8+
project (tcp)
9+
10+
# Human-readable name of your service
11+
set(SERVICE_NAME "TCP Example Service")
12+
13+
# Name of your service binary
14+
set(BINARY "tcp_example")
15+
16+
# Source files to be linked with OS library parts to form bootable image
17+
set(SOURCES
18+
service.cpp # ...add more here
19+
)
20+
21+
# To add your own include paths:
22+
# set(LOCAL_INCLUDES ".")
23+
24+
# Adding memdisk (expects my.disk to exist in current dir):
25+
# set(MEMDISK ${CMAKE_SOURCE_DIR}/my.disk)
26+
27+
# DRIVERS / PLUGINS:
28+
29+
set(DRIVERS
30+
virtionet # Virtio networking
31+
# ... Others from IncludeOS/src/drivers
32+
)
33+
34+
set(PLUGINS
35+
# syslogd # Syslog over UDP
36+
# ...others
37+
)
38+
39+
40+
# include service build script
41+
include($ENV{INCLUDEOS_PREFIX}/includeos/post.service.cmake)

examples/transfer/config.json

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"net" : [
3+
{
4+
"iface": 0,
5+
"config": "static",
6+
"address": "10.0.0.42",
7+
"netmask": "255.255.255.0",
8+
"gateway": "10.0.0.1"
9+
}
10+
]
11+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
cmake_minimum_required(VERSION 2.8.9)
2+
if (NOT DEFINED ENV{INCLUDEOS_PREFIX})
3+
set(ENV{INCLUDEOS_PREFIX} /usr/local)
4+
endif()
5+
project (service C CXX)
6+
7+
# Human-readable name of your service
8+
set(SERVICE_NAME "TCP Transfer From Linux")
9+
10+
# Name of your service binary
11+
set(BINARY "tcp_linux")
12+
13+
# Source files to be linked with OS library parts to form bootable image
14+
set(SOURCES
15+
../service.cpp
16+
)
17+
18+
include($ENV{INCLUDEOS_PREFIX}/includeos/linux.service.cmake)

examples/transfer/send_file.sh

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,2 @@
1+
#!/bin/bash
2+
dd if=/dev/zero bs=1280 count=1048576 > /dev/tcp/10.0.0.42/81

examples/transfer/server.py

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,37 @@
1+
import socket
2+
import sys
3+
4+
# Create a TCP/IP socket
5+
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)
6+
7+
# Bind the socket to the port
8+
server_address = ('10.0.0.1', 1337)
9+
print 'starting up on %s port %s' % server_address
10+
sock.bind(server_address)
11+
12+
# Listen for incoming connections
13+
sock.listen(5)
14+
15+
while True:
16+
# Wait for a connection
17+
print 'waiting for a connection'
18+
connection, client_address = sock.accept()
19+
20+
try:
21+
print 'connection from', client_address
22+
bytes = 0
23+
24+
while True:
25+
data = connection.recv(8192)
26+
if data:
27+
bytes += len(data)
28+
#print 'received: %d' % len(data)
29+
connection.sendall(data)
30+
else:
31+
print 'received %d bytes' % bytes
32+
print 'closing', client_address
33+
break
34+
35+
finally:
36+
# Clean up the connection
37+
connection.close()

examples/transfer/service.cpp

Lines changed: 92 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,92 @@
1+
// This file is a part of the IncludeOS unikernel - www.includeos.org
2+
//
3+
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
4+
// and Alfred Bratterud
5+
//
6+
// Licensed under the Apache License, Version 2.0 (the "License");
7+
// you may not use this file except in compliance with the License.
8+
// You may obtain a copy of the License at
9+
//
10+
// http://www.apache.org/licenses/LICENSE-2.0
11+
//
12+
// Unless required by applicable law or agreed to in writing, software
13+
// distributed under the License is distributed on an "AS IS" BASIS,
14+
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
15+
// See the License for the specific language governing permissions and
16+
// limitations under the License.
17+
18+
#include <os>
19+
#include <net/inet>
20+
21+
/**
22+
* An example to show incoming and outgoing TCP Connections.
23+
* In this example, IncludeOS is listening on port 80.
24+
*
25+
* Data received on port 80 will be redirected to a
26+
* outgoing connection to a (in this case) python server (server.py)
27+
*
28+
* Data received from the python server connection
29+
* will be redirected back to the client.
30+
*
31+
* To try it out, use netcat to connect to this IncludeOS instance.
32+
**/
33+
34+
using Connection_ptr = net::tcp::Connection_ptr;
35+
using Disconnect = net::tcp::Connection::Disconnect;
36+
37+
// Address to our python server: 10.0.2.2:1337
38+
// @note: This may have to be modified depending on network and server settings.
39+
net::Socket python_server{ {10,0,0,1} , 1337};
40+
41+
void Service::start()
42+
{
43+
#ifdef USERSPACE_LINUX
44+
extern void create_network_device(int N, const char* route, const char* ip);
45+
create_network_device(0, "10.0.0.0/24", "10.0.0.1");
46+
#endif
47+
auto& inet = net::Super_stack::get(0);
48+
inet.network_config(
49+
{ 10, 0, 0, 42 }, // IP
50+
{ 255,255,255, 0 }, // Netmask
51+
{ 10, 0, 0, 1 }, // Gateway
52+
{ 10, 0, 0, 1 }); // DNS
53+
54+
// Set up a TCP server on port 81
55+
auto& server = inet.tcp().listen(81);
56+
printf("Server listening: %s \n", server.local().to_string().c_str());
57+
58+
// When someone connects to our server
59+
server.on_connect(
60+
[&inet] (Connection_ptr client) {
61+
printf("Connected [Client]: %s\n", client->to_string().c_str());
62+
// Make an outgoing connection to our python server
63+
auto outgoing = inet.tcp().connect(python_server);
64+
// When outgoing connection to python sever is established
65+
outgoing->on_connect(
66+
[client] (Connection_ptr python) {
67+
if (!python) {
68+
printf("Connection failed!\n");
69+
return;
70+
}
71+
printf("Connected [Python]: %s\n", python->to_string().c_str());
72+
73+
// Setup handlers for when data is received on client and python connection
74+
// When client reads data
75+
client->on_read(1024, [python](auto buf) {
76+
python->write(buf);
77+
});
78+
79+
// When client is disconnecting
80+
client->on_disconnect([python](Connection_ptr, Disconnect reason) {
81+
printf("Disconnected [Client]: %s\n", reason.to_string().c_str());
82+
python->close();
83+
});
84+
85+
// When python is disconnecting
86+
python->on_disconnect([client](Connection_ptr, Disconnect reason) {
87+
printf("Disconnected [Python]: %s\n", reason.to_string().c_str());
88+
client->close();
89+
});
90+
}); // << onConnect (outgoing (python))
91+
}); // << onConnect (client)
92+
}

examples/transfer/vm.json

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
{
2+
"mem" : 128
3+
}

0 commit comments

Comments
 (0)