xorp

io_link.hh

00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*-
00002 
00003 // Copyright (c) 2007-2011 XORP, Inc and Others
00004 //
00005 // This program is free software; you can redistribute it and/or modify
00006 // it under the terms of the GNU General Public License, Version 2, June
00007 // 1991 as published by the Free Software Foundation. Redistribution
00008 // and/or modification of this program under the terms of any other
00009 // version of the GNU General Public License is not permitted.
00010 // 
00011 // This program is distributed in the hope that it will be useful, but
00012 // WITHOUT ANY WARRANTY; without even the implied warranty of
00013 // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. For more details,
00014 // see the GNU General Public License, Version 2, a copy of which can be
00015 // found in the XORP LICENSE.gpl file.
00016 // 
00017 // XORP Inc, 2953 Bunker Hill Lane, Suite 204, Santa Clara, CA 95054, USA;
00018 // http://xorp.net
00019 
00020 
00021 
00022 #ifndef __FEA_IO_LINK_HH__
00023 #define __FEA_IO_LINK_HH__
00024 
00025 
00026 
00027 #include "fea_data_plane_manager.hh"
00028 
00029 //
00030 // I/O link raw communication API.
00031 //
00032 
00033 
00034 class EventLoop;
00035 class IfTree;
00036 class IfTreeInterface;
00037 class IfTreeVif;
00038 class IoLinkManager;
00039 class IoLinkReceiver;
00040 class Mac;
00041 
00042 
00049 class IoLink {
00050 public:
00064     IoLink(FeaDataPlaneManager& fea_data_plane_manager, const IfTree& iftree,
00065        const string& if_name, const string& vif_name,
00066        uint16_t ether_type, const string& filter_program);
00067 
00071     virtual ~IoLink();
00072 
00078     IoLinkManager& io_link_manager() { return _io_link_manager; }
00079 
00085     FeaDataPlaneManager& fea_data_plane_manager() { return _fea_data_plane_manager; }
00086 
00092     virtual bool is_running() const { return _is_running; }
00093 
00099     EventLoop&  eventloop() { return (_eventloop); }
00100 
00106     const IfTree& iftree() const { return (_iftree); }
00107 
00113     virtual const string& if_name() const { return (_if_name); }
00114 
00120     virtual const string& vif_name() const { return (_vif_name); }
00121 
00128     virtual uint16_t ether_type() const { return (_ether_type); }
00129 
00136     const string& filter_program() const { return (_filter_program); }
00137 
00143     IoLinkReceiver* io_link_receiver() { return (_io_link_receiver); }
00144 
00150     virtual void register_io_link_receiver(IoLinkReceiver* io_link_receiver);
00151 
00155     virtual void unregister_io_link_receiver();
00156 
00163     virtual int start(string& error_msg) = 0;
00164 
00171     virtual int stop(string& error_msg) = 0;
00172 
00180     virtual int join_multicast_group(const Mac& group, string& error_msg) = 0;
00181     
00189     virtual int leave_multicast_group(const Mac& group, string& error_msg) = 0;
00190 
00201     virtual int send_packet(const Mac&      src_address,
00202                 const Mac&      dst_address,
00203                 uint16_t        ether_type,
00204                 const vector<uint8_t>& payload,
00205                 string&     error_msg) = 0;
00206 
00207     //
00208     // Debug-related methods
00209     //
00210     
00218     bool    is_log_trace() const { return (_is_log_trace); }
00219     
00227     void    set_log_trace(bool is_enabled) { _is_log_trace = is_enabled; }
00228 
00229 protected:
00238     virtual void recv_packet(const Mac&     src_address,
00239                  const Mac&     dst_address,
00240                  uint16_t       ether_type,
00241                  const vector<uint8_t>& payload);
00242 
00249     void recv_ethernet_packet(const uint8_t* packet, size_t packet_size);
00250 
00262     int prepare_ethernet_packet(const Mac& src_address,
00263                 const Mac& dst_address,
00264                 uint16_t ether_type,
00265                 const vector<uint8_t>& payload,
00266                 vector<uint8_t>& packet,
00267                 string& error_msg);
00268 
00269     // Misc other state
00270     bool    _is_running;
00271 
00272     // Misc. constants
00273     static const uint32_t L2_MAX_PACKET_SIZE = (64*1024); // Max. packet size
00274     static const uint16_t ETHERNET_HEADER_SIZE      = 14;
00275     static const uint16_t ETHERNET_LENGTH_TYPE_THRESHOLD = 1536;
00276     static const uint16_t ETHERNET_MIN_FRAME_SIZE   = 60;   // Excl. CRC
00277 
00278 private:
00279     IoLinkManager&  _io_link_manager;   // The I/O Link manager
00280     FeaDataPlaneManager& _fea_data_plane_manager; // The data plane manager
00281     EventLoop&      _eventloop;     // The event loop to use
00282     const IfTree&   _iftree;        // The interface tree to use
00283     const string    _if_name;       // The interface name
00284     const string    _vif_name;      // The vif name
00285     const uint16_t  _ether_type;    // The EtherType protocol number
00286     const string    _filter_program;    // The filter program
00287     IoLinkReceiver* _io_link_receiver;  // The registered receiver
00288 
00289     bool        _is_log_trace;      // True if trace log is enabled
00290 };
00291 
00299 class IoLinkReceiver {
00300 public:
00304     IoLinkReceiver() {}
00305 
00309     virtual ~IoLinkReceiver() {}
00310 
00319     virtual void recv_packet(const Mac&     src_address,
00320                  const Mac&     dst_address,
00321                  uint16_t       ether_type,
00322                  const vector<uint8_t>& payload) = 0;
00323 };
00324 
00325 #endif // __FEA_IO_LINK_HH__
 All Classes Namespaces Functions Variables Typedefs Enumerations