|
xorp
|
00001 // -*- c-basic-offset: 4; tab-width: 8; indent-tabs-mode: t -*- 00002 00003 // Copyright (c) 2001-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 #ifndef __FEA_IO_IP_MANAGER_HH__ 00022 #define __FEA_IO_IP_MANAGER_HH__ 00023 00024 00025 #include "libxorp/callback.hh" 00026 #include "libxorp/ipvx.hh" 00027 #include "libxorp/xorpfd.hh" 00028 00029 #include "fea_io.hh" 00030 #include "io_ip.hh" 00031 00032 class FeaDataPlaneManager; 00033 class FeaNode; 00034 class IoIpManager; 00035 00036 00040 struct IPvXHeaderInfo { 00041 string if_name; 00042 string vif_name; 00043 IPvX src_address; 00044 IPvX dst_address; 00045 uint8_t ip_protocol; 00046 int32_t ip_ttl; 00047 int32_t ip_tos; 00048 bool ip_router_alert; 00049 bool ip_internet_control; 00050 vector<uint8_t> ext_headers_type; 00051 vector<vector<uint8_t> > ext_headers_payload; 00052 }; 00053 00060 class IoIpComm : 00061 public NONCOPYABLE, 00062 public IoIpReceiver 00063 { 00064 public: 00068 class InputFilter { 00069 public: 00070 InputFilter(IoIpManager& io_ip_manager, 00071 const string& receiver_name, 00072 uint8_t ip_protocol) 00073 : _io_ip_manager(io_ip_manager), 00074 _receiver_name(receiver_name), 00075 _ip_protocol(ip_protocol) 00076 {} 00077 virtual ~InputFilter() {} 00078 00084 IoIpManager& io_ip_manager() { return (_io_ip_manager); } 00085 00091 const IoIpManager& io_ip_manager() const { return (_io_ip_manager); } 00092 00098 const string& receiver_name() const { return (_receiver_name); } 00099 00105 uint8_t ip_protocol() const { return (_ip_protocol); } 00106 00110 virtual void recv(const struct IPvXHeaderInfo& header, 00111 const vector<uint8_t>& payload) = 0; 00112 00117 virtual void recv_system_multicast_upcall(const vector<uint8_t>& payload) = 0; 00118 00126 virtual void bye() = 0; 00127 00128 private: 00129 IoIpManager& _io_ip_manager; 00130 string _receiver_name; 00131 uint8_t _ip_protocol; 00132 }; 00133 00137 class JoinedMulticastGroup { 00138 public: 00139 JoinedMulticastGroup(const string& if_name, const string& vif_name, 00140 const IPvX& group_address) 00141 : _if_name(if_name), 00142 _vif_name(vif_name), 00143 _group_address(group_address) 00144 {} 00145 #ifdef XORP_USE_USTL 00146 JoinedMulticastGroup() { } 00147 #endif 00148 virtual ~JoinedMulticastGroup() {} 00149 00150 const string& if_name() const { return _if_name; } 00151 const string& vif_name() const { return _vif_name; } 00152 const IPvX& group_address() const { return _group_address; } 00153 00161 bool operator<(const JoinedMulticastGroup& other) const { 00162 if (_if_name != other._if_name) 00163 return (_if_name < other._if_name); 00164 if (_vif_name != other._vif_name) 00165 return (_vif_name < other._vif_name); 00166 return (_group_address < other._group_address); 00167 } 00168 00176 bool operator==(const JoinedMulticastGroup& other) const { 00177 return ((_if_name == other._if_name) && 00178 (_vif_name == other._vif_name) && 00179 (_group_address == other._group_address)); 00180 } 00181 00187 void add_receiver(const string& receiver_name) { 00188 _receivers.insert(receiver_name); 00189 } 00190 00196 void delete_receiver(const string& receiver_name) { 00197 _receivers.erase(receiver_name); 00198 } 00199 00203 bool empty() const { return _receivers.empty(); } 00204 00205 set<string>& get_receivers() { return _receivers; } 00206 00207 private: 00208 string _if_name; 00209 string _vif_name; 00210 IPvX _group_address; 00211 set<string> _receivers; 00212 }; 00213 00214 public: 00225 IoIpComm(IoIpManager& io_ip_manager, const IfTree& iftree, int family, 00226 uint8_t ip_protocol); 00227 00231 virtual ~IoIpComm(); 00232 00236 void allocate_io_ip_plugins(); 00237 00241 void deallocate_io_ip_plugins(); 00242 00248 void allocate_io_ip_plugin(FeaDataPlaneManager* fea_data_plane_manager); 00249 00255 void deallocate_io_ip_plugin(FeaDataPlaneManager* fea_data_plane_manager); 00256 00260 void start_io_ip_plugins(); 00261 00265 void stop_io_ip_plugins(); 00266 00277 int add_filter(InputFilter* filter); 00278 00285 int remove_filter(InputFilter* filter); 00286 00290 bool no_input_filters() const { return _input_filters.empty(); } 00291 00318 int send_packet(const string& if_name, 00319 const string& vif_name, 00320 const IPvX& src_address, 00321 const IPvX& dst_address, 00322 int32_t ip_ttl, 00323 int32_t ip_tos, 00324 bool ip_router_alert, 00325 bool ip_internet_control, 00326 const vector<uint8_t>& ext_headers_type, 00327 const vector<vector<uint8_t> >& ext_headers_payload, 00328 const vector<uint8_t>& payload, 00329 string& error_msg); 00330 00353 virtual void recv_packet(const string& if_name, 00354 const string& vif_name, 00355 const IPvX& src_address, 00356 const IPvX& dst_address, 00357 int32_t ip_ttl, 00358 int32_t ip_tos, 00359 bool ip_router_alert, 00360 bool ip_internet_control, 00361 const vector<uint8_t>& ext_headers_type, 00362 const vector<vector<uint8_t> >& ext_headers_payload, 00363 const vector<uint8_t>& payload); 00364 00373 virtual void recv_system_multicast_upcall(const vector<uint8_t>& payload); 00374 00385 int join_multicast_group(const string& if_name, 00386 const string& vif_name, 00387 const IPvX& group_address, 00388 const string& receiver_name, 00389 string& error_msg); 00390 00402 int leave_multicast_group(const string& if_name, 00403 const string& vif_name, 00404 const IPvX& group_address, 00405 const string& receiver_name, 00406 string& error_msg); 00407 00417 int leave_all_multicast_groups(const string& if_name, 00418 const string& vif_name, 00419 string& error_msg); 00420 00426 uint8_t ip_protocol() const { return (_ip_protocol); } 00427 00434 XorpFd first_valid_mcast_protocol_fd_in(); 00435 00436 private: 00437 IoIpComm(const IoIpComm&); // Not implemented. 00438 IoIpComm& operator=(const IoIpComm&); // Not implemented. 00439 00440 IoIpManager& _io_ip_manager; 00441 const IfTree& _iftree; 00442 const int _family; 00443 const uint8_t _ip_protocol; 00444 00445 typedef list<pair<FeaDataPlaneManager*, IoIp*> >IoIpPlugins; 00446 IoIpPlugins _io_ip_plugins; 00447 00448 list<InputFilter*> _input_filters; 00449 typedef map<JoinedMulticastGroup, JoinedMulticastGroup> JoinedGroupsTable; 00450 JoinedGroupsTable _joined_groups_table; 00451 }; 00452 00457 class IoIpManagerReceiver { 00458 public: 00462 virtual ~IoIpManagerReceiver() {} 00463 00473 virtual void recv_event(const string& receiver_name, 00474 const struct IPvXHeaderInfo& header, 00475 const vector<uint8_t>& payload) = 0; 00476 }; 00477 00488 class IoIpManager : public IoIpManagerReceiver, 00489 public InstanceWatcher { 00490 public: 00491 typedef XorpCallback2<int, const uint8_t*, size_t>::RefPtr UpcallReceiverCb; 00492 00496 IoIpManager(FeaNode& fea_node, const IfTree& iftree); 00497 00501 virtual ~IoIpManager(); 00502 00531 int send(const string& if_name, 00532 const string& vif_name, 00533 const IPvX& src_address, 00534 const IPvX& dst_address, 00535 uint8_t ip_protocol, 00536 int32_t ip_ttl, 00537 int32_t ip_tos, 00538 bool ip_router_alert, 00539 bool ip_internet_control, 00540 const vector<uint8_t>& ext_headers_type, 00541 const vector<vector<uint8_t> >& ext_headers_payload, 00542 const vector<uint8_t>& payload, 00543 string& error_msg); 00544 00562 int register_receiver(int family, 00563 const string& receiver_name, 00564 const string& if_name, 00565 const string& vif_name, 00566 uint8_t ip_protocol, 00567 bool enable_multicast_loopback, 00568 string& error_msg); 00569 00585 int unregister_receiver(int family, 00586 const string& receiver_name, 00587 const string& if_name, 00588 const string& vif_name, 00589 uint8_t ip_protocol, 00590 string& error_msg); 00591 00605 int join_multicast_group(const string& receiver_name, 00606 const string& if_name, 00607 const string& vif_name, 00608 uint8_t ip_protocol, 00609 const IPvX& group_address, 00610 string& error_msg); 00611 00626 int leave_multicast_group(const string& receiver_name, 00627 const string& if_name, 00628 const string& vif_name, 00629 uint8_t ip_protocol, 00630 const IPvX& group_address, 00631 string& error_msg); 00632 00634 int leave_all_multicast_groups(const string& if_name, 00635 const string& vif_name, 00636 string& error_msg); 00637 00654 int register_system_multicast_upcall_receiver(int family, 00655 uint8_t ip_protocol, 00656 IoIpManager::UpcallReceiverCb receiver_cb, 00657 XorpFd& mcast_receiver_fd, 00658 string& error_msg); 00659 00672 int unregister_system_multicast_upcall_receiver(int family, 00673 uint8_t ip_protocol, 00674 string& error_msg); 00675 00683 void recv_event(const string& receiver_name, 00684 const struct IPvXHeaderInfo& header, 00685 const vector<uint8_t>& payload); 00686 00692 void instance_birth(const string& instance_name); 00693 00699 void instance_death(const string& instance_name); 00700 00705 void set_io_ip_manager_receiver(IoIpManagerReceiver* v) { 00706 _io_ip_manager_receiver = v; 00707 } 00708 00714 const IfTree& iftree() const { return _iftree; } 00715 00724 int register_data_plane_manager(FeaDataPlaneManager* fea_data_plane_manager, 00725 bool is_exclusive); 00726 00733 int unregister_data_plane_manager(FeaDataPlaneManager* fea_data_plane_manager); 00734 00740 list<FeaDataPlaneManager*>& fea_data_plane_managers() { 00741 return _fea_data_plane_managers; 00742 } 00743 00744 private: 00745 typedef map<uint8_t, IoIpComm*> CommTable; 00746 typedef multimap<string, IoIpComm::InputFilter*> FilterBag; 00747 00754 CommTable& comm_table_by_family(int family); 00755 00762 FilterBag& filters_by_family(int family); 00763 00770 void erase_filters_by_receiver_name(int family, 00771 const string& receiver_name); 00772 00780 bool has_filter_by_receiver_name(const string& receiver_name) const; 00781 00792 void erase_filters(CommTable& comm_table, FilterBag& filters, 00793 const FilterBag::iterator& begin, 00794 const FilterBag::iterator& end); 00795 00796 FeaNode& _fea_node; 00797 EventLoop& _eventloop; 00798 const IfTree& _iftree; 00799 00800 // Collection of IP communication handlers keyed by protocol. 00801 CommTable _comm_table4; 00802 CommTable _comm_table6; 00803 00804 // Collection of input filters created by IoIpManager 00805 FilterBag _filters4; 00806 FilterBag _filters6; 00807 00808 IoIpManagerReceiver* _io_ip_manager_receiver; 00809 00810 list<FeaDataPlaneManager*> _fea_data_plane_managers; 00811 }; 00812 00813 #endif // __FEA_IO_IP_MANAGER_HH__