NVIDIA DOCA SDK Data Center on a Chip Framework Documentation
psp_gw_svc_impl.h
Go to the documentation of this file.
1 /*
2  * Copyright (c) 2024 NVIDIA CORPORATION AND AFFILIATES. All rights reserved.
3  *
4  * Redistribution and use in source and binary forms, with or without modification, are permitted
5  * provided that the following conditions are met:
6  * * Redistributions of source code must retain the above copyright notice, this list of
7  * conditions and the following disclaimer.
8  * * Redistributions in binary form must reproduce the above copyright notice, this list of
9  * conditions and the following disclaimer in the documentation and/or other materials
10  * provided with the distribution.
11  * * Neither the name of the NVIDIA CORPORATION nor the names of its contributors may be used
12  * to endorse or promote products derived from this software without specific prior written
13  * permission.
14  *
15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS" AND ANY EXPRESS OR
16  * IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND
17  * FITNESS FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL NVIDIA CORPORATION BE LIABLE
18  * FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
19  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS;
20  * OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT,
21  * STRICT LIABILITY, OR TOR (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE
22  * OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
23  *
24  */
25 
26 #ifndef _PSP_GW_SVC_H
27 #define _PSP_GW_SVC_H
28 
29 #include <memory>
30 #include <map>
31 
32 #include <doca_flow.h>
33 
34 #include <psp_gateway.pb.h>
35 #include <psp_gateway.grpc.pb.h>
36 #include "psp_gw_config.h"
37 #include "psp_gw_flows.h"
38 
39 struct psp_pf_dev;
40 struct doca_flow_crypto_psp_spi_key_bulk;
41 
42 using psp_session_and_key_t = std::pair<psp_session_t *, void *>;
43 
59 class PSP_GatewayImpl : public psp_gateway::PSP_Gateway::Service {
60 public:
61  static constexpr uint16_t DEFAULT_HTTP_PORT_NUM = 3000;
62 
69 
79  ::grpc::Status RequestMultipleTunnelParams(::grpc::ServerContext *context,
80  const ::psp_gateway::MultiTunnelRequest *request,
81  ::psp_gateway::MultiTunnelResponse *response) override;
82 
91  ::grpc::Status RequestKeyRotation(::grpc::ServerContext *context,
92  const ::psp_gateway::KeyRotationRequest *request,
93  ::psp_gateway::KeyRotationResponse *response) override;
94 
102  doca_error_t handle_miss_packet(struct rte_mbuf *packet);
103 
111 
120  size_t try_connect(std::vector<psp_gw_peer> &peers);
121 
122 private:
130  static uint32_t psp_version_to_key_length_bits(uint32_t psp_proto_ver)
131  {
132  return (psp_proto_ver == 0 || psp_proto_ver == 2) ? 128 : 256;
133  }
134 
152  doca_error_t request_tunnel_to_host(struct psp_gw_peer *peer,
153  ip_pair *vip_pair,
154  bool supply_reverse_params,
155  bool suppress_failure_msg,
156  bool has_remote);
157 
164  ::psp_gateway::PSP_Gateway::Stub *get_stub(const std::string &peer_ip);
165 
173  psp_gw_peer *lookup_vip_pair(ip_pair &vip_pair);
174 
182  int select_psp_version(const ::psp_gateway::MultiTunnelRequest *request) const;
183 
190  bool is_psp_ver_supported(uint32_t psp_ver) const
191  {
192  return SUPPORTED_PSP_VERSIONS.count(psp_ver) > 0;
193  }
194 
203  void fill_tunnel_params(int psp_ver, uint32_t *key, uint32_t spi, psp_gateway::TunnelParameters *params);
204 
214  doca_error_t generate_keys_spis(uint32_t key_len_bits, uint32_t nr_keys_spis, uint32_t *keys, uint32_t *spis);
215 
223  doca_error_t add_encrypt_entries(std::vector<psp_session_and_key_t> &new_sessions_keys,
224  std::string peer_svc_addr);
234  doca_error_t prepare_session(std::string peer_svc_addr,
235  ip_pair &vip_pair,
236  const psp_gateway::TunnelParameters &params,
237  std::vector<psp_session_and_key_t> &sessions_keys_prepared);
238 
246  void debug_key(const char *msg_prefix, const void *key, size_t key_size_bytes) const;
247 
254  uint32_t next_crypto_id(void);
255 
256  // Application state data:
257 
258  psp_gw_app_config *config{};
259 
260  PSP_GatewayFlows *psp_flows{};
261 
262  psp_pf_dev *pf{};
263 
264  // Used to uniquely populate the request ID in each NewTunnelRequest message.
265  uint64_t next_request_id{};
266 
267  // This flag will cause encryption keys to be logged to stderr, etc.
268  const bool DEBUG_KEYS{false};
269 
270  // map each svc_addr to an RPC object
271  std::map<std::string, std::unique_ptr<::psp_gateway::PSP_Gateway::Stub>> stubs;
272 
273  // map tuple of (src vip, dst vip) to an active session object
274  std::map<session_key, psp_session_t> sessions;
275 
276  // Used to assign a unique shared-resource ID to each encryption flow.
277  uint32_t next_crypto_id_ = 1;
278 };
279 
280 #endif // _PSP_GW_SVC_H
The entity which owns all the doca flow shared resources and flow pipes (but not sessions).
Definition: psp_gw_flows.h:87
Implementation of the PSP_Gateway service.
doca_error_t handle_miss_packet(struct rte_mbuf *packet)
Handles any "miss" packets received by RSS which indicate a new tunnel connection is needed.
::grpc::Status RequestKeyRotation(::grpc::ServerContext *context, const ::psp_gateway::KeyRotationRequest *request, ::psp_gateway::KeyRotationResponse *response) override
Requests that the recipient rotate the PSP master key.
PSP_GatewayImpl(psp_gw_app_config *config, PSP_GatewayFlows *psp_flows)
Constructs the object. This operation cannot fail.
::grpc::Status RequestMultipleTunnelParams(::grpc::ServerContext *context, const ::psp_gateway::MultiTunnelRequest *request, ::psp_gateway::MultiTunnelResponse *response) override
Requests that the recipient allocate multiple SPIs and encryption keys so that the initiator can begi...
doca_error_t show_flow_counts(void)
Displays the counters of all tunnel sessions that have changed since the previous invocation.
static constexpr uint16_t DEFAULT_HTTP_PORT_NUM
size_t try_connect(std::vector< psp_gw_peer > &peers)
Attempt to establish tunnels to each of the passed peers. On success, a given peer is removed from th...
enum doca_error doca_error_t
DOCA API return codes.
const std::set< uint32_t > SUPPORTED_PSP_VERSIONS
Definition: psp_gw_config.h:45
std::pair< psp_session_t *, void * > psp_session_and_key_t
describes the configuration of the PSP networking service on the local host.
Describes a peer which is capable of exchanging traffic flows over a PSP tunnel.
Definition: psp_gw_config.h:98
Maintains the state of the host PF.
Definition: psp_gw_flows.h:47