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