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