├── Kconfig ├── LICENSE ├── Makefile ├── README.md ├── dkms.conf ├── toa.c └── toa.h /Kconfig: -------------------------------------------------------------------------------- 1 | config TOA 2 | tristate "The private TCP option for support Taobao LVS full-NAT feature" 3 | default m 4 | ---help--- 5 | This option saves the original IP address and source port of a TCP segment 6 | after LVS performed NAT on it. So far, this module only supports IPv4 and 7 | IPv6 mapped IPv4. 8 | 9 | Say m if unsure. 10 | -------------------------------------------------------------------------------- /LICENSE: -------------------------------------------------------------------------------- 1 | GNU GENERAL PUBLIC LICENSE 2 | Version 2, June 1991 3 | 4 | Copyright (C) 1989, 1991 Free Software Foundation, Inc., 5 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA 6 | Everyone is permitted to copy and distribute verbatim copies 7 | of this license document, but changing it is not allowed. 8 | 9 | Preamble 10 | 11 | The licenses for most software are designed to take away your 12 | freedom to share and change it. By contrast, the GNU General Public 13 | License is intended to guarantee your freedom to share and change free 14 | software--to make sure the software is free for all its users. This 15 | General Public License applies to most of the Free Software 16 | Foundation's software and to any other program whose authors commit to 17 | using it. (Some other Free Software Foundation software is covered by 18 | the GNU Lesser General Public License instead.) You can apply it to 19 | your programs, too. 20 | 21 | When we speak of free software, we are referring to freedom, not 22 | price. Our General Public Licenses are designed to make sure that you 23 | have the freedom to distribute copies of free software (and charge for 24 | this service if you wish), that you receive source code or can get it 25 | if you want it, that you can change the software or use pieces of it 26 | in new free programs; and that you know you can do these things. 27 | 28 | To protect your rights, we need to make restrictions that forbid 29 | anyone to deny you these rights or to ask you to surrender the rights. 30 | These restrictions translate to certain responsibilities for you if you 31 | distribute copies of the software, or if you modify it. 32 | 33 | For example, if you distribute copies of such a program, whether 34 | gratis or for a fee, you must give the recipients all the rights that 35 | you have. You must make sure that they, too, receive or can get the 36 | source code. And you must show them these terms so they know their 37 | rights. 38 | 39 | We protect your rights with two steps: (1) copyright the software, and 40 | (2) offer you this license which gives you legal permission to copy, 41 | distribute and/or modify the software. 42 | 43 | Also, for each author's protection and ours, we want to make certain 44 | that everyone understands that there is no warranty for this free 45 | software. If the software is modified by someone else and passed on, we 46 | want its recipients to know that what they have is not the original, so 47 | that any problems introduced by others will not reflect on the original 48 | authors' reputations. 49 | 50 | Finally, any free program is threatened constantly by software 51 | patents. We wish to avoid the danger that redistributors of a free 52 | program will individually obtain patent licenses, in effect making the 53 | program proprietary. To prevent this, we have made it clear that any 54 | patent must be licensed for everyone's free use or not licensed at all. 55 | 56 | The precise terms and conditions for copying, distribution and 57 | modification follow. 58 | 59 | GNU GENERAL PUBLIC LICENSE 60 | TERMS AND CONDITIONS FOR COPYING, DISTRIBUTION AND MODIFICATION 61 | 62 | 0. This License applies to any program or other work which contains 63 | a notice placed by the copyright holder saying it may be distributed 64 | under the terms of this General Public License. The "Program", below, 65 | refers to any such program or work, and a "work based on the Program" 66 | means either the Program or any derivative work under copyright law: 67 | that is to say, a work containing the Program or a portion of it, 68 | either verbatim or with modifications and/or translated into another 69 | language. (Hereinafter, translation is included without limitation in 70 | the term "modification".) Each licensee is addressed as "you". 71 | 72 | Activities other than copying, distribution and modification are not 73 | covered by this License; they are outside its scope. The act of 74 | running the Program is not restricted, and the output from the Program 75 | is covered only if its contents constitute a work based on the 76 | Program (independent of having been made by running the Program). 77 | Whether that is true depends on what the Program does. 78 | 79 | 1. You may copy and distribute verbatim copies of the Program's 80 | source code as you receive it, in any medium, provided that you 81 | conspicuously and appropriately publish on each copy an appropriate 82 | copyright notice and disclaimer of warranty; keep intact all the 83 | notices that refer to this License and to the absence of any warranty; 84 | and give any other recipients of the Program a copy of this License 85 | along with the Program. 86 | 87 | You may charge a fee for the physical act of transferring a copy, and 88 | you may at your option offer warranty protection in exchange for a fee. 89 | 90 | 2. You may modify your copy or copies of the Program or any portion 91 | of it, thus forming a work based on the Program, and copy and 92 | distribute such modifications or work under the terms of Section 1 93 | above, provided that you also meet all of these conditions: 94 | 95 | a) You must cause the modified files to carry prominent notices 96 | stating that you changed the files and the date of any change. 97 | 98 | b) You must cause any work that you distribute or publish, that in 99 | whole or in part contains or is derived from the Program or any 100 | part thereof, to be licensed as a whole at no charge to all third 101 | parties under the terms of this License. 102 | 103 | c) If the modified program normally reads commands interactively 104 | when run, you must cause it, when started running for such 105 | interactive use in the most ordinary way, to print or display an 106 | announcement including an appropriate copyright notice and a 107 | notice that there is no warranty (or else, saying that you provide 108 | a warranty) and that users may redistribute the program under 109 | these conditions, and telling the user how to view a copy of this 110 | License. (Exception: if the Program itself is interactive but 111 | does not normally print such an announcement, your work based on 112 | the Program is not required to print an announcement.) 113 | 114 | These requirements apply to the modified work as a whole. If 115 | identifiable sections of that work are not derived from the Program, 116 | and can be reasonably considered independent and separate works in 117 | themselves, then this License, and its terms, do not apply to those 118 | sections when you distribute them as separate works. But when you 119 | distribute the same sections as part of a whole which is a work based 120 | on the Program, the distribution of the whole must be on the terms of 121 | this License, whose permissions for other licensees extend to the 122 | entire whole, and thus to each and every part regardless of who wrote it. 123 | 124 | Thus, it is not the intent of this section to claim rights or contest 125 | your rights to work written entirely by you; rather, the intent is to 126 | exercise the right to control the distribution of derivative or 127 | collective works based on the Program. 128 | 129 | In addition, mere aggregation of another work not based on the Program 130 | with the Program (or with a work based on the Program) on a volume of 131 | a storage or distribution medium does not bring the other work under 132 | the scope of this License. 133 | 134 | 3. You may copy and distribute the Program (or a work based on it, 135 | under Section 2) in object code or executable form under the terms of 136 | Sections 1 and 2 above provided that you also do one of the following: 137 | 138 | a) Accompany it with the complete corresponding machine-readable 139 | source code, which must be distributed under the terms of Sections 140 | 1 and 2 above on a medium customarily used for software interchange; or, 141 | 142 | b) Accompany it with a written offer, valid for at least three 143 | years, to give any third party, for a charge no more than your 144 | cost of physically performing source distribution, a complete 145 | machine-readable copy of the corresponding source code, to be 146 | distributed under the terms of Sections 1 and 2 above on a medium 147 | customarily used for software interchange; or, 148 | 149 | c) Accompany it with the information you received as to the offer 150 | to distribute corresponding source code. (This alternative is 151 | allowed only for noncommercial distribution and only if you 152 | received the program in object code or executable form with such 153 | an offer, in accord with Subsection b above.) 154 | 155 | The source code for a work means the preferred form of the work for 156 | making modifications to it. For an executable work, complete source 157 | code means all the source code for all modules it contains, plus any 158 | associated interface definition files, plus the scripts used to 159 | control compilation and installation of the executable. However, as a 160 | special exception, the source code distributed need not include 161 | anything that is normally distributed (in either source or binary 162 | form) with the major components (compiler, kernel, and so on) of the 163 | operating system on which the executable runs, unless that component 164 | itself accompanies the executable. 165 | 166 | If distribution of executable or object code is made by offering 167 | access to copy from a designated place, then offering equivalent 168 | access to copy the source code from the same place counts as 169 | distribution of the source code, even though third parties are not 170 | compelled to copy the source along with the object code. 171 | 172 | 4. You may not copy, modify, sublicense, or distribute the Program 173 | except as expressly provided under this License. Any attempt 174 | otherwise to copy, modify, sublicense or distribute the Program is 175 | void, and will automatically terminate your rights under this License. 176 | However, parties who have received copies, or rights, from you under 177 | this License will not have their licenses terminated so long as such 178 | parties remain in full compliance. 179 | 180 | 5. You are not required to accept this License, since you have not 181 | signed it. However, nothing else grants you permission to modify or 182 | distribute the Program or its derivative works. These actions are 183 | prohibited by law if you do not accept this License. Therefore, by 184 | modifying or distributing the Program (or any work based on the 185 | Program), you indicate your acceptance of this License to do so, and 186 | all its terms and conditions for copying, distributing or modifying 187 | the Program or works based on it. 188 | 189 | 6. Each time you redistribute the Program (or any work based on the 190 | Program), the recipient automatically receives a license from the 191 | original licensor to copy, distribute or modify the Program subject to 192 | these terms and conditions. You may not impose any further 193 | restrictions on the recipients' exercise of the rights granted herein. 194 | You are not responsible for enforcing compliance by third parties to 195 | this License. 196 | 197 | 7. If, as a consequence of a court judgment or allegation of patent 198 | infringement or for any other reason (not limited to patent issues), 199 | conditions are imposed on you (whether by court order, agreement or 200 | otherwise) that contradict the conditions of this License, they do not 201 | excuse you from the conditions of this License. If you cannot 202 | distribute so as to satisfy simultaneously your obligations under this 203 | License and any other pertinent obligations, then as a consequence you 204 | may not distribute the Program at all. For example, if a patent 205 | license would not permit royalty-free redistribution of the Program by 206 | all those who receive copies directly or indirectly through you, then 207 | the only way you could satisfy both it and this License would be to 208 | refrain entirely from distribution of the Program. 209 | 210 | If any portion of this section is held invalid or unenforceable under 211 | any particular circumstance, the balance of the section is intended to 212 | apply and the section as a whole is intended to apply in other 213 | circumstances. 214 | 215 | It is not the purpose of this section to induce you to infringe any 216 | patents or other property right claims or to contest validity of any 217 | such claims; this section has the sole purpose of protecting the 218 | integrity of the free software distribution system, which is 219 | implemented by public license practices. Many people have made 220 | generous contributions to the wide range of software distributed 221 | through that system in reliance on consistent application of that 222 | system; it is up to the author/donor to decide if he or she is willing 223 | to distribute software through any other system and a licensee cannot 224 | impose that choice. 225 | 226 | This section is intended to make thoroughly clear what is believed to 227 | be a consequence of the rest of this License. 228 | 229 | 8. If the distribution and/or use of the Program is restricted in 230 | certain countries either by patents or by copyrighted interfaces, the 231 | original copyright holder who places the Program under this License 232 | may add an explicit geographical distribution limitation excluding 233 | those countries, so that distribution is permitted only in or among 234 | countries not thus excluded. In such case, this License incorporates 235 | the limitation as if written in the body of this License. 236 | 237 | 9. The Free Software Foundation may publish revised and/or new versions 238 | of the General Public License from time to time. Such new versions will 239 | be similar in spirit to the present version, but may differ in detail to 240 | address new problems or concerns. 241 | 242 | Each version is given a distinguishing version number. If the Program 243 | specifies a version number of this License which applies to it and "any 244 | later version", you have the option of following the terms and conditions 245 | either of that version or of any later version published by the Free 246 | Software Foundation. If the Program does not specify a version number of 247 | this License, you may choose any version ever published by the Free Software 248 | Foundation. 249 | 250 | 10. If you wish to incorporate parts of the Program into other free 251 | programs whose distribution conditions are different, write to the author 252 | to ask for permission. For software which is copyrighted by the Free 253 | Software Foundation, write to the Free Software Foundation; we sometimes 254 | make exceptions for this. Our decision will be guided by the two goals 255 | of preserving the free status of all derivatives of our free software and 256 | of promoting the sharing and reuse of software generally. 257 | 258 | NO WARRANTY 259 | 260 | 11. BECAUSE THE PROGRAM IS LICENSED FREE OF CHARGE, THERE IS NO WARRANTY 261 | FOR THE PROGRAM, TO THE EXTENT PERMITTED BY APPLICABLE LAW. EXCEPT WHEN 262 | OTHERWISE STATED IN WRITING THE COPYRIGHT HOLDERS AND/OR OTHER PARTIES 263 | PROVIDE THE PROGRAM "AS IS" WITHOUT WARRANTY OF ANY KIND, EITHER EXPRESSED 264 | OR IMPLIED, INCLUDING, BUT NOT LIMITED TO, THE IMPLIED WARRANTIES OF 265 | MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE. THE ENTIRE RISK AS 266 | TO THE QUALITY AND PERFORMANCE OF THE PROGRAM IS WITH YOU. SHOULD THE 267 | PROGRAM PROVE DEFECTIVE, YOU ASSUME THE COST OF ALL NECESSARY SERVICING, 268 | REPAIR OR CORRECTION. 269 | 270 | 12. IN NO EVENT UNLESS REQUIRED BY APPLICABLE LAW OR AGREED TO IN WRITING 271 | WILL ANY COPYRIGHT HOLDER, OR ANY OTHER PARTY WHO MAY MODIFY AND/OR 272 | REDISTRIBUTE THE PROGRAM AS PERMITTED ABOVE, BE LIABLE TO YOU FOR DAMAGES, 273 | INCLUDING ANY GENERAL, SPECIAL, INCIDENTAL OR CONSEQUENTIAL DAMAGES ARISING 274 | OUT OF THE USE OR INABILITY TO USE THE PROGRAM (INCLUDING BUT NOT LIMITED 275 | TO LOSS OF DATA OR DATA BEING RENDERED INACCURATE OR LOSSES SUSTAINED BY 276 | YOU OR THIRD PARTIES OR A FAILURE OF THE PROGRAM TO OPERATE WITH ANY OTHER 277 | PROGRAMS), EVEN IF SUCH HOLDER OR OTHER PARTY HAS BEEN ADVISED OF THE 278 | POSSIBILITY OF SUCH DAMAGES. 279 | 280 | END OF TERMS AND CONDITIONS 281 | 282 | How to Apply These Terms to Your New Programs 283 | 284 | If you develop a new program, and you want it to be of the greatest 285 | possible use to the public, the best way to achieve this is to make it 286 | free software which everyone can redistribute and change under these terms. 287 | 288 | To do so, attach the following notices to the program. It is safest 289 | to attach them to the start of each source file to most effectively 290 | convey the exclusion of warranty; and each file should have at least 291 | the "copyright" line and a pointer to where the full notice is found. 292 | 293 | 294 | Copyright (C) 295 | 296 | This program is free software; you can redistribute it and/or modify 297 | it under the terms of the GNU General Public License as published by 298 | the Free Software Foundation; either version 2 of the License, or 299 | (at your option) any later version. 300 | 301 | This program is distributed in the hope that it will be useful, 302 | but WITHOUT ANY WARRANTY; without even the implied warranty of 303 | MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 304 | GNU General Public License for more details. 305 | 306 | You should have received a copy of the GNU General Public License along 307 | with this program; if not, write to the Free Software Foundation, Inc., 308 | 51 Franklin Street, Fifth Floor, Boston, MA 02110-1301 USA. 309 | 310 | Also add information on how to contact you by electronic and paper mail. 311 | 312 | If the program is interactive, make it output a short notice like this 313 | when it starts in an interactive mode: 314 | 315 | Gnomovision version 69, Copyright (C) year name of author 316 | Gnomovision comes with ABSOLUTELY NO WARRANTY; for details type `show w'. 317 | This is free software, and you are welcome to redistribute it 318 | under certain conditions; type `show c' for details. 319 | 320 | The hypothetical commands `show w' and `show c' should show the appropriate 321 | parts of the General Public License. Of course, the commands you use may 322 | be called something other than `show w' and `show c'; they could even be 323 | mouse-clicks or menu items--whatever suits your program. 324 | 325 | You should also get your employer (if you work as a programmer) or your 326 | school, if any, to sign a "copyright disclaimer" for the program, if 327 | necessary. Here is a sample; alter the names: 328 | 329 | Yoyodyne, Inc., hereby disclaims all copyright interest in the program 330 | `Gnomovision' (which makes passes at compilers) written by James Hacker. 331 | 332 | , 1 April 1989 333 | Ty Coon, President of Vice 334 | 335 | This General Public License does not permit incorporating your program into 336 | proprietary programs. If your program is a subroutine library, you may 337 | consider it more useful to permit linking proprietary applications with the 338 | library. If this is what you want to do, use the GNU Lesser General 339 | Public License instead of this License. 340 | -------------------------------------------------------------------------------- /Makefile: -------------------------------------------------------------------------------- 1 | # 2 | # Makefile for TOA module. 3 | # 4 | 5 | 6 | #obj-$(CONFIG_TOA) += toa.o 7 | obj-m += toa.o 8 | all: 9 | make -C /lib/modules/`uname -r`/build M=$(PWD) 10 | #make -C /lib/modules/`uname -r`/build SUBDIRS=$(PWD) modules 11 | clean: 12 | make -C /lib/modules/`uname -r`/build M=$(PWD) clean 13 | #make -C /lib/modules/`uname -r`/build SUBDIRS=$(PWD) clean 14 | 15 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # TCP Option Address 2 | 3 | The TCP Option Address (TOA) module is a kernel module that obtains the client IPv4 address from the option section of a TCP header. 4 | 5 | It's typically used on the backends of LVS(toa enable). 6 | 7 | ### Features 8 | 9 | 1. Support kernel from 2.6.32 to the mainline now 10 | 2. Support IPV6 listen(golang default method) 11 | 12 | ### Requirements 13 | 14 | 1. Install kernel-devel, kernel-headers related kernel development packages which match the running kernel. 15 | 2. Install gcc and make 16 | 17 | ### Usage 18 | 19 | 1. Compiling the module 20 | 21 | ```bash 22 | make 23 | ``` 24 | 25 | 2. Load the module 26 | 27 | ```bash 28 | insmod ./toa.ko 29 | ``` 30 | 31 | ## Distribution license 32 | 33 | TOA is distributed under the terms of the GNU General Public License v2.0. The full terms and conditions of this license are detailed in the LICENSE file. 34 | -------------------------------------------------------------------------------- /dkms.conf: -------------------------------------------------------------------------------- 1 | PACKAGE_NAME="toa" 2 | PACKAGE_VERSION="1.0.0" 3 | BUILT_MODULE_NAME[0]="toa" 4 | DEST_MODULE_LOCATION[0]="/extra" 5 | AUTOINSTALL="yes" 6 | -------------------------------------------------------------------------------- /toa.c: -------------------------------------------------------------------------------- 1 | // SPDX-License-Identifier: GPL-2.0 2 | #include "toa.h" 3 | 4 | /* 5 | * TOA a new Tcp Option as Address, 6 | * here address including IP and Port. 7 | * the real {IP,Port} can be added into option field of TCP header, 8 | * with LVS FULLNAT model, the realservice are still able to receive real {IP,Port} info. 9 | * So far, this module only supports IPv4 and IPv6 mapped IPv4. 10 | * 11 | * Authors: 12 | * Wen Li 13 | * Yan Tian 14 | * Jiaming Wu 15 | * Jiajun Chen 16 | * Faicker Mo 17 | * 18 | * This program is free software; you can redistribute it and/or 19 | * modify it under the terms of the GNU General Public License 20 | * as published by the Free Software Foundation; either version 21 | * 2 of the License, or (at your option) any later version. 22 | * 23 | */ 24 | 25 | typedef unsigned long (*kallsyms_lookup_name_fp)(const char *name); 26 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(5, 7, 0) 27 | #define KPROBE_LOOKUP 1 28 | #include 29 | static struct kprobe kp = { 30 | .symbol_name = "kallsyms_lookup_name" 31 | }; 32 | #endif 33 | 34 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0) 35 | typedef struct sock *(*syn_recv_sock_fp)(struct sock *sk, struct sk_buff *skb, 36 | struct request_sock *req, 37 | struct dst_entry *dst); 38 | #else 39 | typedef struct sock *(*syn_recv_sock_fp)(const struct sock *sk, 40 | struct sk_buff *skb, 41 | struct request_sock *req, 42 | struct dst_entry *dst, 43 | struct request_sock *req_unhash, 44 | bool *own_req); 45 | #endif 46 | 47 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 15, 0) 48 | typedef void (*sk_data_ready_fp)(struct sock *sk, int bytes); 49 | #else 50 | typedef void (*sk_data_ready_fp)(struct sock *sk); 51 | #endif 52 | 53 | struct proto_ops *inet6_stream_ops_p; 54 | struct inet_connection_sock_af_ops *ipv6_specific_p; 55 | sk_data_ready_fp sk_data_ready_fn; 56 | 57 | syn_recv_sock_fp tcp_v6_syn_recv_sock_fn; // origin fn 58 | 59 | unsigned int is_ro_addr(unsigned long addr) 60 | { 61 | unsigned int level; 62 | unsigned int ro_enable = 0; 63 | pte_t *pte = lookup_address(addr, &level); 64 | 65 | if ((pte_val(*pte) & _PAGE_RW) == 0) 66 | ro_enable = 1; 67 | 68 | return ro_enable; 69 | } 70 | 71 | void set_addr_rw(unsigned long addr) 72 | { 73 | unsigned int level; 74 | pte_t *pte = lookup_address(addr, &level); 75 | 76 | if (pte->pte & ~_PAGE_RW) 77 | pte->pte |= _PAGE_RW; 78 | } 79 | 80 | void set_addr_ro(unsigned long addr) 81 | { 82 | unsigned int level; 83 | pte_t *pte = lookup_address(addr, &level); 84 | 85 | pte->pte = pte->pte & ~_PAGE_RW; 86 | } 87 | 88 | /* 89 | * Statistics of toa in proc /proc/net/toa_stats 90 | */ 91 | 92 | struct toa_stats_entry toa_stats[] = { 93 | TOA_STAT_ITEM("syn_recv_sock_toa", SYN_RECV_SOCK_TOA_CNT), 94 | TOA_STAT_ITEM("syn_recv_sock_no_toa", SYN_RECV_SOCK_NO_TOA_CNT), 95 | TOA_STAT_ITEM("getname_toa_ok", GETNAME_TOA_OK_CNT), 96 | TOA_STAT_ITEM("getname_toa_mismatch", GETNAME_TOA_MISMATCH_CNT), 97 | TOA_STAT_ITEM("getname_toa_bypass", GETNAME_TOA_BYPASS_CNT), 98 | TOA_STAT_ITEM("getname_toa_empty", GETNAME_TOA_EMPTY_CNT), 99 | TOA_STAT_END 100 | }; 101 | 102 | DEFINE_TOA_STAT(struct toa_stat_mib, ext_stats); 103 | 104 | /* 105 | * Funcs for toa hooks 106 | */ 107 | 108 | /* Parse TCP options in skb, try to get client ip, port 109 | * @param skb [in] received skb, it should be a ack/get-ack packet. 110 | * @return NULL if we don't get client ip/port; 111 | * value of toa_data in ret_ptr if we get client ip/port. 112 | */ 113 | static void *get_toa_data(struct sk_buff *skb) 114 | { 115 | struct tcphdr *th; 116 | int length; 117 | unsigned char *ptr; 118 | 119 | struct toa_data tdata; 120 | 121 | void *ret_ptr = NULL; 122 | 123 | //TOA_DBG("get_toa_data called\n"); 124 | 125 | if (skb != NULL) { 126 | th = tcp_hdr(skb); 127 | length = (th->doff * 4) - sizeof(struct tcphdr); 128 | ptr = (unsigned char *) (th + 1); 129 | 130 | while (length > 0) { 131 | int opcode = *ptr++; 132 | int opsize; 133 | 134 | switch (opcode) { 135 | case TCPOPT_EOL: 136 | return NULL; 137 | case TCPOPT_NOP: /* Ref: RFC 793 section 3.1 */ 138 | length--; 139 | continue; 140 | default: 141 | opsize = *ptr++; 142 | if (opsize < 2) /* "silly options" */ 143 | return NULL; 144 | if (opsize > length) 145 | return NULL; /* don't parse partial options */ 146 | if ((opcode == TCPOPT_TOA_UCLOUD || opcode == TCPOPT_TOA_COMPAT || opcode == TCPOPT_TOA_AKAMAI) && 147 | opsize == TCPOLEN_TOA) { 148 | memcpy(&tdata, ptr - 2, sizeof(tdata)); 149 | //TOA_DBG("find toa data: ip = %u.%u.%u.%u, port = %u\n", NIPQUAD(tdata.ip), 150 | //ntohs(tdata.port)); 151 | memcpy(&ret_ptr, &tdata, sizeof(ret_ptr)); 152 | //TOA_DBG("coded toa data: %p\n", ret_ptr); 153 | return ret_ptr; 154 | } 155 | ptr += opsize - 2; 156 | length -= opsize; 157 | } 158 | } 159 | } 160 | return NULL; 161 | } 162 | 163 | /* get client ip from socket 164 | * @param sock [in] the socket to getpeername() or getsockname() 165 | * @param uaddr [out] the place to put client ip, port 166 | * @param uaddr_len [out] lenth of @uaddr 167 | * @peer [in] if(peer), try to get remote address; if(!peer), try to get local address 168 | * @return return what the original inet_getname() returns. 169 | */ 170 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 171 | static int 172 | inet_getname_toa(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) 173 | #else 174 | static int 175 | inet_getname_toa(struct socket *sock, struct sockaddr *uaddr, int peer) 176 | #endif 177 | { 178 | int retval = 0; 179 | struct sock *sk = sock->sk; 180 | struct sockaddr_in *sin = (struct sockaddr_in *) uaddr; 181 | struct toa_data tdata; 182 | 183 | //TOA_DBG("inet_getname_toa called, sk->sk_user_data is %p\n", sk->sk_user_data); 184 | 185 | /* call orginal one */ 186 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 187 | retval = inet_getname(sock, uaddr, uaddr_len, peer); 188 | #else 189 | retval = inet_getname(sock, uaddr, peer); 190 | #endif 191 | 192 | /* set our value if need */ 193 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 194 | if (retval == 0 && NULL != sk->sk_user_data && peer) { 195 | #else 196 | if (retval >= 0 && NULL != sk->sk_user_data && peer) { 197 | #endif 198 | if (sk_data_ready_fn == sk->sk_data_ready) { 199 | memcpy(&tdata, &sk->sk_user_data, sizeof(tdata)); 200 | if ((tdata.opcode == TCPOPT_TOA_UCLOUD || tdata.opcode == TCPOPT_TOA_COMPAT || tdata.opcode == TCPOPT_TOA_AKAMAI) && 201 | tdata.opsize == TCPOLEN_TOA) { 202 | TOA_INC_STATS(ext_stats, GETNAME_TOA_OK_CNT); 203 | //TOA_DBG("inet_getname_toa: set new sockaddr, ip %pI4 -> %pI4, port %u -> %u\n", 204 | // &sin->sin_addr.s_addr, &tdata.ip, ntohs(sin->sin_port), 205 | // ntohs(tdata.port)); 206 | sin->sin_port = tdata.port; 207 | sin->sin_addr.s_addr = tdata.ip; 208 | } else { /* sk_user_data doesn't belong to us */ 209 | TOA_INC_STATS(ext_stats, GETNAME_TOA_MISMATCH_CNT); 210 | //TOA_DBG("inet_getname_toa: invalid toa data, ip %pI4 port %u opcode %u opsize %u\n", 211 | // &tdata.ip, ntohs(tdata.port), tdata.opcode, tdata.opsize); 212 | } 213 | } else { 214 | TOA_INC_STATS(ext_stats, GETNAME_TOA_BYPASS_CNT); 215 | } 216 | } else { /* no need to get client ip */ 217 | TOA_INC_STATS(ext_stats, GETNAME_TOA_EMPTY_CNT); 218 | } 219 | 220 | return retval; 221 | } 222 | 223 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 224 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 225 | static int 226 | inet6_getname_toa(struct socket *sock, struct sockaddr *uaddr, int *uaddr_len, int peer) 227 | #else 228 | static int 229 | inet6_getname_toa(struct socket *sock, struct sockaddr *uaddr, int peer) 230 | #endif 231 | { 232 | int retval = 0; 233 | struct sock *sk = sock->sk; 234 | struct sockaddr_in6 *sin = (struct sockaddr_in6 *) uaddr; 235 | struct toa_data tdata; 236 | 237 | //TOA_DBG("inet6_getname_toa called, sk->sk_user_data is %p\n", sk->sk_user_data); 238 | 239 | /* call orginal one */ 240 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 241 | retval = inet6_getname(sock, uaddr, uaddr_len, peer); 242 | #else 243 | retval = inet6_getname(sock, uaddr, peer); 244 | #endif 245 | 246 | /* set our value if need */ 247 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 17, 0) 248 | if (retval == 0 && NULL != sk->sk_user_data && peer) { 249 | #else 250 | if (retval >= 0 && NULL != sk->sk_user_data && peer) { 251 | #endif 252 | if (sk_data_ready_fn == sk->sk_data_ready) { 253 | memcpy(&tdata, &sk->sk_user_data, sizeof(tdata)); 254 | if ((tdata.opcode == TCPOPT_TOA_UCLOUD || tdata.opcode == TCPOPT_TOA_COMPAT || tdata.opcode == TCPOPT_TOA_AKAMAI) && 255 | tdata.opsize == TCPOLEN_TOA) { 256 | TOA_INC_STATS(ext_stats, GETNAME_TOA_OK_CNT); 257 | sin->sin6_port = tdata.port; 258 | ipv6_addr_set(&sin->sin6_addr, 0, 0, htonl(0x0000FFFF), tdata.ip); 259 | } else { /* sk_user_data doesn't belong to us */ 260 | TOA_INC_STATS(ext_stats, GETNAME_TOA_MISMATCH_CNT); 261 | } 262 | } else { 263 | TOA_INC_STATS(ext_stats, GETNAME_TOA_BYPASS_CNT); 264 | } 265 | } else { /* no need to get client ip */ 266 | TOA_INC_STATS(ext_stats, GETNAME_TOA_EMPTY_CNT); 267 | } 268 | 269 | return retval; 270 | } 271 | #endif 272 | 273 | /* The three way handshake has completed - we got a valid synack - 274 | * now create the new socket. 275 | * We need to save toa data into the new socket. 276 | * @param sk [out] the socket 277 | * @param skb [in] the ack/ack-get packet 278 | * @param req [in] the open request for this connection 279 | * @param dst [out] route cache entry 280 | * @return NULL if fail new socket if succeed. 281 | */ 282 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0) 283 | static struct sock * 284 | tcp_v4_syn_recv_sock_toa(struct sock *sk, struct sk_buff *skb, 285 | struct request_sock *req, struct dst_entry *dst) 286 | #else 287 | static struct sock * 288 | tcp_v4_syn_recv_sock_toa(const struct sock *sk, struct sk_buff *skb, 289 | struct request_sock *req, struct dst_entry *dst, 290 | struct request_sock *req_unhash, bool *own_req) 291 | #endif 292 | { 293 | struct sock *newsock = NULL; 294 | 295 | //TOA_DBG("tcp_v4_syn_recv_sock_toa called\n"); 296 | 297 | /* call orginal one */ 298 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0) 299 | newsock = tcp_v4_syn_recv_sock(sk, skb, req, dst); 300 | #else 301 | newsock = tcp_v4_syn_recv_sock(sk, skb, req, dst, req_unhash, own_req); 302 | #endif 303 | 304 | /* set our value if need */ 305 | if (NULL != newsock && NULL == newsock->sk_user_data) { 306 | newsock->sk_user_data = get_toa_data(skb); 307 | if (newsock->sk_user_data != NULL) 308 | TOA_INC_STATS(ext_stats, SYN_RECV_SOCK_TOA_CNT); 309 | else 310 | TOA_INC_STATS(ext_stats, SYN_RECV_SOCK_NO_TOA_CNT); 311 | //TOA_DBG("tcp_v4_syn_recv_sock_toa: set sk->sk_user_data to %p\n", newsock->sk_user_data); 312 | } 313 | return newsock; 314 | } 315 | 316 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 317 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0) 318 | static struct sock * 319 | tcp_v6_syn_recv_sock_toa(struct sock *sk, struct sk_buff *skb, 320 | struct request_sock *req, struct dst_entry *dst) 321 | #else 322 | static struct sock * 323 | tcp_v6_syn_recv_sock_toa(const struct sock *sk, struct sk_buff *skb, 324 | struct request_sock *req, struct dst_entry *dst, 325 | struct request_sock *req_unhash, bool *own_req) 326 | #endif 327 | { 328 | struct sock *newsock = NULL; 329 | 330 | //TOA_DBG("tcp_v6_syn_recv_sock_toa called\n"); 331 | 332 | /* call orginal one */ 333 | #if LINUX_VERSION_CODE < KERNEL_VERSION(4, 4, 0) 334 | newsock = tcp_v6_syn_recv_sock_fn(sk, skb, req, dst); 335 | #else 336 | newsock = tcp_v6_syn_recv_sock_fn(sk, skb, req, dst, req_unhash, own_req); 337 | #endif 338 | 339 | /* set our value if need */ 340 | if (NULL != newsock && NULL == newsock->sk_user_data) { 341 | newsock->sk_user_data = get_toa_data(skb); 342 | if (newsock->sk_user_data != NULL) 343 | TOA_INC_STATS(ext_stats, SYN_RECV_SOCK_TOA_CNT); 344 | else 345 | TOA_INC_STATS(ext_stats, SYN_RECV_SOCK_NO_TOA_CNT); 346 | //TOA_DBG("tcp_v6_syn_recv_sock_toa: set sk->sk_user_data to %p\n", newsock->sk_user_data); 347 | } 348 | return newsock; 349 | } 350 | #endif 351 | 352 | /* 353 | * HOOK FUNCS 354 | */ 355 | 356 | #define REPLACE_FUNC(VA, FIELD, NEW_FN) \ 357 | do { \ 358 | if (is_ro_addr((unsigned long)(&VA->FIELD))) { \ 359 | set_addr_rw((unsigned long)(&VA->FIELD)); \ 360 | rw_enable = 1; \ 361 | } \ 362 | VA->FIELD = NEW_FN; \ 363 | if (rw_enable == 1) { \ 364 | set_addr_ro((unsigned long)(&VA->FIELD)); \ 365 | rw_enable = 0; \ 366 | } \ 367 | } while (0) 368 | 369 | /* replace the functions with our functions */ 370 | static inline int 371 | hook_toa_functions(void) 372 | { 373 | struct proto_ops *inet_stream_ops_p; 374 | struct inet_connection_sock_af_ops *ipv4_specific_p; 375 | int rw_enable = 0; 376 | 377 | /* hook inet_getname for ipv4 */ 378 | inet_stream_ops_p = (struct proto_ops *)&inet_stream_ops; 379 | REPLACE_FUNC(inet_stream_ops_p, getname, inet_getname_toa); 380 | TOA_INFO("CPU [%u] hooked inet_getname <%p> --> <%p>\n", smp_processor_id(), 381 | inet_getname, inet_stream_ops_p->getname); 382 | 383 | 384 | /* hook tcp_v4_syn_recv_sock for ipv4 */ 385 | ipv4_specific_p = (struct inet_connection_sock_af_ops *)&ipv4_specific; 386 | REPLACE_FUNC(ipv4_specific_p, syn_recv_sock, tcp_v4_syn_recv_sock_toa); 387 | TOA_INFO("CPU [%u] hooked tcp_v4_syn_recv_sock <%p> --> <%p>\n", smp_processor_id(), 388 | tcp_v4_syn_recv_sock, ipv4_specific_p->syn_recv_sock); 389 | 390 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 391 | /* hook inet_getname for ipv6 */ 392 | REPLACE_FUNC(inet6_stream_ops_p, getname, inet6_getname_toa); 393 | TOA_INFO("CPU [%u] hooked inet6_getname <%p> --> <%p>\n", smp_processor_id(), 394 | inet6_getname, inet6_stream_ops_p->getname); 395 | 396 | /* hook tcp_v6_syn_recv_sock for ipv6 */ 397 | tcp_v6_syn_recv_sock_fn = ipv6_specific_p->syn_recv_sock; 398 | REPLACE_FUNC(ipv6_specific_p, syn_recv_sock, tcp_v6_syn_recv_sock_toa); 399 | TOA_INFO("CPU [%u] hooked tcp_v6_syn_recv_sock <%p> --> <%p>\n", smp_processor_id(), 400 | tcp_v6_syn_recv_sock_fn, ipv6_specific_p->syn_recv_sock); 401 | #endif 402 | return 0; 403 | } 404 | 405 | /* replace the functions to original ones */ 406 | static int 407 | unhook_toa_functions(void) 408 | { 409 | struct proto_ops *inet_stream_ops_p; 410 | struct inet_connection_sock_af_ops *ipv4_specific_p; 411 | int rw_enable = 0; 412 | 413 | /* unhook inet_getname for ipv4 */ 414 | inet_stream_ops_p = (struct proto_ops *)&inet_stream_ops; 415 | REPLACE_FUNC(inet_stream_ops_p, getname, inet_getname); 416 | TOA_INFO("CPU [%u] unhooked inet_getname\n", smp_processor_id()); 417 | 418 | /* unhook tcp_v4_syn_recv_sock for ipv4 */ 419 | ipv4_specific_p = (struct inet_connection_sock_af_ops *)&ipv4_specific; 420 | REPLACE_FUNC(ipv4_specific_p, syn_recv_sock, tcp_v4_syn_recv_sock); 421 | TOA_INFO("CPU [%u] unhooked tcp_v4_syn_recv_sock\n", smp_processor_id()); 422 | 423 | #if defined(CONFIG_IPV6) || defined(CONFIG_IPV6_MODULE) 424 | /* unhook inet_getname for ipv6 */ 425 | REPLACE_FUNC(inet6_stream_ops_p, getname, inet6_getname); 426 | TOA_INFO("CPU [%u] unhooked inet6_getname\n", smp_processor_id()); 427 | 428 | /* unhook tcp_v6_syn_recv_sock for ipv6 */ 429 | REPLACE_FUNC(ipv6_specific_p, syn_recv_sock, tcp_v6_syn_recv_sock_fn); 430 | TOA_INFO("CPU [%u] unhooked tcp_v6_syn_recv_sock\n", smp_processor_id()); 431 | #endif 432 | return 0; 433 | } 434 | 435 | /* 436 | * Statistics of toa in proc /proc/net/toa_stats 437 | */ 438 | static int toa_stats_show(struct seq_file *seq, void *v) 439 | { 440 | int i, j; 441 | 442 | /* print CPU first */ 443 | seq_puts(seq, " "); 444 | for (i = 0; i < num_possible_cpus(); i++) 445 | if (cpu_online(i)) 446 | seq_printf(seq, "CPU%d ", i); 447 | seq_putc(seq, '\n'); 448 | 449 | i = 0; 450 | while (toa_stats[i].name != NULL) { 451 | seq_printf(seq, "%-25s:", toa_stats[i].name); 452 | for (j = 0; j < num_possible_cpus(); j++) { 453 | if (cpu_online(j)) { 454 | seq_printf(seq, "%10lu ", 455 | *(((unsigned long *) per_cpu_ptr(ext_stats, j)) + toa_stats[i].entry)); 456 | } 457 | } 458 | seq_putc(seq, '\n'); 459 | i++; 460 | } 461 | return 0; 462 | } 463 | 464 | static int toa_stats_seq_open(struct inode *inode, struct file *file) 465 | { 466 | return single_open(file, toa_stats_show, NULL); 467 | } 468 | 469 | #if LINUX_VERSION_CODE < KERNEL_VERSION(5, 6, 0) 470 | static const struct file_operations toa_stats_fops = { 471 | .owner = THIS_MODULE, 472 | .open = toa_stats_seq_open, 473 | .read = seq_read, 474 | .llseek = seq_lseek, 475 | .release = single_release, 476 | }; 477 | #else 478 | static const struct proc_ops toa_stats_pops = { 479 | .proc_open = toa_stats_seq_open, 480 | .proc_read = seq_read, 481 | .proc_lseek = seq_lseek, 482 | .proc_release = single_release, 483 | }; 484 | #endif 485 | 486 | /* symbol */ 487 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) 488 | static int find_fn(void *data, const char *name, struct module *mod, unsigned long addr) 489 | { 490 | if (name != NULL) { 491 | if (strcmp(name, "sock_def_readable") == 0) 492 | sk_data_ready_fn = (sk_data_ready_fp)addr; 493 | else if (strcmp(name, "inet6_stream_ops") == 0) 494 | inet6_stream_ops_p = (struct proto_ops *)addr; 495 | else if (strcmp(name, "ipv6_specific") == 0) 496 | ipv6_specific_p = (struct inet_connection_sock_af_ops *)addr; 497 | } 498 | if (sk_data_ready_fn > 0 && inet6_stream_ops_p > 0 && ipv6_specific_p > 0) 499 | return 1; 500 | 501 | return 0; 502 | } 503 | #endif 504 | 505 | static int get_fn_addr(void) 506 | { 507 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 33) 508 | kallsyms_on_each_symbol(find_fn, 0); 509 | #else 510 | kallsyms_lookup_name_fp kallsyms_lookup_name_fn; 511 | #ifdef KPROBE_LOOKUP 512 | register_kprobe(&kp); 513 | kallsyms_lookup_name_fn = (kallsyms_lookup_name_fp) kp.addr; 514 | unregister_kprobe(&kp); 515 | #else 516 | kallsyms_lookup_name_fn = kallsyms_lookup_name; 517 | #endif 518 | sk_data_ready_fn = (sk_data_ready_fp)kallsyms_lookup_name_fn("sock_def_readable"); 519 | inet6_stream_ops_p = (struct proto_ops *)kallsyms_lookup_name_fn("inet6_stream_ops"); 520 | ipv6_specific_p = (struct inet_connection_sock_af_ops *)kallsyms_lookup_name_fn("ipv6_specific"); 521 | #endif 522 | if (sk_data_ready_fn > 0) 523 | TOA_INFO("CPU [%u] sk_data_ready_fn = kallsyms_lookup_name(sock_def_readable) = %p\n", 524 | smp_processor_id(), sk_data_ready_fn); 525 | else 526 | return 1; 527 | if (inet6_stream_ops_p > 0) 528 | TOA_INFO("CPU [%u] inet6_stream_ops_p = kallsyms_lookup_name(inet6_stream_ops) = %p\n", 529 | smp_processor_id(), inet6_stream_ops_p); 530 | else 531 | return 1; 532 | if (ipv6_specific_p > 0) 533 | TOA_INFO("CPU [%u] ipv6_specific_p = kallsyms_lookup_name(ipv6_specific) = %p\n", 534 | smp_processor_id(), ipv6_specific_p); 535 | else 536 | return 1; 537 | return 0; 538 | } 539 | 540 | /* 541 | * TOA module init and destory 542 | */ 543 | /* module init */ 544 | static int __init 545 | toa_init(void) 546 | { 547 | int ret; 548 | 549 | TOA_INFO("TOA " TOA_VERSION " by pukong.wjm\n"); 550 | 551 | /* alloc statistics array for toa */ 552 | ext_stats = alloc_percpu(struct toa_stat_mib); 553 | if (ext_stats == NULL) 554 | return 1; 555 | 556 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) 557 | proc_net_fops_create(&init_net, "toa_stats", 0, &toa_stats_fops); 558 | #elif LINUX_VERSION_CODE < KERNEL_VERSION(5, 6, 0) 559 | proc_create("toa_stats", 0444, init_net.proc_net, &toa_stats_fops); 560 | #else 561 | proc_create("toa_stats", 0444, init_net.proc_net, &toa_stats_pops); 562 | #endif 563 | 564 | /* get the address of function sock_def_readable 565 | * so later we can know whether the sock is for rpc, tux or others 566 | */ 567 | ret = get_fn_addr(); 568 | if (ret != 0) { 569 | TOA_INFO("cannot find symbols.\n"); 570 | goto err; 571 | } 572 | 573 | /* hook funcs for parse and get toa */ 574 | hook_toa_functions(); 575 | 576 | TOA_INFO("toa loaded\n"); 577 | return 0; 578 | 579 | err: 580 | 581 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) 582 | proc_net_remove(&init_net, "toa_stats"); 583 | #else 584 | remove_proc_entry("toa_stats", init_net.proc_net); 585 | #endif 586 | if (ext_stats != NULL) { 587 | free_percpu(ext_stats); 588 | ext_stats = NULL; 589 | } 590 | 591 | return 1; 592 | } 593 | 594 | /* module cleanup*/ 595 | static void __exit 596 | toa_exit(void) 597 | { 598 | unhook_toa_functions(); 599 | synchronize_net(); 600 | 601 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3, 9, 0) 602 | proc_net_remove(&init_net, "toa_stats"); 603 | #else 604 | remove_proc_entry("toa_stats", init_net.proc_net); 605 | #endif 606 | 607 | if (ext_stats != NULL) { 608 | free_percpu(ext_stats); 609 | ext_stats = NULL; 610 | } 611 | TOA_INFO("toa unloaded\n"); 612 | } 613 | 614 | module_init(toa_init); 615 | module_exit(toa_exit); 616 | MODULE_LICENSE("GPL v2"); 617 | -------------------------------------------------------------------------------- /toa.h: -------------------------------------------------------------------------------- 1 | /* SPDX-License-Identifier: GPL-2.0 */ 2 | #ifndef __NET__TOA_H__ 3 | #define __NET__TOA_H__ 4 | 5 | #include 6 | #include 7 | #include 8 | #include 9 | #include 10 | #include 11 | #include 12 | #include 13 | #include 14 | #include 15 | #include 16 | #include 17 | #include 18 | #include 19 | #include 20 | #include 21 | #include 22 | #include 23 | 24 | #define TOA_VERSION "2.0.0.0" 25 | 26 | #define TOA_DBG(msg...) printk(KERN_DEBUG "[DEBUG] TOA: " msg) 27 | 28 | #define TOA_INFO(msg...) \ 29 | do { \ 30 | if (net_ratelimit()) \ 31 | printk(KERN_INFO "TOA: " msg); \ 32 | } while (0) 33 | 34 | /* UCloud mode */ 35 | #define TCPOPT_TOA_UCLOUD 200 36 | /* compatible mode */ 37 | #define TCPOPT_TOA_COMPAT 254 38 | #define TCPOPT_TOA_AKAMAI 253 39 | 40 | /* MUST be 4n !!!! */ 41 | #define TCPOLEN_TOA 8 /* |opcode|size|ip+port| = 1 + 1 + 6 */ 42 | 43 | /* MUST be 4 bytes alignment */ 44 | struct toa_data { 45 | __u8 opcode; 46 | __u8 opsize; 47 | __u16 port; 48 | __u32 ip; 49 | }; 50 | 51 | /* statistics about toa in proc /proc/net/toa_stat */ 52 | enum { 53 | SYN_RECV_SOCK_TOA_CNT = 1, 54 | SYN_RECV_SOCK_NO_TOA_CNT, 55 | GETNAME_TOA_OK_CNT, 56 | GETNAME_TOA_MISMATCH_CNT, 57 | GETNAME_TOA_BYPASS_CNT, 58 | GETNAME_TOA_EMPTY_CNT, 59 | TOA_STAT_LAST 60 | }; 61 | 62 | struct toa_stats_entry { 63 | char *name; 64 | int entry; 65 | }; 66 | 67 | #define TOA_STAT_ITEM(_name, _entry) { \ 68 | .name = _name, \ 69 | .entry = _entry, \ 70 | } 71 | 72 | #define TOA_STAT_END { \ 73 | NULL, \ 74 | 0, \ 75 | } 76 | 77 | struct toa_stat_mib { 78 | unsigned long mibs[TOA_STAT_LAST]; 79 | }; 80 | 81 | #define DEFINE_TOA_STAT(type, name) \ 82 | __typeof__(type) *name 83 | #define TOA_INC_STATS(mib, field) \ 84 | (per_cpu_ptr(mib, smp_processor_id())->mibs[field]++) 85 | 86 | 87 | #endif 88 | 89 | --------------------------------------------------------------------------------