|
| 1 | +// This file is a part of the IncludeOS unikernel - www.includeos.org |
| 2 | +// |
| 3 | +// Copyright 2015 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 | +// #define DEBUG // Debug supression |
| 19 | +// #define NO_INFO |
| 20 | + |
| 21 | +#include <os> |
| 22 | +#include <kernel/irq_manager.hpp> |
| 23 | +#include <list> |
| 24 | +#include <net/inet4> |
| 25 | +#include <net/router.hpp> |
| 26 | +#include <timers> |
| 27 | +#include <profile> |
| 28 | + |
| 29 | +#define USE_STACK_SAMPLING |
| 30 | + |
| 31 | +using namespace net; |
| 32 | +using namespace std::chrono; |
| 33 | + |
| 34 | +std::unique_ptr<Router<IP4> > router; |
| 35 | + |
| 36 | +bool route_checker(IP4::addr addr) { |
| 37 | + INFO("Route checker", "asked for route to IP %s", addr.to_string().c_str()); |
| 38 | + |
| 39 | + bool have_route = router->route_check(addr); |
| 40 | + |
| 41 | + INFO("Route checker", "The router says %i", have_route); |
| 42 | + |
| 43 | + if (have_route) |
| 44 | + INFO2("* Responding YES"); |
| 45 | + else |
| 46 | + INFO2("* Responding NO"); |
| 47 | + |
| 48 | + return have_route; |
| 49 | +} |
| 50 | + |
| 51 | +void ip_forward (Inet<IP4>& stack, IP4::IP_packet_ptr pckt) { |
| 52 | + |
| 53 | + // Packet could have been erroneously moved prior to this call |
| 54 | + if (not pckt) |
| 55 | + return; |
| 56 | + |
| 57 | + Inet<IP4>* route = router->get_first_interface(pckt->dst()); |
| 58 | + |
| 59 | + if (not route){ |
| 60 | + INFO("ip_fwd", "No route found for %s dropping\n", pckt->dst().to_string().c_str()); |
| 61 | + return; |
| 62 | + } |
| 63 | + |
| 64 | + if (route == &stack) { |
| 65 | + INFO("ip_fwd", "* Oh, this packet was for me, sow why was it forwarded here? \n"); |
| 66 | + return; |
| 67 | + } |
| 68 | + |
| 69 | + debug("ip_fwd %s transmitting packet to %s", ifname, route->ifname().c_str()); |
| 70 | + route->ip_obj().ship(std::move(pckt)); |
| 71 | +} |
| 72 | + |
| 73 | + |
| 74 | +void Service::start(const std::string&) |
| 75 | +{ |
| 76 | + auto& inet = Inet4::stack<0>(); |
| 77 | + inet.network_config({ 10, 0, 0, 42 }, // IP |
| 78 | + { 255, 255, 0, 0 }, // Netmask |
| 79 | + { 10, 0, 0, 1 } ); // Gateway |
| 80 | + |
| 81 | + INFO("Router","Interface 1 IP: %s\n", inet.ip_addr().str().c_str()); |
| 82 | + |
| 83 | + auto& inet2 = Inet4::stack<1>(); |
| 84 | + inet2.network_config({ 10, 42, 42, 43 }, // IP |
| 85 | + { 255, 255, 255, 0 }, // Netmask |
| 86 | + { 10, 42, 42, 2 } ); // Gateway |
| 87 | + |
| 88 | + INFO("Router","Interface2 IP: %s\n", inet2.ip_addr().str().c_str()); |
| 89 | + |
| 90 | + |
| 91 | + // IP Forwarding |
| 92 | + inet.ip_obj().set_packet_forwarding(ip_forward); |
| 93 | + inet2.ip_obj().set_packet_forwarding(ip_forward); |
| 94 | + |
| 95 | + // ARP Route checker |
| 96 | + inet.set_route_checker(route_checker); |
| 97 | + inet2.set_route_checker(route_checker); |
| 98 | + |
| 99 | + |
| 100 | + /** Some times it's nice to add dest. to arp-cache to avoid having it respond to arp */ |
| 101 | + // inet2.cache_link_ip({10,42,42,2}, {0x10,0x11, 0x12, 0x13, 0x14, 0x15}); |
| 102 | + // inet2.cache_link_ip({10,42,42,2}, {0x1e,0x5f,0x30,0x98,0x19,0x8b}); |
| 103 | + // inet2.cache_link_ip({10,42,42,2}, {0xc0,0x00, 0x10, 0x00, 0x00, 0x02}); |
| 104 | + |
| 105 | + // Routing table |
| 106 | + Router<IP4>::Routing_table routing_table{ |
| 107 | + {{10, 42, 42, 0 }, { 255, 255, 255, 0}, {10, 42, 42, 2}, inet2 , 1 }, |
| 108 | + {{10, 0, 0, 0 }, { 255, 255, 255, 0}, {10, 0, 0, 1}, inet , 1 } |
| 109 | + }; |
| 110 | + |
| 111 | + router = std::make_unique<Router<IP4>>(Super_stack::inet().ip4_stacks(), routing_table); |
| 112 | + |
| 113 | + INFO("Router", "Routing enabled - routing table:"); |
| 114 | + |
| 115 | + for (auto r : routing_table) |
| 116 | + INFO2("* %s/%i -> %s / %s, cost %i", r.net().str().c_str(), |
| 117 | + __builtin_popcount(r.netmask().whole), |
| 118 | + r.interface()->ifname().c_str(), |
| 119 | + r.gateway().str().c_str(), |
| 120 | + r.cost()); |
| 121 | + printf("\n"); |
| 122 | + INFO("Router","Service ready"); |
| 123 | +} |
0 commit comments