Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 1 | /* |
| 2 | * Copyright 2014 The Android Open Source Project |
| 3 | * |
| 4 | * Licensed under the Apache License, Version 2.0 (the "License"); |
| 5 | * you may not use this file except in compliance with the License. |
| 6 | * You may obtain a copy of the License at |
| 7 | * |
| 8 | * http://www.apache.org/licenses/LICENSE-2.0 |
| 9 | * |
| 10 | * Unless required by applicable law or agreed to in writing, software |
| 11 | * distributed under the License is distributed on an "AS IS" BASIS, |
| 12 | * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. |
| 13 | * See the License for the specific language governing permissions and |
| 14 | * limitations under the License. |
| 15 | * |
| 16 | * ring.c - packet ring buffer functions |
| 17 | */ |
| 18 | #ifndef __RING_H__ |
| 19 | #define __RING_H__ |
| 20 | |
| 21 | #include <linux/if.h> |
| 22 | #include <linux/if_packet.h> |
| 23 | |
| 24 | #include "clatd.h" |
| 25 | |
| 26 | struct tun_data; |
| 27 | |
| 28 | // Frame size. Must be a multiple of TPACKET_ALIGNMENT (=16) |
| 29 | // Why the 16? http://lxr.free-electrons.com/source/net/packet/af_packet.c?v=3.4#L1764 |
Subash Abhinov Kasiviswanathan | 5c5b534 | 2015-10-26 18:45:04 -0600 | [diff] [blame] | 30 | #define TP_FRAME_SIZE (TPACKET_ALIGN(MAXMRU) + TPACKET_ALIGN(TPACKET2_HDRLEN) + 16) |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 31 | |
| 32 | // Block size. Must be a multiple of the page size, and a power of two for efficient memory use. |
Subash Abhinov Kasiviswanathan | 5c5b534 | 2015-10-26 18:45:04 -0600 | [diff] [blame] | 33 | #define TP_BLOCK_SIZE 2686976 |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 34 | |
| 35 | // In order to save memory, our frames are not an exact divider of the block size. Therefore, the |
| 36 | // mmaped region will have gaps corresponding to the empty space at the end of each block. |
| 37 | #define TP_FRAMES (TP_BLOCK_SIZE / TP_FRAME_SIZE) |
| 38 | #define TP_FRAME_GAP (TP_BLOCK_SIZE % TP_FRAME_SIZE) |
| 39 | |
| 40 | // TODO: Make this configurable. This requires some refactoring because the packet socket is |
| 41 | // opened before we drop privileges, but the configuration file is read after. A value of 16 |
| 42 | // results in 656 frames (1048576 bytes). |
| 43 | #define TP_NUM_BLOCKS 16 |
| 44 | |
Subash Abhinov Kasiviswanathan | 5c5b534 | 2015-10-26 18:45:04 -0600 | [diff] [blame] | 45 | #define TP_CSUM_NONE (0) |
| 46 | #define TP_CSUM_UNNECESSARY (1) |
| 47 | |
Lorenzo Colitti | 9353be2 | 2014-12-03 15:18:29 +0900 | [diff] [blame] | 48 | struct packet_ring { |
| 49 | uint8_t *base; |
| 50 | struct tpacket2_hdr *next; |
| 51 | int slot, numslots; |
| 52 | int block, numblocks; |
| 53 | }; |
| 54 | |
| 55 | int ring_create(struct tun_data *tunnel); |
| 56 | void ring_read(struct packet_ring *ring, int write_fd, int to_ipv6); |
| 57 | |
| 58 | #endif |