OpenSDN source code
 All Classes Namespaces Files Functions Variables Typedefs Enumerations Enumerator Friends Macros Pages
bgp_as_service_utils.cc
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2017 Juniper Networks, Inc. All rights reserved.
3  */
4 
6 /*
7  * This utils apis will be used by BGP As A Service feature to map
8  * between the original service port allocated for the given BgpAsAService
9  * session and the derived service port for each VMI who is sharing the same
10  * session
11  * GetDerivedBgpaasServicePortRange - From the given config port range and max
12  * session, derive the internal port range
13  * Port range and maximum number of VMIs who is sharing the given BgpAsAService
14  * session will be given by config
15  * EncodeBgpaasServicePort - For the given original service port and
16  * index, derive the service port which will be used by the corresponding
17  * flow which is associated with the corresponding VMI.
18  * DecodeBgpaasServicePort - For the given service port, decode the
19  * original service port and the index for the given port.
20  * From the given range, the dervied port will be defined as follows,
21  * Ex : User Configured Range : 50000 - 50512
22  * Session:1 : config allocates : 50000 for VMI1
23  * Sharing the session-1 for VMI2: allocates 50513
24  * Sharing the session-1 for VMI3: allocates 51026
25  *
26  * Session:2 : config allocates : 50001 for VMI4
27  * Sharing the session-2 for VMI5: allocates 50514
28  * Sharing the session-2 for VMI6: allocates 51027
29  */
30 using namespace std;
32  const uint32_t sport,
33  const uint16_t port_range_start,
34  const uint16_t port_range_end) {
35  size_t index = 0;
36  uint32_t original_sport;
37 
38  if (!port_range_start || !port_range_end || sport < port_range_start) {
39  return std::make_pair(sport, index);
40  }
41 
42  if ((sport >= port_range_start) &&
43  (sport <= port_range_end)) {
44  return std::make_pair(sport, index);
45  }
46 
47  uint16_t port_range = port_range_end - port_range_start + 1;
48 
49  /*
50  * From the given derived port, the original port will be
51  * calculated based on posisiton in the the given port range in
52  * addition to the port range start.
53  * Ex : given port is 51026 and the range is 50000-50512
54  * original port will be 50000 and index will be 2
55  */
56  original_sport = (((sport - port_range_end) % port_range) - 1)
57  + port_range_start;
58  index = ((sport - port_range_end) / port_range) + 1;
59 
60  return std::make_pair(original_sport, index);
61 }
62 
63 uint32_t BGPaaSUtils::EncodeBgpaasServicePort(const uint32_t sport,
64  const size_t index,
65  const uint16_t port_range_start,
66  const uint16_t port_range_end) {
67  if (!index || !port_range_start || !port_range_end) {
68  return sport;
69  }
70 
71  uint16_t port_range = port_range_end - port_range_start + 1;
72  /*
73  * From the given original port and index, the derived port will be
74  * calculated based on selection of derived range which is the multiple
75  * of given port range by the given index in addition to the position of
76  * the original port with respect to port range start.
77  * Ex : given port, index : 50000, 2 and the range is 50000-50512
78  * derived port port will be 51026
79  */
80  return static_cast<uint32_t>(port_range_start + (port_range * index)
81  + (sport - port_range_start));
82 }
std::pair< uint32_t, size_t > BgpAsServicePortIndexPair
static uint32_t EncodeBgpaasServicePort(const uint32_t sport, const size_t index, const uint16_t port_range_start, const uint16_t port_range_end)
static BgpAsServicePortIndexPair DecodeBgpaasServicePort(const uint32_t sport, const uint16_t port_range_start, const uint16_t port_range_end)