OLD | NEW |
1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. | 1 // Copyright (c) 2012 The Chromium Authors. All rights reserved. |
2 // Use of this source code is governed by a BSD-style license that can be | 2 // Use of this source code is governed by a BSD-style license that can be |
3 // found in the LICENSE file. | 3 // found in the LICENSE file. |
4 | 4 |
5 #include "net/tools/quic/quic_server.h" | 5 #include "net/tools/quic/quic_server.h" |
6 | 6 |
7 #include <errno.h> | 7 #include <errno.h> |
8 #include <features.h> | 8 #include <features.h> |
9 #include <netinet/in.h> | 9 #include <netinet/in.h> |
10 #include <string.h> | 10 #include <string.h> |
11 #include <sys/epoll.h> | 11 #include <sys/epoll.h> |
12 #include <sys/socket.h> | 12 #include <sys/socket.h> |
13 | 13 |
14 #include "net/base/ip_endpoint.h" | 14 #include "net/base/ip_endpoint.h" |
15 #include "net/quic/crypto/crypto_handshake.h" | 15 #include "net/quic/crypto/crypto_handshake.h" |
16 #include "net/quic/crypto/quic_random.h" | 16 #include "net/quic/crypto/quic_random.h" |
17 #include "net/quic/quic_clock.h" | 17 #include "net/quic/quic_clock.h" |
18 #include "net/quic/quic_crypto_stream.h" | 18 #include "net/quic/quic_crypto_stream.h" |
19 #include "net/quic/quic_data_reader.h" | 19 #include "net/quic/quic_data_reader.h" |
20 #include "net/quic/quic_protocol.h" | 20 #include "net/quic/quic_protocol.h" |
21 #include "net/tools/quic/quic_dispatcher.h" | 21 #include "net/tools/quic/quic_dispatcher.h" |
22 #include "net/tools/quic/quic_epoll_clock.h" | 22 #include "net/tools/quic/quic_epoll_clock.h" |
23 #include "net/tools/quic/quic_epoll_connection_helper.h" | 23 #include "net/tools/quic/quic_epoll_connection_helper.h" |
24 #include "net/tools/quic/quic_in_memory_cache.h" | 24 #include "net/tools/quic/quic_in_memory_cache.h" |
25 #include "net/tools/quic/quic_packet_reader.h" | 25 #include "net/tools/quic/quic_packet_reader.h" |
26 #include "net/tools/quic/quic_socket_utils.h" | 26 #include "net/tools/quic/quic_socket_utils.h" |
27 | 27 |
28 // TODO(rtenneti): Add support for MMSG_MORE. | 28 // TODO(rtenneti): Add support for MMSG_MORE. |
29 #define MMSG_MORE 0 | 29 #define MMSG_MORE 0 |
30 // If true, QuicListener uses the QuicPacketReader to read packets instead of | |
31 // QuicServer. | |
32 // TODO(rtenneti): Enable this flag after MMSG_MORE is set to 1. | |
33 #define FLAGS_quic_use_optimized_packet_reader false | |
34 | 30 |
35 #ifndef SO_RXQ_OVFL | 31 #ifndef SO_RXQ_OVFL |
36 #define SO_RXQ_OVFL 40 | 32 #define SO_RXQ_OVFL 40 |
37 #endif | 33 #endif |
38 | 34 |
39 namespace net { | 35 namespace net { |
40 namespace tools { | 36 namespace tools { |
41 namespace { | 37 namespace { |
42 | 38 |
43 // Specifies the directory used during QuicInMemoryCache | 39 // Specifies the directory used during QuicInMemoryCache |
(...skipping 170 matching lines...) Expand 10 before | Expand all | Expand 10 after Loading... |
214 | 210 |
215 void QuicServer::OnEvent(int fd, EpollEvent* event) { | 211 void QuicServer::OnEvent(int fd, EpollEvent* event) { |
216 DCHECK_EQ(fd, fd_); | 212 DCHECK_EQ(fd, fd_); |
217 event->out_ready_mask = 0; | 213 event->out_ready_mask = 0; |
218 | 214 |
219 if (event->in_events & EPOLLIN) { | 215 if (event->in_events & EPOLLIN) { |
220 DVLOG(1) << "EPOLLIN"; | 216 DVLOG(1) << "EPOLLIN"; |
221 bool read = true; | 217 bool read = true; |
222 while (read) { | 218 while (read) { |
223 if (use_recvmmsg_) { | 219 if (use_recvmmsg_) { |
224 if (FLAGS_quic_use_optimized_packet_reader) { | 220 read = packet_reader_->ReadAndDispatchPackets( |
225 read = packet_reader_->ReadAndDispatchPackets( | 221 fd_, port_, dispatcher_.get(), |
226 fd_, port_, dispatcher_.get(), | 222 overflow_supported_ ? &packets_dropped_ : nullptr); |
227 overflow_supported_ ? &packets_dropped_ : nullptr); | |
228 } else { | |
229 // TODO(rtenneti): Add support for ReadAndDispatchPackets. | |
230 #if 0 | |
231 read = ReadAndDispatchPackets( | |
232 fd_, port_, dispatcher_.get(), | |
233 overflow_supported_ ? &packets_dropped_ : nullptr); | |
234 #else | |
235 read = ReadAndDispatchSinglePacket( | |
236 fd_, port_, dispatcher_.get(), | |
237 overflow_supported_ ? &packets_dropped_ : nullptr); | |
238 #endif | |
239 } | |
240 } else { | 223 } else { |
241 if (FLAGS_quic_use_optimized_packet_reader) { | 224 read = QuicPacketReader::ReadAndDispatchSinglePacket( |
242 read = QuicPacketReader::ReadAndDispatchSinglePacket( | 225 fd_, port_, dispatcher_.get(), |
243 fd_, port_, dispatcher_.get(), | 226 overflow_supported_ ? &packets_dropped_ : nullptr); |
244 overflow_supported_ ? &packets_dropped_ : nullptr); | |
245 } else { | |
246 read = ReadAndDispatchSinglePacket( | |
247 fd_, port_, dispatcher_.get(), | |
248 overflow_supported_ ? &packets_dropped_ : nullptr); | |
249 } | |
250 } | 227 } |
251 } | 228 } |
252 } | 229 } |
253 if (event->in_events & EPOLLOUT) { | 230 if (event->in_events & EPOLLOUT) { |
254 dispatcher_->OnCanWrite(); | 231 dispatcher_->OnCanWrite(); |
255 if (dispatcher_->HasPendingWrites()) { | 232 if (dispatcher_->HasPendingWrites()) { |
256 event->out_ready_mask |= EPOLLOUT; | 233 event->out_ready_mask |= EPOLLOUT; |
257 } | 234 } |
258 } | 235 } |
259 if (event->in_events & EPOLLERR) { | 236 if (event->in_events & EPOLLERR) { |
260 } | 237 } |
261 } | 238 } |
262 | 239 |
263 /* static */ | |
264 bool QuicServer::ReadAndDispatchSinglePacket(int fd, | |
265 int port, | |
266 ProcessPacketInterface* processor, | |
267 QuicPacketCount* packets_dropped) { | |
268 // Allocate some extra space so we can send an error if the client goes over | |
269 // the limit. | |
270 char buf[2 * kMaxPacketSize]; | |
271 | |
272 IPEndPoint client_address; | |
273 IPAddressNumber server_ip; | |
274 int bytes_read = | |
275 QuicSocketUtils::ReadPacket(fd, buf, arraysize(buf), | |
276 packets_dropped, | |
277 &server_ip, &client_address); | |
278 | |
279 if (bytes_read < 0) { | |
280 return false; // We failed to read. | |
281 } | |
282 | |
283 QuicEncryptedPacket packet(buf, bytes_read, false); | |
284 | |
285 IPEndPoint server_address(server_ip, port); | |
286 processor->ProcessPacket(server_address, client_address, packet); | |
287 | |
288 // The socket read was successful, so return true even if packet dispatch | |
289 // failed. | |
290 return true; | |
291 } | |
292 | |
293 } // namespace tools | 240 } // namespace tools |
294 } // namespace net | 241 } // namespace net |
OLD | NEW |