Fix some bugs when testing opensds ansible
[stor4nfv.git] / src / ceph / src / msg / async / dpdk / IP.h
1 // -*- mode:C++; tab-width:8; c-basic-offset:2; indent-tabs-mode:t -*- 
2 /*
3  * This file is open source software, licensed to you under the terms
4  * of the Apache License, Version 2.0 (the "License").  See the NOTICE file
5  * distributed with this work for additional information regarding copyright
6  * ownership.  You may not use this file except in compliance with the License.
7  *
8  * You may obtain a copy of the License at
9  *
10  *   http://www.apache.org/licenses/LICENSE-2.0
11  *
12  * Unless required by applicable law or agreed to in writing,
13  * software distributed under the License is distributed on an
14  * "AS IS" BASIS, WITHOUT WARRANTIES OR CONDITIONS OF ANY
15  * KIND, either express or implied.  See the License for the
16  * specific language governing permissions and limitations
17  * under the License.
18  */
19 /*
20  * Copyright (C) 2014 Cloudius Systems, Ltd.
21  *
22  */
23 /*
24  * Ceph - scalable distributed file system
25  *
26  * Copyright (C) 2015 XSky <haomai@xsky.com>
27  *
28  * Author: Haomai Wang <haomaiwang@gmail.com>
29  *
30  * This is free software; you can redistribute it and/or
31  * modify it under the terms of the GNU Lesser General Public
32  * License version 2.1, as published by the Free Software
33  * Foundation.  See file COPYING.
34  *
35  */
36
37 #ifndef CEPH_MSG_IP_H_
38 #define CEPH_MSG_IP_H_
39
40 #include <arpa/inet.h>
41 #include <unordered_map>
42 #include <cstdint>
43 #include <array>
44 #include <map>
45 #include <list>
46 #include <chrono>
47
48 #include "msg/async/Event.h"
49 #include "common/Throttle.h"
50
51 #include "array_map.h"
52 #include "ARP.h"
53 #include "IPChecksum.h"
54 #include "ip_types.h"
55 #include "const.h"
56 #include "net.h"
57 #include "PacketUtil.h"
58 #include "toeplitz.h"
59
60 class ipv4;
61 template <ip_protocol_num ProtoNum>
62 class ipv4_l4;
63
64 template <typename InetTraits>
65 class tcp;
66
67 struct ipv4_traits {
68   using address_type = ipv4_address;
69   using inet_type = ipv4_l4<ip_protocol_num::tcp>;
70   struct l4packet {
71     ipv4_address to;
72     Packet p;
73     ethernet_address e_dst;
74     ip_protocol_num proto_num;
75   };
76   using packet_provider_type = std::function<Tub<l4packet> ()>;
77   static void tcp_pseudo_header_checksum(checksummer& csum, ipv4_address src, ipv4_address dst, uint16_t len) {
78     csum.sum_many(src.ip, dst.ip, uint8_t(0), uint8_t(ip_protocol_num::tcp), len);
79   }
80   static constexpr uint8_t ip_hdr_len_min = ipv4_hdr_len_min;
81 };
82
83 template <ip_protocol_num ProtoNum>
84 class ipv4_l4 {
85  public:
86   ipv4& _inet;
87  public:
88   ipv4_l4(ipv4& inet) : _inet(inet) {}
89   void register_packet_provider(ipv4_traits::packet_provider_type func);
90   void wait_l2_dst_address(ipv4_address to, Packet p, resolution_cb cb);
91 };
92
93 class ip_protocol {
94  public:
95   virtual ~ip_protocol() {}
96   virtual void received(Packet p, ipv4_address from, ipv4_address to) = 0;
97   virtual bool forward(forward_hash& out_hash_data, Packet& p, size_t off) { return true; }
98 };
99
100 template <typename InetTraits>
101 struct l4connid {
102   using ipaddr = typename InetTraits::address_type;
103   using inet_type = typename InetTraits::inet_type;
104   struct connid_hash;
105
106   ipaddr local_ip;
107   ipaddr foreign_ip;
108   uint16_t local_port;
109   uint16_t foreign_port;
110
111   bool operator==(const l4connid& x) const {
112     return local_ip == x.local_ip
113            && foreign_ip == x.foreign_ip
114            && local_port == x.local_port
115            && foreign_port == x.foreign_port;
116   }
117
118   uint32_t hash(const rss_key_type& rss_key) {
119     forward_hash hash_data;
120     hash_data.push_back(hton(foreign_ip.ip));
121     hash_data.push_back(hton(local_ip.ip));
122     hash_data.push_back(hton(foreign_port));
123     hash_data.push_back(hton(local_port));
124     return toeplitz_hash(rss_key, hash_data);
125   }
126 };
127
128 class ipv4_tcp final : public ip_protocol {
129   ipv4_l4<ip_protocol_num::tcp> _inet_l4;
130   std::unique_ptr<tcp<ipv4_traits>> _tcp;
131  public:
132   ipv4_tcp(ipv4& inet, EventCenter *c);
133   ~ipv4_tcp();
134   virtual void received(Packet p, ipv4_address from, ipv4_address to);
135   virtual bool forward(forward_hash& out_hash_data, Packet& p, size_t off) override;
136   friend class ipv4;
137 };
138
139 struct icmp_hdr {
140   enum class msg_type : uint8_t {
141     echo_reply = 0,
142     echo_request = 8,
143   };
144   msg_type type;
145   uint8_t code;
146   uint16_t csum;
147   uint32_t rest;
148 } __attribute__((packed));
149
150
151 class icmp {
152  public:
153   using ipaddr = ipv4_address;
154   using inet_type = ipv4_l4<ip_protocol_num::icmp>;
155   explicit icmp(CephContext *c, inet_type& inet)
156       : cct(c), _inet(inet), _queue_space(c, "DPDK::icmp::_queue_space", 212992) {
157     _inet.register_packet_provider([this] {
158       Tub<ipv4_traits::l4packet> l4p;
159       if (!_packetq.empty()) {
160         l4p = std::move(_packetq.front());
161         _packetq.pop_front();
162         _queue_space.put(l4p->p.len());
163       }
164       return l4p;
165     });
166   }
167   void received(Packet p, ipaddr from, ipaddr to);
168
169  private:
170   CephContext *cct;
171   // ipv4_l4<ip_protocol_num::icmp>
172   inet_type& _inet;
173   circular_buffer<ipv4_traits::l4packet> _packetq;
174   Throttle _queue_space;
175 };
176
177 class ipv4_icmp final : public ip_protocol {
178   CephContext *cct;
179   ipv4_l4<ip_protocol_num::icmp> _inet_l4;
180   icmp _icmp;
181  public:
182   ipv4_icmp(CephContext *c, ipv4& inet) : cct(c), _inet_l4(inet), _icmp(c, _inet_l4) {}
183   virtual void received(Packet p, ipv4_address from, ipv4_address to) {
184     _icmp.received(std::move(p), from, to);
185   }
186   friend class ipv4;
187 };
188
189 struct ip_hdr;
190
191 struct ip_packet_filter {
192   virtual ~ip_packet_filter() {};
193   virtual void handle(Packet& p, ip_hdr* iph, ethernet_address from, bool & handled) = 0;
194 };
195
196 struct ipv4_frag_id {
197   struct hash;
198   ipv4_address src_ip;
199   ipv4_address dst_ip;
200   uint16_t identification;
201   uint8_t protocol;
202   bool operator==(const ipv4_frag_id& x) const {
203     return src_ip == x.src_ip &&
204            dst_ip == x.dst_ip &&
205            identification == x.identification &&
206            protocol == x.protocol;
207   }
208 };
209
210 struct ipv4_frag_id::hash : private std::hash<ipv4_address>,
211                             private std::hash<uint16_t>, private std::hash<uint8_t> {
212   size_t operator()(const ipv4_frag_id& id) const noexcept {
213     using h1 = std::hash<ipv4_address>;
214     using h2 = std::hash<uint16_t>;
215     using h3 = std::hash<uint8_t>;
216     return h1::operator()(id.src_ip) ^
217            h1::operator()(id.dst_ip) ^
218            h2::operator()(id.identification) ^
219            h3::operator()(id.protocol);
220   }
221 };
222
223 struct ipv4_tag {};
224 using ipv4_packet_merger = packet_merger<uint32_t, ipv4_tag>;
225
226 class interface;
227
228 class ipv4 {
229  public:
230   using address_type = ipv4_address;
231   using proto_type = uint16_t;
232   static address_type broadcast_address() { return ipv4_address(0xffffffff); }
233   static proto_type arp_protocol_type() { return proto_type(eth_protocol_num::ipv4); }
234   CephContext *cct;
235   EventCenter *center;
236
237  private:
238   interface* _netif;
239   std::vector<ipv4_traits::packet_provider_type> _pkt_providers;
240   Tub<uint64_t> frag_timefd;
241   EventCallbackRef frag_handler;
242   arp _global_arp;
243   arp_for<ipv4> _arp;
244   ipv4_address _host_address;
245   ipv4_address _gw_address;
246   ipv4_address _netmask;
247   l3_protocol _l3;
248   subscription<Packet, ethernet_address> _rx_packets;
249   ipv4_tcp _tcp;
250   ipv4_icmp _icmp;
251   array_map<ip_protocol*, 256> _l4;
252   ip_packet_filter *_packet_filter;
253   struct frag {
254     Packet header;
255     ipv4_packet_merger data;
256     utime_t rx_time;
257     uint32_t mem_size = 0;
258     // fragment with MF == 0 inidates it is the last fragment
259     bool last_frag_received = false;
260
261     Packet get_assembled_packet(ethernet_address from, ethernet_address to);
262     int32_t merge(ip_hdr &h, uint16_t offset, Packet p);
263     bool is_complete();
264   };
265   std::unordered_map<ipv4_frag_id, frag, ipv4_frag_id::hash> _frags;
266   std::list<ipv4_frag_id> _frags_age;
267   static utime_t _frag_timeout;
268   static constexpr uint32_t _frag_low_thresh{3 * 1024 * 1024};
269   static constexpr uint32_t _frag_high_thresh{4 * 1024 * 1024};
270   uint32_t _frag_mem = 0;
271   circular_buffer<l3_protocol::l3packet> _packetq;
272   unsigned _pkt_provider_idx = 0;
273   PerfCounters *perf_logger;
274
275  private:
276   int handle_received_packet(Packet p, ethernet_address from);
277   bool forward(forward_hash& out_hash_data, Packet& p, size_t off);
278   Tub<l3_protocol::l3packet> get_packet();
279   bool in_my_netmask(ipv4_address a) const {
280     return !((a.ip ^ _host_address.ip) & _netmask.ip);
281   }
282   void frag_limit_mem();
283   void frag_drop(ipv4_frag_id frag_id, uint32_t dropped_size) {
284     _frags.erase(frag_id);
285     _frag_mem -= dropped_size;
286   }
287   void frag_arm(utime_t now) {
288     auto tp = now + _frag_timeout;
289     frag_timefd.construct(center->create_time_event(tp.to_nsec() / 1000, frag_handler));
290   }
291   void frag_arm() {
292     auto now = ceph_clock_now();
293     frag_timefd.construct(center->create_time_event(now.to_nsec() / 1000, frag_handler));
294   }
295
296  public:
297   void frag_timeout();
298
299  public:
300   explicit ipv4(CephContext *c, EventCenter *cen, interface* netif);
301   ~ipv4() {
302     delete frag_handler;
303   }
304   void set_host_address(ipv4_address ip) {
305     _host_address = ip;
306     _arp.set_self_addr(ip);
307   }
308   ipv4_address host_address() {
309     return _host_address;
310   }
311   void set_gw_address(ipv4_address ip) {
312     _gw_address = ip;
313   }
314   ipv4_address gw_address() const {
315     return _gw_address;
316   }
317   void set_netmask_address(ipv4_address ip) {
318     _netmask = ip;
319   }
320   ipv4_address netmask_address() const {
321     return _netmask;
322   }
323   interface *netif() const {
324     return _netif;
325   }
326   // TODO or something. Should perhaps truly be a list
327   // of filters. With ordering. And blackjack. Etc.
328   // But for now, a simple single raw pointer suffices
329   void set_packet_filter(ip_packet_filter *f) {
330     _packet_filter = f;
331   }
332   ip_packet_filter * packet_filter() const {
333     return _packet_filter;
334   }
335   void send(ipv4_address to, ip_protocol_num proto_num, Packet p, ethernet_address e_dst);
336   tcp<ipv4_traits>& get_tcp() { return *_tcp._tcp; }
337   void register_l4(proto_type id, ip_protocol* handler);
338   const hw_features& get_hw_features() const;
339   static bool needs_frag(Packet& p, ip_protocol_num proto_num, hw_features hw_features) {
340     if (p.len() + ipv4_hdr_len_min <= hw_features.mtu)
341       return false;
342
343     if ((proto_num == ip_protocol_num::tcp && hw_features.tx_tso))
344       return false;
345
346     return true;
347   }
348   void learn(ethernet_address l2, ipv4_address l3) {
349     _arp.learn(l2, l3);
350   }
351   void register_packet_provider(ipv4_traits::packet_provider_type&& func) {
352     _pkt_providers.push_back(std::move(func));
353   }
354   void wait_l2_dst_address(ipv4_address to, Packet p, resolution_cb cb);
355 };
356
357 template <ip_protocol_num ProtoNum>
358 inline void ipv4_l4<ProtoNum>::register_packet_provider(
359     ipv4_traits::packet_provider_type func) {
360   _inet.register_packet_provider([func] {
361     auto l4p = func();
362     if (l4p) {
363       (*l4p).proto_num = ProtoNum;
364     }
365     return l4p;
366   });
367 }
368
369 template <ip_protocol_num ProtoNum>
370 inline void ipv4_l4<ProtoNum>::wait_l2_dst_address(ipv4_address to, Packet p, resolution_cb cb) {
371   _inet.wait_l2_dst_address(to, std::move(p), std::move(cb));
372 }
373
374 struct ip_hdr {
375   uint8_t ihl : 4;
376   uint8_t ver : 4;
377   uint8_t dscp : 6;
378   uint8_t ecn : 2;
379   uint16_t len;
380   uint16_t id;
381   uint16_t frag;
382   enum class frag_bits : uint8_t { mf = 13, df = 14, reserved = 15, offset_shift = 3 };
383   uint8_t ttl;
384   uint8_t ip_proto;
385   uint16_t csum;
386   ipv4_address src_ip;
387   ipv4_address dst_ip;
388   uint8_t options[0];
389   ip_hdr hton() {
390     ip_hdr hdr = *this;
391     hdr.len = ::hton(len);
392     hdr.id = ::hton(id);
393     hdr.frag = ::hton(frag);
394     hdr.csum = ::hton(csum);
395     hdr.src_ip.ip = ::hton(src_ip.ip);
396     hdr.dst_ip.ip = ::hton(dst_ip.ip);
397     return hdr;
398   }
399   ip_hdr ntoh() {
400     ip_hdr hdr = *this;
401     hdr.len = ::ntoh(len);
402     hdr.id = ::ntoh(id);
403     hdr.frag = ::ntoh(frag);
404     hdr.csum = ::ntoh(csum);
405     hdr.src_ip = src_ip.ntoh();
406     hdr.dst_ip = dst_ip.ntoh();
407     return hdr;
408   }
409
410   bool mf() { return frag & (1 << uint8_t(frag_bits::mf)); }
411   bool df() { return frag & (1 << uint8_t(frag_bits::df)); }
412   uint16_t offset() { return frag << uint8_t(frag_bits::offset_shift); }
413 } __attribute__((packed));
414
415 template <typename InetTraits>
416 struct l4connid<InetTraits>::connid_hash : private std::hash<ipaddr>, private std::hash<uint16_t> {
417   size_t operator()(const l4connid<InetTraits>& id) const noexcept {
418     using h1 = std::hash<ipaddr>;
419     using h2 = std::hash<uint16_t>;
420     return h1::operator()(id.local_ip)
421            ^ h1::operator()(id.foreign_ip)
422            ^ h2::operator()(id.local_port)
423            ^ h2::operator()(id.foreign_port);
424   }
425 };
426
427 #endif /* CEPH_MSG_IP_H */