<commit_before>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// 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.

//#define DEBUG // Debug supression

#include <os>
#include <list>
#include <net/inet4>
#include <vector>

using namespace std;
using namespace net;

std::shared_ptr<net::Inet4<VirtioNet> > inet;

void Service::start()
{
  // Assign an IP-address, using Hårek-mapping :-)
  auto& eth0 = hw::Dev::eth<0,VirtioNet>();
  auto& mac = eth0.mac();

  inet = std::make_shared<net::Inet4<VirtioNet>>(eth0);
  auto& timer = hw::PIT::instance();

  inet->network_config({{ mac.part[2],mac.part[3],mac.part[4],mac.part[5] }},
                       {{ 255,255,0,0 }}, // Netmask
                       {{ 10,0,0,1 }}, // Gateway
                       {{ 8,8,8,8}});  // DNS

  printf("Service IP address: %s \n", inet->ip_addr().str().c_str());

  // UDP
  UDP::port_t port = 4242;
  auto& sock = inet->udp().bind(port);

  sock.onRead([&] (UDP::Socket& conn, UDP::addr_t addr, UDP::port_t port,
                   const char* data, int len) -> int {
                string received = std::string(data,len-1);
                INFO("Test 2","Starting UDP-test (got UDP data from %s: %i: '%s')",
                     addr.str().c_str(), port, received.c_str());

                const int packets { 600 };

                string first_reply {string("Received '") + received +
                    "'. Expect " + to_string(packets) + " packets in 1s\n" };

                // Send the first packet, and then wait for ARP
                conn.sendto(addr, port, first_reply.c_str(), first_reply.size());

                timer.onTimeout(1s, [&conn, addr, port, data, len]() {
                    INFO("Test 2", "Trying to transmit %i UDP packets at maximum throttle", packets);
                    auto bufcount = inet->buffers_available();

                    for (int i = 0; i < packets; i++)
                      conn.sendto(addr, port, data, len);

                    CHECK(1,"UDP-transmission didn't panic");
                    auto bufcount2 = inet->buffers_available();

                    CHECKSERT(bufcount2 < bufcount,
                              "%i buffers available after transmission (Had %i). ",
                              bufcount2, bufcount);

                    INFO("Transmision tests","SUCCESS");
                  });

                return 0;

              });

  eth0.on_transmit_queue_available([](size_t s){
      CHECKSERT(s,"There is now room for %i packets in transmit queue", s);
    });


  timer.onTimeout(200ms,[=](){
      const int packets { 600 };
      INFO("Test 1", "Trying to transmit %i ethernet packets at maximum throttle", packets);
      for (int i=0; i < packets; i++){<commit_msg>Updated transmit test to use new UDP interface<commit_after>// This file is a part of the IncludeOS unikernel - www.includeos.org
//
// Copyright 2015 Oslo and Akershus University College of Applied Sciences
// and <NAME>
//
// 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.

//#define DEBUG // Debug supression

#include <os>
#include <list>
#include <net/inet4>
#include <vector>

using namespace std;
using namespace net;

std::shared_ptr<net::Inet4<VirtioNet> > inet;

void Service::start()
{
  // Assign an IP-address, using Hårek-mapping :-)
  auto& eth0 = hw::Dev::eth<0,VirtioNet>();
  auto& mac = eth0.mac();

  inet = std::make_shared<net::Inet4<VirtioNet>>(eth0);
  auto& timer = hw::PIT::instance();

  inet->network_config({{ mac.part[2],mac.part[3],mac.part[4],mac.part[5] }},
                       {{ 255,255,0,0 }}, // Netmask
                       {{ 10,0,0,1 }}, // Gateway
                       {{ 8,8,8,8}});  // DNS

  printf("Service IP address: %s \n", inet->ip_addr().str().c_str());

  // UDP
  UDP::port_t port = 4242;
  auto& conn = inet->udp().bind(port);

  conn.on_read([&] (UDP::addr_t addr, UDP::port_t port,
                   const char* data, int len) {
                string received = std::string(data,len-1);
                INFO("Test 2","Starting UDP-test (got UDP data from %s: %i: '%s')",
                     addr.str().c_str(), port, received.c_str());

                const int packets { 600 };

                string first_reply {string("Received '") + received +
                    "'. Expect " + to_string(packets) + " packets in 1s\n" };

                // Send the first packet, and then wait for ARP
                conn.sendto(addr, port, first_reply.c_str(), first_reply.size());

                timer.onTimeout(1s, [&conn, addr, port, data, len]() {
                    INFO("Test 2", "Trying to transmit %i UDP packets at maximum throttle", packets);
                    auto bufcount = inet->buffers_available();

                    for (int i = 0; i < packets; i++)
                      conn.sendto(addr, port, data, len);

                    CHECK(1,"UDP-transmission didn't panic");
                    auto bufcount2 = inet->buffers_available();

                    CHECKSERT(bufcount2 < bufcount,
                              "%i buffers available after transmission (Had %i). ",
                              bufcount2, bufcount);

                    INFO("Transmision tests","SUCCESS");
                  });

              });

  eth0.on_transmit_queue_available([](size_t s){
      CHECKSERT(s,"There is now room for %i packets in transmit queue", s);
    });


  timer.onTimeout(200ms,[=](){
      const int packets { 600 };
      INFO("Test 1", "Trying to transmit %i ethernet packets at maximum throttle", packets);
      for (int i=0; i < packets; i++){
{'fraction_non_alphanumeric': 0.13919140683276146, 'fraction_numerical': 0.01864836640310309, 'mean_word_length': 2.1863117870722433, 'pattern_counts': {'":': 0, '<': 29, '<?xml version=': 0, '>': 34, 'https://': 0, 'lorem ipsum': 0, 'www.': 4, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 2, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'dir': 'git-commits-cleaned', 'id': '5293227', 'n_tokens_mistral': 2099, 'n_tokens_neox': 1915, 'n_words': 609}