Line data Source code
1 : /* 2 : * Copyright (c) 2013 Juniper Networks, Inc. All rights reserved. 3 : * Copyright Contributors to the OpenSDN project 4 : */ 5 : 6 : #include <arpa/inet.h> 7 : #include <netinet/in.h> 8 : #include <netinet/tcp.h> 9 : #include <netinet/udp.h> 10 : 11 : #include <virtual_machine_types.h> 12 : #include <virtual_network_types.h> 13 : #include <vrouter_types.h> 14 : #include <uve/l4_port_bitmap.h> 15 : 16 : #include <oper/interface_common.h> 17 : #include <oper/vm.h> 18 : #include <oper/vn.h> 19 : #include <oper/mirror_table.h> 20 : #include <cmn/agent_cmn.h> 21 : #include <uve/agent_uve_base.h> 22 : #include "pkt/flow_proto.h" 23 : 24 : using namespace std; 25 : 26 : //////////////////////////////////////////////////////////////////////////// 27 : // Routines to manage the L4 Port Bitmaps 28 : //////////////////////////////////////////////////////////////////////////// 29 1638 : L4PortBitmap::L4PortBitmap() 30 1638 : : tcp_sport_(), tcp_dport_(), udp_sport_(), udp_dport_() { 31 1638 : } 32 : 33 1638 : L4PortBitmap::~L4PortBitmap() { 34 1638 : } 35 : 36 401 : void L4PortBitmap::Reset() { 37 401 : tcp_sport_.Reset(); 38 401 : tcp_dport_.Reset(); 39 401 : udp_sport_.Reset(); 40 401 : udp_dport_.Reset(); 41 401 : } 42 : 43 1604 : void L4PortBitmap::PortBitmap::Reset() { 44 14436 : for (int i = 0; i < L4PortBitmap::kBmapCount; i++) { 45 12832 : bitmap_[i] = bitmap_old_[i] = 0; 46 : } 47 412228 : for (int i = 0; i < L4PortBitmap::kBucketCount; i++) { 48 410624 : counts_[i] = 0; 49 : } 50 1604 : } 51 : 52 1136 : void L4PortBitmap::PortBitmap::AddPort(uint16_t port) { 53 1136 : int idx = port / kBucketCount; 54 1136 : counts_[idx]++; 55 1136 : if (counts_[idx] == 1) { 56 84 : bitmap_[idx / kBitsPerEntry] |= (1 << (idx % kBitsPerEntry)); 57 : } 58 1136 : } 59 : 60 0 : void L4PortBitmap::PortBitmap::Encode(std::vector<uint32_t> &bmap) { 61 0 : for (int i = 0; i < L4PortBitmap::kBmapCount; i++) { 62 0 : bmap.push_back(bitmap_[i]); 63 : } 64 0 : } 65 : 66 0 : bool L4PortBitmap::PortBitmap::Sync(std::vector<uint32_t> &bmap) { 67 0 : bool changed = false; 68 0 : for (int i = 0; i < kBmapCount; i++) { 69 0 : if (bitmap_[i] != bitmap_old_[i]) { 70 0 : bitmap_old_[i] = bitmap_[i]; 71 0 : changed = true; 72 : } 73 : } 74 : 75 0 : if (changed) { 76 0 : for (int i = 0; i < L4PortBitmap::kBmapCount; i++) { 77 0 : bmap.push_back(bitmap_old_[i]); 78 : } 79 : } 80 : 81 0 : return changed; 82 : } 83 : 84 719 : void L4PortBitmap::AddPort(uint8_t proto, uint16_t sport, uint16_t dport) { 85 719 : if (proto == IPPROTO_UDP) { 86 548 : udp_sport_.AddPort(sport); 87 548 : udp_dport_.AddPort(dport); 88 : } 89 : 90 719 : if (proto == IPPROTO_TCP) { 91 20 : tcp_sport_.AddPort(sport); 92 20 : tcp_dport_.AddPort(dport); 93 : } 94 719 : } 95 : 96 0 : void L4PortBitmap::Encode(PortBucketBitmap &bmap) { 97 0 : std::vector<uint32_t> tmp; 98 0 : tcp_sport_.Encode(tmp); 99 0 : bmap.set_tcp_sport_bitmap(tmp); 100 : 101 0 : tmp.clear(); 102 0 : tcp_dport_.Encode(tmp); 103 0 : bmap.set_tcp_dport_bitmap(tmp); 104 : 105 0 : tmp.clear(); 106 0 : udp_sport_.Encode(tmp); 107 0 : bmap.set_udp_sport_bitmap(tmp); 108 : 109 0 : tmp.clear(); 110 0 : udp_dport_.Encode(tmp); 111 0 : bmap.set_udp_dport_bitmap(tmp); 112 0 : } 113 :