OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
l4_port_bitmap.cc
Go to the documentation of this file.
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 
26 // Routines to manage the L4 Port Bitmaps
29  : tcp_sport_(), tcp_dport_(), udp_sport_(), udp_dport_() {
30 }
31 
33 }
34 
36  tcp_sport_.Reset();
37  tcp_dport_.Reset();
38  udp_sport_.Reset();
39  udp_dport_.Reset();
40 }
41 
43  for (int i = 0; i < L4PortBitmap::kBmapCount; i++) {
44  bitmap_[i] = bitmap_old_[i] = 0;
45  }
46  for (int i = 0; i < L4PortBitmap::kBucketCount; i++) {
47  counts_[i] = 0;
48  }
49 }
50 
51 void L4PortBitmap::PortBitmap::AddPort(uint16_t port) {
52  int idx = port / kBucketCount;
53  counts_[idx]++;
54  if (counts_[idx] == 1) {
55  bitmap_[idx / kBitsPerEntry] |= (1 << (idx % kBitsPerEntry));
56  }
57 }
58 
59 void L4PortBitmap::PortBitmap::Encode(std::vector<uint32_t> &bmap) {
60  for (int i = 0; i < L4PortBitmap::kBmapCount; i++) {
61  bmap.push_back(bitmap_[i]);
62  }
63 }
64 
65 bool L4PortBitmap::PortBitmap::Sync(std::vector<uint32_t> &bmap) {
66  bool changed = false;
67  for (int i = 0; i < kBmapCount; i++) {
68  if (bitmap_[i] != bitmap_old_[i]) {
69  bitmap_old_[i] = bitmap_[i];
70  changed = true;
71  }
72  }
73 
74  if (changed) {
75  for (int i = 0; i < L4PortBitmap::kBmapCount; i++) {
76  bmap.push_back(bitmap_old_[i]);
77  }
78  }
79 
80  return changed;
81 }
82 
83 void L4PortBitmap::AddPort(uint8_t proto, uint16_t sport, uint16_t dport) {
84  if (proto == IPPROTO_UDP) {
85  udp_sport_.AddPort(sport);
86  udp_dport_.AddPort(dport);
87  }
88 
89  if (proto == IPPROTO_TCP) {
90  tcp_sport_.AddPort(sport);
91  tcp_dport_.AddPort(dport);
92  }
93 }
94 
95 void L4PortBitmap::Encode(PortBucketBitmap &bmap) {
96  std::vector<uint32_t> tmp;
97  tcp_sport_.Encode(tmp);
98  bmap.set_tcp_sport_bitmap(tmp);
99 
100  tmp.clear();
101  tcp_dport_.Encode(tmp);
102  bmap.set_tcp_dport_bitmap(tmp);
103 
104  tmp.clear();
105  udp_sport_.Encode(tmp);
106  bmap.set_udp_sport_bitmap(tmp);
107 
108  tmp.clear();
109  udp_dport_.Encode(tmp);
110  bmap.set_udp_dport_bitmap(tmp);
111 }
112 
static const uint16_t kBmapCount
static const uint16_t kBucketCount
void AddPort(uint8_t proto, uint16_t sport, uint16_t dport)
PortBitmap udp_sport_
static const uint16_t kBitsPerEntry
uint32_t bitmap_old_[kBmapCount]
PortBitmap udp_dport_
PortBitmap tcp_sport_
void Encode(std::vector< uint32_t > &bmap)
uint32_t bitmap_[kBmapCount]
PortBitmap tcp_dport_
bool Sync(std::vector< uint32_t > &bmap)
void AddPort(uint16_t port)
uint32_t counts_[kBucketCount]
void Encode(PortBucketBitmap &bmap)