Configuration.cc
Go to the documentation of this file.
1 //
2 // Copyright (c) 2020 Fraunhofer Institute for Applied Information Technology (FIT)
3 // Network Research Group (NET)
4 // Schloss Birlinghoven, 53754 Sankt Augustin, GERMANY
5 // Contact: support@wiback.org
6 //
7 // This file is part of the SENF code tree.
8 // It is licensed under the 3-clause BSD License (aka New BSD License).
9 // See LICENSE.txt in the top level directory for details or visit
10 // https://opensource.org/licenses/BSD-3-Clause
11 //
12 
13 
14 extern "C" {
15  #include <sched.h>
16  #include <sys/mman.h>
17  #include <sys/utsname.h>
18 }
19 
20 // Custom includes
21 #include <senf/Utils/membind.hh>
22 #include <senf/Utils/hexdump.hh>
23 #include <senf/Utils/Logger.hh>
24 #include <senf/Utils/Console.hh>
26 #include "Configuration.hh"
27 
28 #define prefix_
29 //-/////////////////////////////////////////////////////////////////////////////////////////////////
30 
32  verbose( false),
33  numPackets(0),
34  pktsPerSecond( 100),
35  pktSize( 64),
36  interface( "eth0"),
37  destination( senf::MACAddress::Broadcast)
38 {
39  namespace fty = senf::console::factory;
40  namespace kw = senf::console::kw;
41  senf::console::DirectoryNode & initDir (senf::console::root().add("init", fty::Directory()));
42  initDir.add("help", fty::Command( &Configuration::help, this));
43  initDir.add("version", fty::Command( &Configuration::version, this));
44  initDir.add("realtime-scheduling", fty::Command( &Configuration::enableRealtimeScheduling, this));
45  initDir.add("verbose", fty::Variable( verbose));
46  initDir.add("num-packets", fty::Variable( numPackets));
47  initDir.add("packets-per-second", fty::Variable( pktsPerSecond));
48  initDir.add("packet-size", fty::Variable( pktSize));
49  initDir.add("interface", fty::Variable( interface));
50  initDir.add("destination", fty::Variable( destination));
51 
52  // always turn those on, where available
53  enableHighresTimers();
54 }
55 
57 {
58  senf::console::DirectoryNode::ChildrenRange range(senf::console::root()["init"].children());
59 
60  senf::console::DirectoryNode::ChildrenRange::iterator it;
61  for (it = range.begin(); it != range.end(); it++) {
62  std::cerr << it->first << ": ";
63  it->second->help( std::cout);
64  }
65 
66  exit(0);
67 }
68 
70 {
71  std::cerr << "0.1" << std::endl;
72  exit(0);
73 }
74 
76 {
77  // switching to scheduler highres timer
78  if (senf::scheduler::haveScalableHiresTimers())
79  SENF_LOG((senf::log::IMPORTANT)("using 'timerfd' hi-res timer."));
80  else
81  SENF_LOG((senf::log::IMPORTANT)("using 'posix' hi-res timer."));
82  senf::scheduler::hiresTimers();
83 }
84 
86 {
87  // switching to kernel realtime scheduling
88  struct sched_param param;
89  param.sched_priority = sched_get_priority_max(SCHED_FIFO);
90  if (sched_setscheduler(0, SCHED_FIFO, &param) !=0 ) {
91  SENF_LOG((senf::log::IMPORTANT)("WARNING: unable to switch to realtime mode "));
92  } else {
93  mlockall(MCL_FUTURE);
94  SENF_LOG((senf::log::IMPORTANT) ("switching to realtime mode (SCHED_FIFO)"));
95  }
96 }
97 
98 prefix_ bool Configuration::parse( int argc, char const *argv[])
99 {
100  namespace fty = senf::console::factory;
101  namespace kw = senf::console::kw;
102  senf::console::DirectoryNode & initDir (senf::console::root()["init"]);
103 
104  senf::console::ProgramOptions cmdlineOptions ( argc, argv, initDir);
105 
106  try {
107  // now we parse the filled up configBundle
108  cmdlineOptions.parse( initDir);
109  } catch (senf::console::SyntaxErrorException const & e) {
110  std::cerr << e.message() << std::endl;
111  return false;
112  }
113  catch (senf::ExceptionMixin & ex) {
114  std::cerr << "Exception occurred during configuration parsing: "
115  << ex.message() << std::endl
116  << ex.backtrace() << std::endl;
117  return false;
118  }
119 
120  return true;
121 }
std::string backtrace() const
boost::iterator_range< ChildMap::const_iterator > ChildrenRange
NodeType & add(std::string const &name, boost::shared_ptr< NodeType > node)
bool parse(int argc, char const *argv[])
void enableHighresTimers()
std::string message() const
#define prefix_
#define SENF_LOG(args)
void enableRealtimeScheduling()