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