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
6 changes: 5 additions & 1 deletion api/net/stream_buffer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@ namespace net {

void on_read(size_t, ReadCallback cb) override {
m_on_read = std::move(cb);
signal_data();
}
void on_data(DataCallback cb) override {
m_on_data = std::move(cb);
signal_data();
}
size_t next_size() override;

Expand Down Expand Up @@ -205,7 +207,9 @@ namespace net {
// Pop each time, in case callback leads to another call here.
m_send_buffers.pop_front();
m_on_read(buf);
if (m_on_read == nullptr) { break; } //if calling m_on_read reset the callbacks exit
if (m_on_read == nullptr) {
break;
} //if calling m_on_read reset the callbacks exit
}
}
}
Expand Down
8 changes: 6 additions & 2 deletions api/net/tcp/connection_states.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -352,11 +352,15 @@ class Connection::LastAck : public State {
*/
virtual Result handle(Connection&, Packet_view& in) override;

inline virtual std::string to_string() const override {
std::string to_string() const override {
return "LAST-ACK";
};

inline virtual bool is_closing() const override {
bool is_closing() const override {
return true;
}

bool is_closed() const override {
return true;
}

Expand Down
5 changes: 3 additions & 2 deletions api/util/detail/alloc_pmr.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -155,7 +155,8 @@ namespace os::mem::detail {
std::size_t resource_capacity() {
if (cap_suballoc_ == 0)
{
return cap_total_ / (used_resources_ + os::mem::Pmr_pool::resource_division_offset);
auto div = cap_total_ / (used_resources_ + os::mem::Pmr_pool::resource_division_offset);
return std::min(div, allocatable());
}
return cap_suballoc_;
}
Expand Down Expand Up @@ -249,7 +250,7 @@ namespace os::mem {
//
Pmr_resource::Pmr_resource(Pool_ptr p) : pool_{p} {}
std::size_t Pmr_resource::capacity() {
return std::min(pool_->resource_capacity(), pool_->allocatable());
return pool_->resource_capacity();
}
std::size_t Pmr_resource::allocatable() {
auto cap = capacity();
Expand Down
41 changes: 41 additions & 0 deletions examples/transfer/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,41 @@
cmake_minimum_required(VERSION 2.8.9)

# IncludeOS install location
if (NOT DEFINED ENV{INCLUDEOS_PREFIX})
set(ENV{INCLUDEOS_PREFIX} /usr/local)
endif()
include($ENV{INCLUDEOS_PREFIX}/includeos/pre.service.cmake)
project (tcp)

# Human-readable name of your service
set(SERVICE_NAME "TCP Example Service")

# Name of your service binary
set(BINARY "tcp_example")

# Source files to be linked with OS library parts to form bootable image
set(SOURCES
service.cpp # ...add more here
)

# To add your own include paths:
# set(LOCAL_INCLUDES ".")

# Adding memdisk (expects my.disk to exist in current dir):
# set(MEMDISK ${CMAKE_SOURCE_DIR}/my.disk)

# DRIVERS / PLUGINS:

set(DRIVERS
virtionet # Virtio networking
# ... Others from IncludeOS/src/drivers
)

set(PLUGINS
# syslogd # Syslog over UDP
# ...others
)


# include service build script
include($ENV{INCLUDEOS_PREFIX}/includeos/post.service.cmake)
11 changes: 11 additions & 0 deletions examples/transfer/config.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@
{
"net" : [
{
"iface": 0,
"config": "static",
"address": "10.0.0.42",
"netmask": "255.255.255.0",
"gateway": "10.0.0.1"
}
]
}
18 changes: 18 additions & 0 deletions examples/transfer/linux/CMakeLists.txt
Original file line number Diff line number Diff line change
@@ -0,0 +1,18 @@
cmake_minimum_required(VERSION 2.8.9)
if (NOT DEFINED ENV{INCLUDEOS_PREFIX})
set(ENV{INCLUDEOS_PREFIX} /usr/local)
endif()
project (service C CXX)

# Human-readable name of your service
set(SERVICE_NAME "TCP Transfer From Linux")

# Name of your service binary
set(BINARY "tcp_linux")

# Source files to be linked with OS library parts to form bootable image
set(SOURCES
../service.cpp
)

include($ENV{INCLUDEOS_PREFIX}/includeos/linux.service.cmake)
2 changes: 2 additions & 0 deletions examples/transfer/send_file.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
#!/bin/bash
dd if=/dev/zero bs=1280 count=1048576 > /dev/tcp/10.0.0.42/81
37 changes: 37 additions & 0 deletions examples/transfer/server.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,37 @@
import socket
import sys

# Create a TCP/IP socket
sock = socket.socket(socket.AF_INET, socket.SOCK_STREAM)

# Bind the socket to the port
server_address = ('10.0.0.1', 1337)
print 'starting up on %s port %s' % server_address
sock.bind(server_address)

# Listen for incoming connections
sock.listen(5)

while True:
# Wait for a connection
print 'waiting for a connection'
connection, client_address = sock.accept()

try:
print 'connection from', client_address
bytes = 0

while True:
data = connection.recv(8192)
if data:
bytes += len(data)
#print 'received: %d' % len(data)
connection.sendall(data)
else:
print 'received %d bytes' % bytes
print 'closing', client_address
break

finally:
# Clean up the connection
connection.close()
92 changes: 92 additions & 0 deletions examples/transfer/service.cpp
Original file line number Diff line number Diff line change
@@ -0,0 +1,92 @@
// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015-2016 Oslo and Akershus University College of Applied Sciences
// and Alfred Bratterud
//
// Licensed under the Apache License, Version 2.0 (the "License");
// you may not use this file except in compliance with the License.
// You may obtain a copy of the License at
//
// http://www.apache.org/licenses/LICENSE-2.0
//
// Unless required by applicable law or agreed to in writing, software
// distributed under the License is distributed on an "AS IS" BASIS,
// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
// See the License for the specific language governing permissions and
// limitations under the License.

#include <os>
#include <net/inet>

/**
* An example to show incoming and outgoing TCP Connections.
* In this example, IncludeOS is listening on port 80.
*
* Data received on port 80 will be redirected to a
* outgoing connection to a (in this case) python server (server.py)
*
* Data received from the python server connection
* will be redirected back to the client.
*
* To try it out, use netcat to connect to this IncludeOS instance.
**/

using Connection_ptr = net::tcp::Connection_ptr;
using Disconnect = net::tcp::Connection::Disconnect;

// Address to our python server: 10.0.2.2:1337
// @note: This may have to be modified depending on network and server settings.
net::Socket python_server{ {10,0,0,1} , 1337};

void Service::start()
{
#ifdef USERSPACE_LINUX
extern void create_network_device(int N, const char* route, const char* ip);
create_network_device(0, "10.0.0.0/24", "10.0.0.1");
#endif
auto& inet = net::Super_stack::get(0);
inet.network_config(
{ 10, 0, 0, 42 }, // IP
{ 255,255,255, 0 }, // Netmask
{ 10, 0, 0, 1 }, // Gateway
{ 10, 0, 0, 1 }); // DNS

// Set up a TCP server on port 81
auto& server = inet.tcp().listen(81);
printf("Server listening: %s \n", server.local().to_string().c_str());

// When someone connects to our server
server.on_connect(
[&inet] (Connection_ptr client) {
printf("Connected [Client]: %s\n", client->to_string().c_str());
// Make an outgoing connection to our python server
auto outgoing = inet.tcp().connect(python_server);
// When outgoing connection to python sever is established
outgoing->on_connect(
[client] (Connection_ptr python) {
if (!python) {
printf("Connection failed!\n");
return;
}
printf("Connected [Python]: %s\n", python->to_string().c_str());

// Setup handlers for when data is received on client and python connection
// When client reads data
client->on_read(1024, [python](auto buf) {
python->write(buf);
});

// When client is disconnecting
client->on_disconnect([python](Connection_ptr, Disconnect reason) {
printf("Disconnected [Client]: %s\n", reason.to_string().c_str());
python->close();
});

// When python is disconnecting
python->on_disconnect([client](Connection_ptr, Disconnect reason) {
printf("Disconnected [Python]: %s\n", reason.to_string().c_str());
client->close();
});
}); // << onConnect (outgoing (python))
}); // << onConnect (client)
}
3 changes: 3 additions & 0 deletions examples/transfer/vm.json
Original file line number Diff line number Diff line change
@@ -0,0 +1,3 @@
{
"mem" : 128
}
72 changes: 38 additions & 34 deletions lib/microLB/micro_lb/balancer.cpp
Original file line number Diff line number Diff line change
Expand Up @@ -71,16 +71,21 @@ namespace microLB
auto& client = queue.front();
assert(client.conn != nullptr);
if (client.conn->is_connected()) {
// NOTE: explicitly want to copy buffers
net::Stream_ptr rval =
nodes.assign(std::move(client.conn));
if (rval == nullptr) {
// done with this queue item
queue.pop_front();
}
else {
// put connection back in queue item
client.conn = std::move(rval);
try {
// NOTE: explicitly want to copy buffers
net::Stream_ptr rval =
nodes.assign(std::move(client.conn));
if (rval == nullptr) {
// done with this queue item
queue.pop_front();
}
else {
// put connection back in queue item
client.conn = std::move(rval);
}
} catch (...) {
queue.pop_front(); // we have no choice
throw;
}
}
else {
Expand Down Expand Up @@ -278,12 +283,8 @@ namespace microLB
session.outgoing->reset_callbacks();
closed_sessions.push_back(session.self);

if (!cleanup_timer.is_running())
{
cleanup_timer.start(std::chrono::milliseconds(10),[this](){
this->destroy_sessions();
});
}
destroy_sessions();

session_cnt--;
LBOUT("Session %d closed (total = %d)\n", session.self, session_cnt);
}
Expand Down Expand Up @@ -322,7 +323,7 @@ namespace microLB
this->restart_active_check();
}
});
} catch (std::exception& e) {
} catch (const std::exception&) {
// do nothing, because might just be eph.ports used up
}
}
Expand Down Expand Up @@ -440,34 +441,37 @@ namespace microLB
: parent(n), self(idx), incoming(std::move(inc)),
outgoing(std::move(out))
{

incoming->on_data([this]() {
assert(this->is_alive());
while((this->incoming->next_size() > 0) and this->outgoing->is_writable())
{
this->outgoing->write(this->incoming->read_next());
}
});
incoming->on_data({this, &Session::flush_incoming});
incoming->on_close(
[&nodes = n, idx] () {
nodes.close_session(idx);
});

outgoing->on_data([this]() {
assert(this->is_alive());
while((this->outgoing->next_size() > 0) and this->incoming->is_writable())
{
this->incoming->write(this->outgoing->read_next());
}
});
outgoing->on_data({this, &Session::flush_outgoing});
outgoing->on_close(
[&nodes = n, idx] () {
nodes.close_session(idx);
});


}
bool Session::is_alive() const {
return incoming != nullptr;
}

void Session::flush_incoming()
{
assert(this->is_alive());
while((this->incoming->next_size() > 0) and this->outgoing->is_writable())
{
this->outgoing->write(this->incoming->read_next());
}
}

void Session::flush_outgoing()
{
assert(this->is_alive());
while((this->outgoing->next_size() > 0) and this->incoming->is_writable())
{
this->incoming->write(this->outgoing->read_next());
}
}
}
3 changes: 3 additions & 0 deletions lib/microLB/micro_lb/balancer.hpp
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,9 @@ namespace microLB
const int self;
net::Stream_ptr incoming;
net::Stream_ptr outgoing;

void flush_incoming();
void flush_outgoing();
};

struct Node {
Expand Down
Loading