├── LICENSE ├── Makefile ├── README.md ├── dkms.conf └── tcp_probe_plus.c /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 | #This won't work if the CONFIG_NET_TCPPROBE isn't already configured 3 | #for the kernel 4 | #obj-$(CONFIG_NET_TCPPROBE) += tcp_probe.o 5 | 6 | obj-m += tcp_probe_plus.o 7 | 8 | all: modules 9 | 10 | modules: 11 | make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules 12 | 13 | modules_install: 14 | make -C /lib/modules/$(shell uname -r)/build M=$(PWD) modules_install 15 | 16 | clean: 17 | make -C /lib/modules/$(shell uname -r)/build M=$(PWD) clean 18 | -------------------------------------------------------------------------------- /README.md: -------------------------------------------------------------------------------- 1 | # tcp_probe_plus Linux Kernel Module 2 | 3 | ## License 4 | Please review the: 5 | - "LICENSE" file that is part of this distribution and in the same directory as this file 6 | - The header in the "tcp_probe_plus.c" file 7 | - test 8 | 9 | ## Description 10 | - Based on the "tcp_probe.c" Linux Kernel Module (LKM) by Stephen Hemminger 11 | - More statistics added by Lyatiss, Inc. 12 | - rttvar: The Round-Trip Time Variation as per RFC 2988 13 | - rto: The current retransmit timeout in milliseconds 14 | - lost: The number of packets currently considered to be lost 15 | - retrans: The number of packet retransmitted since the connection was established 16 | - inflight: The number of packets sent but not acknowledged yet 17 | - frto_counter: A counter to track the Forward RTO-Recovery algorithm as described in RFC 4138 18 | - rqueue: The number of bytes currently waiting to be read in the socket buffer a.ka. recvq 19 | - wqueue: The number of bytes currently waiting to be sent in the socket buffer a.k.a. sendq 20 | - socket_idf: The first sequence number seen for the connection to identify it. It can be used to detect that the connection has been reset and avoid reporting incorrect throughput. 21 | 22 | - More sampling options added by Lyatiss, Inc. 23 | - There are two options 24 | - Sample an ACK every sampling period (controlled by the Probe time setting as described below), or 25 | - Sample an ACK only if the congestion window has changed every sampling period 26 | - Sampling is done by maintaining a connections table (see the Connection hash table section below for details) 27 | - Add connection termination detection (reported as a magic value in the length field) 28 | - The support for this functionnality is only available for kernel versions >= 2.6.22. Before that, the instrumented method (tcp_done) was defined as an inline method that can't be instrumented through jprobe. 29 | 30 | ## Contents 31 | This repository contains: 32 | - `dkms.conf` Config file for dkms 33 | - `Makefile` Makefile 34 | - `tcp_probe_plus.c` Modified tcp_probe that does the sampling and collects more statistics (NOTE: Works on Linux kernel versions 2.6 and higher) 35 | - `LICENSE` GPLv2 license 36 | 37 | ## Building the module 38 | 1. Copy all the files in this folder to the target directory on your machine, e.g., `/usr/src/tcp_probe_plus` 39 | 2. (Optional) Install DKMS 40 | 41 | On Debian: 42 | 43 | apt-get install dkms 44 | 45 | 3. Install Linux kernel headers 46 | 47 | On Debian, execute the following commands to determine and then install the correct kernel headers 48 | 49 | ubuntu:/usr/src# uname -a 50 | Linux ubuntu 3.2.0-4-686-pae #1 SMP Ubuntu i686 GNU/Linux 51 | ubuntu:/usr/src# apt-get install linux-headers-3.2-0-4-686-pae 52 | 53 | 4. Build and install the LKM 54 | 55 | Using DKMS 56 | 57 | ubuntu@host:/usr/src$ sudo dkms add tcp_probe_plus 58 | 59 | Creating symlink /var/lib/dkms/tcp_probe_plus/1.1.6/source -> 60 | /usr/src/tcp_probe_plus-1.1.6 61 | 62 | DKMS: add completed. 63 | ubuntu@host:/usr/src$ sudo dkms build tcp_probe_plus/1.1.6 64 | ubuntu@host:/usr/src$ sudo dkms install tcp_probe_plus/1.1.6 65 | 66 | Or from the kernel source 67 | 68 | gentoo tcp_probe_plus # make modules modules_install 69 | make -C /lib/modules/3.7.10-gentoo/build M=/usr/src/tcp_probe_plus modules 70 | make[1]: Entering directory `/usr/src/linux-3.7.10-gentoo' 71 | CC [M] /usr/src/tcp_probe_plus/tcp_probe_plus.o 72 | Building modules, stage 2. 73 | MODPOST 1 modules 74 | CC /usr/src/tcp_probe_plus/tcp_probe_plus.mod.o 75 | LD [M] /usr/src/tcp_probe_plus/tcp_probe_plus.ko 76 | make[1]: Leaving directory `/usr/src/linux-3.7.10-gentoo' 77 | make -C /lib/modules/3.7.10-gentoo/build M=/usr/src/tcp_probe_plus modules_install 78 | make[1]: Entering directory `/usr/src/linux-3.7.10-gentoo' 79 | INSTALL /usr/src/tcp_probe_plus/tcp_probe_plus.ko 80 | DEPMOD 3.7.10-gentoo 81 | make[1]: Leaving directory `/usr/src/linux-3.7.10-gentoo' 82 | gentoo tcp_probe_plus # 83 | 84 | 85 | ## Loading the module 86 | 87 | ubuntu@host:~$ sudo modprobe tcp_probe_plus 88 | ubuntu@host:~$ sudo cat /proc/net/tcpprobe 89 | 2.178670575 10.160.229.127:22 10.2.146.10:65221 80 0x3d58a46e 0x3d58a46e 6 2147483647 524280 43 53 255 0 0 0 0 0 0 0x3d58a44e 90 | ... 91 | 92 | ## Exported Data 93 | 94 | The data collected by the LKM is exported through `/proc/net/tcpprobe` and is formatted using the following code: 95 | 96 | return scnprintf(tbuf, n, 97 | "%lu.%09lu %pI4:%u %pI4:%u %d %#llx %#x %u %u %u %u %u %u %u %u %u %u %u %u %#llx\n", 98 | (unsigned long) tv.tv_sec, 99 | (unsigned long) tv.tv_nsec, 100 | &p->saddr, ntohs(p->sport), 101 | &p->daddr, ntohs(p->dport), 102 | p->length, p->snd_nxt, p->snd_una, 103 | p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, 104 | p->rttvar, p->rto, p->lost, p->retrans, p->inflight, p->frto_counter, 105 | p->rqueue, p->wqueue, p->socket_idf); 106 | 107 | | Field | Description | 108 | | ----- | ------------| 109 | | tv.tv_sec | Seconds since tcpprobe loading | 110 | | tv.tv_nsec | Extra milliseconds since tcpprobe loading | 111 | | saddr | Source Address | 112 | | sport | Source Port | 113 | | daddr | Destination Address | 114 | | dport | Destination Port | 115 | | length | Length of the sampled packet (65535 when this is last sample of a connection)| 116 | | snd_nxt | Sequence number of next packet to be sent | 117 | | snd_una | Sequence number of last unacknowledged packet | 118 | | snd_cwnd | Current congestion window size (in number of packets) | 119 | | ssthresh | Slow-start threshold (in number of packets) | 120 | | snd_wnd | Receive window size (in number of packets) | 121 | | srtt | Smoothed rtt (in ms) | 122 | | rttvar | Standard deviation of the rtt (in ms) | 123 | | rto | Number of retransmit timeout events | 124 | | lost | Number of lost packets | 125 | | retrans | Number of retransmitted packets | 126 | | inflight | Number of packets sent but not yet acked | 127 | | frto_counter | Number of spurious RTO events | 128 | | rqueue | Number of bytes in the socket read queue | 129 | | wqueue | Number of bytes in the socket write queue | 130 | | socket_idf | First sequence number seen for the connection | 131 | 132 | ## Sysctl interface 133 | 134 | This LKM offers a sysctl interface to configure it. 135 | 136 | ### Configuration 137 | 138 | The following configuration parameters are available: 139 | 140 | ubuntu@host:~$ ls -al /proc/sys/net/lyatiss_cw_tcpprobe/ 141 | total 0 142 | dr-xr-xr-x 1 root root 0 Mar 6 00:18 . 143 | dr-xr-xr-x 1 root root 0 Mar 5 18:55 .. 144 | -r--r--r-- 1 root root 0 Mar 6 00:18 bufsize 145 | -rw-r--r-- 1 root root 0 Mar 6 00:18 debug 146 | -rw-r--r-- 1 root root 0 Mar 6 00:18 full 147 | -r--r--r-- 1 root root 0 Mar 6 00:18 hashsize 148 | -rw-r--r-- 1 root root 0 Mar 6 00:18 maxflows 149 | -rw-r--r-- 1 root root 0 Mar 6 00:18 port 150 | -rw-r--r-- 1 root root 0 Mar 6 00:18 probetime 151 | -rw-r--r-- 1 root root 0 Mar 6 00:18 purgetime 152 | 153 | #### Buffer size 154 | 155 | This parameter controls the minimum number of sampled packets that will be read from the `/proc/net/tcpprobe` buffer. 156 | 157 | NOTE: The read will block until the specified number of packets are available. 158 | 159 | - default is 4096 packets 160 | - x: number of sampled packets the buffer can store 161 | 162 | Example: 163 | 164 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/bufsize 165 | 4096 166 | ubuntu@host:~$ sudo sh -c 'echo 1024 > /proc/sys/net/lyatiss_cw_tcpprobe/bufsize' 167 | 168 | 169 | #### Enable/Disable debug information in kernel messages 170 | 171 | This parameter controls the debug level. 172 | 173 | - 0: no debug 174 | - 1: debug enabled 175 | - 2: trace enabled 176 | 177 | Example: 178 | 179 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/debug 180 | 1 181 | ubuntu@host:~$ sudo sh -c 'echo 0 > /proc/sys/net/lyatiss_cw_tcpprobe/debug' 182 | 183 | 184 | #### Sample every ACK packet or only on Congestion Window change 185 | 186 | This parameter determines how ACK packets are sampled. 187 | 188 | - 0: only sample on cwnd changes 189 | - 1: sample on every ack packet received 190 | 191 | Example: 192 | 193 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/full 194 | 1 195 | ubuntu@host:~$ sudo sh -c 'echo 0 > /proc/sys/net/lyatiss_cw_tcpprobe/full' 196 | 197 | #### Connection hash table (maxflows/hashsize) 198 | 199 | The memory used by the flow table is controlled by two parameters: 200 | 201 | - maxflows: Maximum number of flows tracked by this module. 202 | - hashsize: Size of the hashtable. 203 | 204 | ##### hashsize 205 | 206 | This parameter defines the size of the hashtable. 207 | 208 | - default size: automatically calculated - similar to the netfilter connection tracker 209 | - x: size of the hashtable - number of slots that the hashtable has 210 | 211 | A linked list is used to track flows that hash to the same slot in the hashtable. 212 | 213 | Example: 214 | 215 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/hashsize 216 | 0 217 | ubuntu@host:~$ sudo sh -c 'echo 16384 > /proc/sys/net/lyatiss_cw_tcpprobe/hashsize' 218 | 219 | Max flow (see maxflows) has a default value of 2 million flows (2000000). 220 | 221 | The minimum hashtable size is 32 slots. If you explicitly set a lower value, it will be reset to 32. 222 | 223 | If you leave the hashtable size to be auto-calculated, then it is based on system memory availability, as coded below, and capped at 16,384 slots. 224 | 225 | /* determine hash size (idea from nf_conntrack_core.c) */ 226 | if (!hashsize) { 227 | hashsize = (((totalram_pages << PAGE_SHIFT) / 16384) 228 | / sizeof(struct hlist_head)); 229 | if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE)) { 230 | hashsize = 16384; 231 | } 232 | } 233 | if (hashsize < 32) { 234 | hashsize = 32; 235 | } 236 | pr_info("Hashtable initialized with %u buckets\n", hashsize); 237 | 238 | ##### maxflows 239 | 240 | This parameter controls the maximum number of flows that this module will track. 241 | 242 | - default: 2,000,000 flows 243 | - x: maximum number of flows to track 244 | 245 | Example: 246 | 247 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/maxflows 248 | 2000000 249 | ubuntu@host:~$ sudo sh -c 'echo 1000000 > /proc/sys/net/lyatiss_cw_tcpprobe/maxflows' 250 | 251 | #### Port filtering 252 | 253 | This parameter controls the port-based filtering of the flows to track. 254 | 255 | - 0: no filtering 256 | - x: port to match. This is a single port number. If it matches any of the send or receive port, then the flow will be tracked. 257 | 258 | Example: 259 | 260 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/port 261 | 0 262 | ubuntu@host:~$ sudo sh -c 'echo 5001 > /proc/sys/net/lyatiss_cw_tcpprobe/port' 263 | 264 | 265 | #### Probe time 266 | 267 | Upon receiving an ACK, the receive time of the ACK is compared with the receive time of the previous ACK for the connection. If the time difference is equal to or more than the probe time, then this ACK is eligible to be written to `/proc/net/tcpprobe`. The probe time is configurable from user space. The default probe time is 500 ms. This value could be passed as a module initialization parameter or changed using this parameter. 268 | 269 | - default is 500 ms 270 | - x: sampling interval 271 | 272 | Example: 273 | 274 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/probetime 275 | 500 276 | ubuntu@host:~$ sudo sh -c 'echo 200 > /proc/sys/net/lyatiss_cw_tcpprobe/probetime' 277 | 278 | 279 | #### Purge time 280 | 281 | Every `purgetime` the flows that are not active anymore are removed from the flow table. The purge time is configurable from user space. The default purge time is 300 s. This value could be passed as a module initialization parameter or changed using this parameter. 282 | 283 | - default is 300 s 284 | - x: purge time interval 285 | 286 | Example: 287 | 288 | ubuntu@host:~$ more /proc/sys/net/lyatiss_cw_tcpprobe/purgetime 289 | 500 290 | ubuntu@host:~$ sudo sh -c 'echo 200 > /proc/sys/net/lyatiss_cw_tcpprobe/purgetime' 291 | 292 | 293 | ### Statistics 294 | 295 | This module offers several statistics about its internal behavior. 296 | 297 | ubuntu@host:~$ more /proc/net/stat/lyatiss_cw_tcpprobe 298 | Flows: active 4 mem 0K 299 | Hash: size 4721 mem 36K 300 | cpu# hash_stat: , ack_drop: , 301 | conn_drop: , err: 302 | Total: hash_stat: 0 25877 151 147, ack_drop: 0 0, 303 | conn_drop: 0 0, err: 0 0 304 | 305 | Description: 306 | 307 | - Flows 308 | - active: Number of active flows being monitored by the module at present. 309 | - mem: Total memory used by the flow table to monitor the current set of flows. 310 | - Hash 311 | - size: Number of slots in the hash table (hashtable size). 312 | - mem: Total memory used by the hash table. 313 | - hash_stat 314 | - search_flows: Number of flows looked up so far in the hash table. 315 | - found: Number of flows found in the hash table. 316 | - new: Number of new flow entries created so far. 317 | - reset: Number of flows entries that have been invalidated because the flows have been closed/reset. 318 | - ack_drop 319 | - purge_in_progress: Number of ACK packets skipped by this module because flow purging was in progress (NOTE: this requires locking the flow table). 320 | - ring_full: Number of ACK packets dropped because of a slow reader (NOTE: User space process reading `/proc/net/tcpprobe`) 321 | - conn_drop 322 | - maxlfow_reached: New flow was skipped because maximum number of flows (2 million by default) has already been reached. 323 | - memory_alloc_failed: New flow was skipped because module was unable to allocate memory for the new flow entry. 324 | - err 325 | - multiple_reader: Module detected multiple readers while writing to `/proc/net/tcpprobe`. Note that multiple readers are not supported. Each reader will see only part of the flow. 326 | - copy_failed: Unable to copy the data to the user-space. 327 | -------------------------------------------------------------------------------- /dkms.conf: -------------------------------------------------------------------------------- 1 | PACKAGE_VERSION="1.1.6" 2 | 3 | # Items below here should not have to change with each driver version 4 | PACKAGE_NAME="tcp_probe_plus" 5 | MAKE[0]="make -C ${kernel_source_dir} SUBDIRS=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build modules" 6 | CLEAN="make -C ${kernel_source_dir} SUBDIRS=${dkms_tree}/${PACKAGE_NAME}/${PACKAGE_VERSION}/build clean" 7 | 8 | BUILT_MODULE_NAME[0]="tcp_probe_plus" 9 | DEST_MODULE_LOCATION[0]="/kernel/net/ipv4" 10 | MODULES_CONF_ALIAS_TYPE[0]="tcp_probe_plus" 11 | 12 | REMAKE_INITRD="no" 13 | -------------------------------------------------------------------------------- /tcp_probe_plus.c: -------------------------------------------------------------------------------- 1 | /* 2 | * tcpprobe - Observe the TCP flow with kprobes. 3 | * 4 | * The idea for this came from Werner Almesberger's umlsim 5 | * Copyright (C) 2004, Stephen Hemminger 6 | * 7 | * Extended by Lyatiss, Inc. to support 8 | * per-connection sampling, added additional metrics 9 | * and signaling of RST/FIN connections. 10 | * Please see the README.md file in the same directory for details. 11 | * 12 | * 13 | * This program is free software; you can redistribute it and/or modify 14 | * it under the terms of the GNU General Public License as published by 15 | * the Free Software Foundation; either version 2 of the License. 16 | * 17 | * This program is distributed in the hope that it will be useful, 18 | * but WITHOUT ANY WARRANTY; without even the implied warranty of 19 | * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the 20 | * GNU General Public License for more details. 21 | * 22 | * You should have received a copy of the GNU General Public License 23 | * along with this program; if not, write to the Free Software 24 | * Foundation, Inc., 675 Mass Ave, Cambridge, MA 02139, USA. 25 | */ 26 | 27 | #include 28 | #include 29 | #include 30 | #include 31 | #include 32 | #include 33 | #include 34 | #include 35 | #include 36 | #include 37 | #include 38 | #include 39 | #include 40 | #include 41 | #include 42 | #include 43 | 44 | 45 | #include 46 | 47 | MODULE_AUTHOR("Stephen Hemminger "); 48 | MODULE_DESCRIPTION("TCP cwnd snooper"); 49 | MODULE_LICENSE("GPL"); 50 | MODULE_VERSION("1.1.6"); 51 | 52 | static int port __read_mostly = 0; 53 | MODULE_PARM_DESC(port, "Port to match (0=all)"); 54 | module_param(port, int, 0); 55 | 56 | static unsigned int bufsize __read_mostly = 4096; 57 | MODULE_PARM_DESC(bufsize, "Log buffer size in packets (4096)"); 58 | module_param(bufsize, uint, 0); 59 | 60 | static int full __read_mostly; 61 | MODULE_PARM_DESC(full, "Full log (1=every ack packet received, 0=only cwnd changes)"); 62 | module_param(full, int, 0); 63 | 64 | static int probetime __read_mostly = 500; 65 | MODULE_PARM_DESC(probetime, "Probe time to write flows in milliseconds (500 milliseconds)"); 66 | module_param(probetime, int, 0); 67 | 68 | static int hashsize __read_mostly = 0; 69 | MODULE_PARM_DESC(hashsize, "hash table size"); 70 | module_param(hashsize, int, 0); 71 | 72 | static int maxflows __read_mostly = 2000000; 73 | MODULE_PARM_DESC(maxflows, "Maximum number of flows"); 74 | module_param(maxflows, int, 0); 75 | 76 | static int debug __read_mostly = 0; 77 | MODULE_PARM_DESC(debug, "Enable debug messages (Default 0) debug=1, trace=2"); 78 | module_param(debug, int , 0); 79 | 80 | static int purgetime __read_mostly = 300; 81 | MODULE_PARM_DESC(purgetime, "Max inactivity in seconds before purging a flow (Default 300 seconds)"); 82 | 83 | #define PROC_TCPPROBE "tcpprobe" 84 | 85 | #define PROC_SYSCTL_TCPPROBE "lyatiss_cw_tcpprobe" 86 | #define PROC_STAT_TCPPROBE "lyatiss_cw_tcpprobe" 87 | 88 | #define UINT32_MAX (u32)(~((u32) 0)) /* 0xFFFFFFFF */ 89 | #define UINT16_MAX (u16)(~((u16) 0)) /* 0xFFFF */ 90 | #define DEBUG_DISABLE 0 91 | #define DEBUG_ENABLE 1 92 | #define TRACE_ENABLE 2 93 | #define PRINT_DEBUG(fmt, arg...) \ 94 | do { \ 95 | if (debug == DEBUG_ENABLE) { \ 96 | pr_info(fmt, ##arg); \ 97 | } \ 98 | } while(0) 99 | 100 | #define PRINT_TRACE(fmt, arg...) \ 101 | do { \ 102 | if (debug == DEBUG_ENABLE || debug == TRACE_ENABLE) { \ 103 | pr_info(fmt, ##arg); \ 104 | } \ 105 | } while(0) 106 | 107 | #ifndef pr_err 108 | #define pr_err(fmt, arg...) pr_info(fmt, ##arg) 109 | #endif 110 | 111 | 112 | struct tcp_tuple { 113 | __be32 saddr; 114 | __be32 daddr; 115 | __be16 sport; 116 | __be16 dport; 117 | }; 118 | 119 | /* tuple size is rounded to u32s */ 120 | #define TCP_TUPLE_SIZE (sizeof(struct tcp_tuple) / 4) 121 | 122 | struct tcp_hash_flow { 123 | struct hlist_node hlist; // hashtable search chain 124 | struct list_head list; // all flows chain 125 | 126 | /* unique per flow data (hashed, TCP_TUPLE_SIZE) */ 127 | struct tcp_tuple tuple; 128 | 129 | /* Last ACK Timestamp */ 130 | ktime_t tstamp; 131 | /* Cumulative bytes sent */ 132 | u64 cumulative_bytes; 133 | /* remember last sequence number */ 134 | u32 last_seq_num; 135 | u64 first_seq_num; 136 | }; 137 | 138 | /* statistics */ 139 | struct tcpprobe_stat { 140 | u64 ack_drop_purge; /* ACK dropped due to purge in progress */ 141 | u64 ack_drop_ring_full; /* ACK dropped due to slow reader */ 142 | u64 conn_maxflow_limit; /* Connection skipped due maxflow limit */ 143 | u64 conn_memory_limit; /* Connection skipped because memory was unavailable */ 144 | u64 searched; /* hash stat - searched */ 145 | u64 found; /* hash stat - found */ 146 | u64 notfound; /* hash stat - not found */ 147 | u64 multiple_readers; /* Multiple readers for /proc/net/tcpprobe */ 148 | u64 copy_error; /* Userspace copy error */ 149 | u64 reset_flows; /* Number of FIN/RST received that caused to purge the flow */ 150 | }; 151 | 152 | #define TCPPROBE_STAT_INC(count) (__get_cpu_var(tcpprobe_stat).count++) 153 | 154 | struct tcp_log { 155 | ktime_t tstamp; 156 | __be32 saddr, daddr; 157 | __be16 sport, dport; 158 | u16 length; 159 | u64 snd_nxt; 160 | u32 snd_una; 161 | u32 snd_wnd; 162 | u32 snd_cwnd; 163 | u32 ssthresh; 164 | u32 srtt; 165 | u32 rttvar; 166 | u32 lost; 167 | u32 retrans; 168 | u32 inflight; 169 | u32 rto; 170 | u8 frto_counter; 171 | u32 rqueue; 172 | u32 wqueue; 173 | u64 socket_idf; 174 | }; 175 | 176 | static struct { 177 | spinlock_t lock; 178 | wait_queue_head_t wait; 179 | ktime_t start; 180 | u32 lastcwnd; 181 | 182 | unsigned long head, tail; 183 | struct tcp_log *log; 184 | } tcp_probe; 185 | 186 | static unsigned int tcp_hash_rnd; 187 | static struct hlist_head *tcp_hash __read_mostly; /* hash table memory */ 188 | static unsigned int tcp_hash_size __read_mostly = 0; /* buckets */ 189 | static struct kmem_cache *tcp_flow_cachep __read_mostly; /* tcp flow memory */ 190 | static DEFINE_SPINLOCK(tcp_hash_lock); /* hash table lock */ 191 | static LIST_HEAD(tcp_flow_list); /* all flows */ 192 | static struct timer_list purge_timer; 193 | static atomic_t flow_count = ATOMIC_INIT(0); 194 | static DEFINE_PER_CPU(struct tcpprobe_stat, tcpprobe_stat); 195 | 196 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,24) 197 | #define INIT_NET(x) x 198 | #else 199 | #define INIT_NET(x) init_net.x 200 | #endif 201 | 202 | //Needed because symbol ns_to_timespec is not always exported... 203 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 204 | struct timespec ns_to_timespec(const s64 nsec) 205 | { 206 | struct timespec ts; 207 | s32 rem; 208 | 209 | if (!nsec) 210 | return (struct timespec) {0, 0}; 211 | 212 | ts.tv_sec = div_s64_rem(nsec, NSEC_PER_SEC, &rem); 213 | if (unlikely(rem < 0)) { 214 | ts.tv_sec--; 215 | rem += NSEC_PER_SEC; 216 | } 217 | ts.tv_nsec = rem; 218 | 219 | return ts; 220 | } 221 | #endif 222 | 223 | 224 | static inline int tcp_probe_used(void) 225 | { 226 | return (tcp_probe.head - tcp_probe.tail) & (bufsize - 1); 227 | } 228 | 229 | static inline int tcp_probe_avail(void) 230 | { 231 | return bufsize - tcp_probe_used() - 1; 232 | } 233 | 234 | static inline int tcp_tuple_equal(const struct tcp_tuple *t1, 235 | const struct tcp_tuple *t2) 236 | { 237 | return (!memcmp(t1, t2, sizeof(struct tcp_tuple))); 238 | } 239 | 240 | static inline u_int32_t hash_tcp_flow(const struct tcp_tuple *tuple) 241 | { 242 | /* tuple is rounded to u32s */ 243 | return jhash2((u32 *)tuple, TCP_TUPLE_SIZE, tcp_hash_rnd) % tcp_hash_size; 244 | } 245 | 246 | static struct tcp_hash_flow* 247 | tcp_flow_find(const struct tcp_tuple *tuple, unsigned int hash) 248 | { 249 | struct tcp_hash_flow *flow; 250 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,9,0) 251 | struct hlist_node *pos; 252 | hlist_for_each_entry(flow, pos, &tcp_hash[hash], hlist) { 253 | #else 254 | //Second argument was removed 255 | hlist_for_each_entry(flow, &tcp_hash[hash], hlist) { 256 | #endif 257 | if (tcp_tuple_equal(tuple, &flow->tuple)) { 258 | TCPPROBE_STAT_INC(found); 259 | return flow; 260 | } 261 | TCPPROBE_STAT_INC(searched); 262 | } 263 | TCPPROBE_STAT_INC(notfound); 264 | return NULL; 265 | } 266 | 267 | static struct hlist_head * alloc_hashtable(int size) 268 | { 269 | struct hlist_head *hash; 270 | hash = vmalloc(sizeof(struct hlist_head) * size); 271 | if (hash) { 272 | int i; 273 | for (i = 0; i < size; i++) { 274 | INIT_HLIST_HEAD(&hash[i]); 275 | } 276 | } else { 277 | pr_err("Unable to vmalloc hash table size = %d\n", size); 278 | } 279 | return hash; 280 | } 281 | 282 | static struct tcp_hash_flow* 283 | tcp_hash_flow_alloc(struct tcp_tuple *tuple) 284 | { 285 | struct tcp_hash_flow *flow; 286 | flow = kmem_cache_alloc(tcp_flow_cachep, GFP_ATOMIC); 287 | if (!flow) { 288 | pr_err("Cannot allocate tcp_hash_flow.\n"); 289 | TCPPROBE_STAT_INC(conn_memory_limit); 290 | return NULL; 291 | } 292 | memset(flow, 0, sizeof(struct tcp_hash_flow)); 293 | flow->tuple = *tuple; 294 | atomic_inc(&flow_count); 295 | return flow; 296 | } 297 | 298 | static void tcp_hash_flow_free(struct tcp_hash_flow *flow) 299 | { 300 | atomic_dec(&flow_count); 301 | kmem_cache_free(tcp_flow_cachep, flow); 302 | } 303 | 304 | static struct tcp_hash_flow* 305 | init_tcp_hash_flow(struct tcp_tuple *tuple, 306 | ktime_t tstamp, unsigned int hash) 307 | { 308 | struct tcp_hash_flow *flow; 309 | flow = tcp_hash_flow_alloc(tuple); 310 | if (!flow) { 311 | return NULL; 312 | } 313 | flow->tstamp = tstamp; 314 | hlist_add_head(&flow->hlist, &tcp_hash[hash]); 315 | INIT_LIST_HEAD(&flow->list); 316 | list_add(&flow->list, &tcp_flow_list); 317 | 318 | return flow; 319 | } 320 | 321 | static void purge_timer_run(unsigned long dummy) 322 | { 323 | struct tcp_hash_flow *flow; 324 | struct tcp_hash_flow *temp; 325 | 326 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 327 | struct timespec ts; 328 | ktime_t tstamp; 329 | getnstimeofday(&ts); 330 | tstamp = timespec_to_ktime(ts); 331 | #else 332 | ktime_t tstamp = ktime_get(); 333 | #endif 334 | 335 | PRINT_DEBUG("Running purge timer.\n"); 336 | spin_lock(&tcp_hash_lock); 337 | list_for_each_entry_safe(flow, temp, &tcp_flow_list, list) { 338 | 339 | struct timespec tv = ktime_to_timespec(ktime_sub(tstamp, flow->tstamp)); 340 | 341 | if (tv.tv_sec >= purgetime) { 342 | PRINT_DEBUG("Purging flow src: %pI4 dst: %pI4" 343 | " src_port: %u dst_port: %u\n", 344 | &flow->tuple.saddr, &flow->tuple.daddr, 345 | ntohs(flow->tuple.sport), ntohs(flow->tuple.dport)); 346 | // Remove from Hashtable 347 | hlist_del(&flow->hlist); 348 | // Remove from Global List 349 | list_del(&flow->list); 350 | // Free memory 351 | tcp_hash_flow_free(flow); 352 | } 353 | } 354 | spin_unlock(&tcp_hash_lock); 355 | mod_timer(&purge_timer, jiffies + (HZ * purgetime)); 356 | } 357 | 358 | static void purge_all_flows(void) 359 | { 360 | //Method to make sure to release all memory before calling kmem_cache_destroy 361 | struct tcp_hash_flow *flow; 362 | struct tcp_hash_flow *temp; 363 | 364 | PRINT_DEBUG("Purging all flows.\n"); 365 | spin_lock(&tcp_hash_lock); 366 | list_for_each_entry_safe(flow, temp, &tcp_flow_list, list) { 367 | // Remove from Hashtable 368 | hlist_del(&flow->hlist); 369 | // Remove from Global List 370 | list_del(&flow->list); 371 | // Free memory 372 | tcp_hash_flow_free(flow); 373 | 374 | } 375 | spin_unlock(&tcp_hash_lock); 376 | 377 | } 378 | 379 | 380 | 381 | /* 382 | * Utility function to write the flow record 383 | * Assumes that the spin_lock on the tcp_probe has been taken 384 | * before calling it 385 | */ 386 | static int write_flow(struct tcp_tuple *tuple, const struct tcp_sock *tp, ktime_t tstamp, 387 | u64 cumulative_bytes, u16 length, u32 ssthresh, 388 | struct sock *sk, u64 first_seq_seen){ 389 | 390 | /* If log fills, just silently drop */ 391 | if (tcp_probe_avail() > 1) { 392 | struct tcp_log *p = tcp_probe.log + tcp_probe.head; 393 | 394 | p->tstamp = tstamp; 395 | p->saddr = tuple->saddr; 396 | p->sport = tuple->sport; 397 | p->daddr = tuple->daddr; 398 | p->dport = tuple->dport; 399 | p->length = length; 400 | /* update the cumulative bytes */ 401 | p->snd_nxt = cumulative_bytes; 402 | p->snd_una = tp->snd_una; 403 | p->snd_cwnd = tp->snd_cwnd; 404 | p->snd_wnd = tp->snd_wnd; 405 | p->ssthresh = ssthresh; 406 | 407 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,15,0) 408 | p->srtt = jiffies_to_usecs(tp->srtt) >> 3; 409 | p->rttvar = jiffies_to_usecs(tp->rttvar) >>3; 410 | #else 411 | /* element was renamed */ 412 | p->srtt = tp->srtt_us >> 3; 413 | p->rttvar = tp->rttvar_us >>3; 414 | #endif 415 | 416 | p->lost = tp->lost_out; 417 | p->retrans = tp->total_retrans; 418 | p->inflight = tp->packets_out; 419 | p->rto = p->srtt + (4 * p->rttvar); 420 | 421 | #if LINUX_VERSION_CODE < KERNEL_VERSION(3,10,0) 422 | p->frto_counter = tp->frto_counter; 423 | #else 424 | p->frto_counter = tp->frto; 425 | #endif 426 | 427 | /* same method as tcp_diag to retrieve the queue sizes */ 428 | if (sk->sk_state == TCP_LISTEN) { 429 | p->rqueue = sk->sk_ack_backlog; 430 | p->wqueue = sk->sk_max_ack_backlog; 431 | } else { 432 | p->rqueue = max_t(int, tp->rcv_nxt - tp->copied_seq, 0); 433 | p->wqueue = tp->write_seq - tp->snd_una; 434 | } 435 | 436 | p->socket_idf = first_seq_seen; 437 | 438 | tcp_probe.head = (tcp_probe.head + 1) & (bufsize - 1); 439 | } else { 440 | TCPPROBE_STAT_INC(ack_drop_ring_full); 441 | } 442 | tcp_probe.lastcwnd = tp->snd_cwnd; 443 | return 0; 444 | } 445 | 446 | 447 | 448 | /* 449 | * Hook inserted to be called before each time a socket is close 450 | * This allow us to purge/flush the corresponding infos 451 | * Note: arguments must match tcp_done()! 452 | * 453 | */ 454 | static void jtcp_done(struct sock *sk) 455 | { 456 | 457 | const struct tcp_sock *tp = tcp_sk(sk); 458 | const struct inet_sock *inet = inet_sk(sk); 459 | struct tcp_tuple tuple; 460 | struct tcp_hash_flow *tcp_flow; 461 | unsigned int hash; 462 | u64 cumulative_bytes = 0; 463 | 464 | 465 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 466 | struct timespec ts; 467 | ktime_t tstamp; 468 | getnstimeofday(&ts); 469 | tstamp = timespec_to_ktime(ts); 470 | #else 471 | ktime_t tstamp = ktime_get(); 472 | #endif 473 | 474 | 475 | 476 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32) 477 | tuple.saddr = inet->inet_saddr; 478 | tuple.daddr = inet->inet_daddr; 479 | tuple.sport = inet->inet_sport; 480 | tuple.dport = inet->inet_dport; 481 | #else 482 | tuple.saddr = inet->saddr; 483 | tuple.daddr = inet->daddr; 484 | tuple.sport = inet->sport; 485 | tuple.dport = inet->dport; 486 | #endif 487 | 488 | PRINT_DEBUG("Reset flow src: %pI4 dst: %pI4" 489 | " src_port: %u dst_port: %u\n", 490 | &tuple.saddr, &tuple.daddr, 491 | ntohs(tuple.sport), ntohs(tuple.dport)); 492 | 493 | hash = hash_tcp_flow(&tuple); 494 | /* Making sure that we are the only one touching this flow */ 495 | spin_lock(&tcp_hash_lock); 496 | 497 | tcp_flow = tcp_flow_find(&tuple, hash); 498 | if (!tcp_flow) { 499 | /*We just saw the FIN for this one so we can probably forget it */ 500 | PRINT_DEBUG("FIN for flow src: %pI4 dst: %pI4" 501 | " src_port: %u dst_port: %u but no corresponding hash\n", 502 | &tuple.saddr, &tuple.daddr, 503 | ntohs(tuple.sport), ntohs(tuple.dport)); 504 | spin_unlock(&tcp_hash_lock); 505 | goto skip; 506 | } else { 507 | /*Retrieve the last value of the cumulative_bytes */ 508 | if (tp->snd_nxt > tcp_flow->last_seq_num) { 509 | tcp_flow->cumulative_bytes += (tp->snd_nxt - tcp_flow->last_seq_num); 510 | } else if (tp->snd_nxt != tcp_flow->last_seq_num) { /* Retransmits */ 511 | /* sequence number rollover. For 10 Gbits/sec flow this will 512 | happen every 4 seconds */ 513 | tcp_flow->cumulative_bytes += ((UINT32_MAX - tcp_flow->last_seq_num) + tp->snd_nxt); 514 | } 515 | tcp_flow->last_seq_num = tp->snd_nxt; 516 | cumulative_bytes = tcp_flow->cumulative_bytes; 517 | 518 | } 519 | 520 | //Get the other lock and write 521 | spin_lock(&tcp_probe.lock); 522 | TCPPROBE_STAT_INC(reset_flows); 523 | write_flow(&tuple, tp, tstamp, 524 | cumulative_bytes, UINT16_MAX, tcp_current_ssthresh(sk), sk, tcp_flow->first_seq_num); 525 | 526 | spin_unlock(&tcp_probe.lock); 527 | 528 | /* Release the flow tuple*/ 529 | // Remove from Hashtable 530 | hlist_del(&tcp_flow->hlist); 531 | // Remove from Global List 532 | list_del(&tcp_flow->list); 533 | // Free memory 534 | tcp_hash_flow_free(tcp_flow); 535 | 536 | spin_unlock(&tcp_hash_lock); 537 | wake_up(&tcp_probe.wait); 538 | 539 | skip: 540 | jprobe_return(); 541 | return; 542 | } 543 | 544 | /* 545 | * Hook inserted to be called before each receive packet. 546 | * Note: arguments must match tcp_rcv_established()! 547 | */ 548 | static int jtcp_rcv_established(struct sock *sk, struct sk_buff *skb, 549 | struct tcphdr *th, unsigned len) 550 | { 551 | 552 | const struct tcp_sock *tp = tcp_sk(sk); 553 | const struct inet_sock *inet = inet_sk(sk); 554 | int should_write_flow = 0; 555 | u16 length = skb->len; 556 | struct tcp_tuple tuple; 557 | struct tcp_hash_flow *tcp_flow; 558 | unsigned int hash; 559 | u64 cumulative_bytes = 0; 560 | 561 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 562 | struct timespec ts; 563 | ktime_t tstamp; 564 | getnstimeofday(&ts); 565 | tstamp = timespec_to_ktime(ts); 566 | #else 567 | ktime_t tstamp = ktime_get(); 568 | #endif 569 | 570 | #if LINUX_VERSION_CODE > KERNEL_VERSION(2,6,32) 571 | tuple.saddr = inet->inet_saddr; 572 | tuple.daddr = inet->inet_daddr; 573 | tuple.sport = inet->inet_sport; 574 | tuple.dport = inet->inet_dport; 575 | #else 576 | tuple.saddr = inet->saddr; 577 | tuple.daddr = inet->daddr; 578 | tuple.sport = inet->sport; 579 | tuple.dport = inet->dport; 580 | #endif 581 | 582 | hash = hash_tcp_flow(&tuple); 583 | if (spin_trylock(&tcp_hash_lock) == 0) { 584 | /* Purge is ongoing.. skip this ACK */ 585 | TCPPROBE_STAT_INC(ack_drop_purge); 586 | goto skip; 587 | } 588 | tcp_flow = tcp_flow_find(&tuple, hash); 589 | 590 | if (!tcp_flow) { 591 | if (maxflows > 0 && atomic_read(&flow_count) >= maxflows) { 592 | /* This is DOC attack prevention */ 593 | TCPPROBE_STAT_INC(conn_maxflow_limit); 594 | PRINT_DEBUG("Flow count = %u execeed max flow = %u\n", 595 | atomic_read(&flow_count), maxflows); 596 | } else { 597 | /* create an entry in hashtable */ 598 | PRINT_DEBUG("Init new flow src: %pI4 dst: %pI4" 599 | " src_port: %u dst_port: %u\n", 600 | &tuple.saddr, &tuple.daddr, 601 | ntohs(tuple.sport), ntohs(tuple.dport)); 602 | tcp_flow = init_tcp_hash_flow(&tuple, tstamp, hash); 603 | tcp_flow->first_seq_num = tp->snd_nxt; 604 | should_write_flow = 1; 605 | } 606 | } else { 607 | /* if the difference between timestamps is >= probetime then write the flow to ring */ 608 | struct timespec tv = ktime_to_timespec(ktime_sub(tstamp, tcp_flow->tstamp)); 609 | u_int64_t milliseconds = (tv.tv_sec * MSEC_PER_SEC) + (tv.tv_nsec/NSEC_PER_MSEC); 610 | if (milliseconds >= probetime) { 611 | tcp_flow->tstamp = tstamp; 612 | should_write_flow = 1; 613 | } 614 | } 615 | if (should_write_flow) { 616 | if (tp->snd_nxt > tcp_flow->last_seq_num) { 617 | tcp_flow->cumulative_bytes += (tp->snd_nxt - tcp_flow->last_seq_num); 618 | } else if (tp->snd_nxt != tcp_flow->last_seq_num) { /* Retransmits */ 619 | /* sequence number rollover. For 10 Gbits/sec flow this will 620 | happen every 4 seconds */ 621 | tcp_flow->cumulative_bytes += ((UINT32_MAX - tcp_flow->last_seq_num) + tp->snd_nxt); 622 | } 623 | tcp_flow->last_seq_num = tp->snd_nxt; 624 | cumulative_bytes = tcp_flow->cumulative_bytes; 625 | } 626 | 627 | /* Only update if port matches */ 628 | if ((port == 0 || ntohs(tuple.dport) == port || 629 | ntohs(tuple.sport) == port) && 630 | (full || tp->snd_cwnd != tcp_probe.lastcwnd) && 631 | should_write_flow) { 632 | 633 | spin_lock(&tcp_probe.lock); 634 | write_flow(&tuple, tp, tstamp, 635 | cumulative_bytes, length, tcp_current_ssthresh(sk), sk, tcp_flow->first_seq_num); 636 | 637 | spin_unlock(&tcp_probe.lock); 638 | wake_up(&tcp_probe.wait); 639 | 640 | } 641 | 642 | spin_unlock(&tcp_hash_lock); 643 | 644 | skip: 645 | jprobe_return(); 646 | return 0; 647 | } 648 | 649 | static struct jprobe tcp_jprobe = { 650 | .kp = { 651 | .symbol_name = "tcp_rcv_established", 652 | }, 653 | .entry = (kprobe_opcode_t *) jtcp_rcv_established, 654 | }; 655 | 656 | 657 | static struct jprobe tcp_jprobe_done = { 658 | .kp = { 659 | .symbol_name = "tcp_done", 660 | }, 661 | .entry = (kprobe_opcode_t *) jtcp_done, 662 | }; 663 | 664 | 665 | static int tcpprobe_open(struct inode * inode, struct file * file) 666 | { 667 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,23) 668 | struct timespec ts; 669 | #endif 670 | 671 | /* Reset (empty) log */ 672 | spin_lock_bh(&tcp_probe.lock); 673 | tcp_probe.head = tcp_probe.tail = 0; 674 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 675 | getnstimeofday(&ts); 676 | tcp_probe.start = timespec_to_ktime(ts); 677 | #else 678 | tcp_probe.start = ktime_get(); 679 | #endif 680 | spin_unlock_bh(&tcp_probe.lock); 681 | 682 | return 0; 683 | } 684 | 685 | static int tcpprobe_sprint(char *tbuf, int n) 686 | { 687 | const struct tcp_log *p 688 | = tcp_probe.log + tcp_probe.tail; 689 | struct timespec tv 690 | = ktime_to_timespec(ktime_sub(p->tstamp, tcp_probe.start)); 691 | 692 | return scnprintf(tbuf, n, 693 | "%lu.%09lu %pI4:%u %pI4:%u %d %#llx %#x %u %u %u %u %u %u %u %u %u %u %u %u %#llx\n", 694 | (unsigned long) tv.tv_sec, 695 | (unsigned long) tv.tv_nsec, 696 | &p->saddr, ntohs(p->sport), 697 | &p->daddr, ntohs(p->dport), 698 | p->length, p->snd_nxt, p->snd_una, 699 | p->snd_cwnd, p->ssthresh, p->snd_wnd, p->srtt, 700 | p->rttvar, p->rto, p->lost, p->retrans, p->inflight, p->frto_counter, 701 | p->rqueue, p->wqueue, p->socket_idf); 702 | } 703 | 704 | static ssize_t tcpprobe_read(struct file *file, char __user *buf, 705 | size_t len, loff_t *ppos) 706 | { 707 | int error = 0; 708 | size_t cnt = 0; 709 | 710 | if (!buf) 711 | return -EINVAL; 712 | PRINT_TRACE("Page size is %lu. Buffer len is %zu.\n", PAGE_SIZE, len); 713 | 714 | while (cnt < len) { 715 | char tbuf[164]; 716 | int width; 717 | 718 | /* Wait for data in buffer */ 719 | error = wait_event_interruptible(tcp_probe.wait, 720 | tcp_probe_used() > 0); 721 | if (error) 722 | break; 723 | 724 | spin_lock_bh(&tcp_probe.lock); 725 | if (tcp_probe.head == tcp_probe.tail) { 726 | /* multiple readers race? */ 727 | TCPPROBE_STAT_INC(multiple_readers); 728 | spin_unlock_bh(&tcp_probe.lock); 729 | continue; 730 | } 731 | 732 | width = tcpprobe_sprint(tbuf, sizeof(tbuf)); 733 | 734 | if (cnt + width < len) { 735 | tcp_probe.tail = (tcp_probe.tail + 1) & (bufsize - 1); 736 | } 737 | 738 | spin_unlock_bh(&tcp_probe.lock); 739 | 740 | /* if record greater than space available 741 | return partial buffer (so far) */ 742 | if (cnt + width >= len) { 743 | break; 744 | } 745 | if (copy_to_user(buf + cnt, tbuf, width)) { 746 | TCPPROBE_STAT_INC(copy_error); 747 | return -EFAULT; 748 | } 749 | cnt += width; 750 | } 751 | 752 | return cnt == 0 ? error : cnt; 753 | } 754 | 755 | /* procfs statistics /proc/net/stat/tcpprobe */ 756 | static int tcpprobe_seq_show(struct seq_file *seq, void *v) 757 | { 758 | unsigned int nr_flows = atomic_read(&flow_count); 759 | struct tcpprobe_stat stat; 760 | int cpu; 761 | 762 | memset(&stat, 0, sizeof(struct tcpprobe_stat)); 763 | 764 | for_each_present_cpu(cpu) { 765 | struct tcpprobe_stat *cpu_stat = &per_cpu(tcpprobe_stat, cpu); 766 | 767 | stat.ack_drop_purge += cpu_stat->ack_drop_purge; 768 | stat.ack_drop_ring_full += cpu_stat->ack_drop_ring_full; 769 | stat.conn_maxflow_limit += cpu_stat->conn_maxflow_limit; 770 | stat.conn_memory_limit += cpu_stat->conn_memory_limit; 771 | stat.searched += cpu_stat->searched; 772 | stat.found += cpu_stat->found; 773 | stat.notfound += cpu_stat->notfound; 774 | stat.multiple_readers += cpu_stat->multiple_readers; 775 | stat.copy_error += cpu_stat->copy_error; 776 | stat.reset_flows += cpu_stat->reset_flows; 777 | } 778 | seq_printf(seq, "Flows: active %u mem %uK\n", nr_flows, 779 | (unsigned int)((nr_flows * sizeof(struct tcp_hash_flow)) >> 10)); 780 | seq_printf(seq, "Hash: size %u mem %uK\n", 781 | hashsize, (unsigned int)((hashsize * sizeof(struct hlist_head)) >> 10)); 782 | seq_printf(seq, "cpu# hash_stat: , ack_drop: , conn_drop: , err: \n"); 783 | seq_printf(seq, "Total: hash_stat: %6llu %6llu %6llu %6llu, ack_drop: %6llu %6llu, conn_drop: %6llu %6llu, err: %6llu %6llu\n", 784 | stat.searched, stat.found, stat.notfound, stat.reset_flows, 785 | stat.ack_drop_purge, stat.ack_drop_ring_full, 786 | stat.conn_maxflow_limit, stat.conn_memory_limit, 787 | stat.multiple_readers, stat.copy_error); 788 | if (num_present_cpus() > 1) { 789 | for_each_present_cpu(cpu) { 790 | struct tcpprobe_stat *cpu_stat = &per_cpu(tcpprobe_stat, cpu); 791 | seq_printf(seq, "cpu%u: hash_stat: %6llu %6llu %6llu %6llu, ack_drop: %6llu %6llu, conn_drop: %6llu %6llu, err: %6llu %6llu\n", 792 | cpu, 793 | cpu_stat->searched, cpu_stat->found, stat.notfound, stat.reset_flows, 794 | cpu_stat->ack_drop_purge, cpu_stat->ack_drop_ring_full, 795 | cpu_stat->conn_maxflow_limit, cpu_stat->conn_memory_limit, 796 | cpu_stat->multiple_readers, cpu_stat->copy_error); 797 | } 798 | } 799 | return 0; 800 | } 801 | 802 | static int tcpprobe_seq_open(struct inode *inode, struct file *file) 803 | { 804 | return single_open(file, tcpprobe_seq_show, NULL); 805 | } 806 | 807 | static struct ctl_table_header *tcpprobe_sysctl_header; 808 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,20) 809 | #define _CTL_NAME(x) .ctl_name = x, 810 | #else 811 | #define _CTL_NAME(x) 812 | #endif 813 | 814 | static struct ctl_table tcpprobe_sysctl_table[] = { 815 | { 816 | _CTL_NAME(1) 817 | .procname = "debug", 818 | .mode = 0644, 819 | .data = &debug, 820 | .maxlen = sizeof(int), 821 | .proc_handler = &proc_dointvec, 822 | }, 823 | { 824 | _CTL_NAME(2) 825 | .procname = "probetime", 826 | .mode = 0644, 827 | .data = &probetime, 828 | .maxlen = sizeof(int), 829 | .proc_handler = &proc_dointvec, 830 | }, 831 | { 832 | _CTL_NAME(3) 833 | .procname = "maxflows", 834 | .mode = 0644, 835 | .data = &maxflows, 836 | .maxlen = sizeof(int), 837 | .proc_handler = &proc_dointvec, 838 | }, 839 | { 840 | _CTL_NAME(4) 841 | .procname = "full", 842 | .mode = 0644, 843 | .data = &full, 844 | .maxlen = sizeof(int), 845 | .proc_handler = &proc_dointvec, 846 | }, 847 | { 848 | _CTL_NAME(5) 849 | .procname = "port", 850 | .mode = 0644, 851 | .data = &port, 852 | .maxlen = sizeof(int), 853 | .proc_handler = &proc_dointvec, 854 | }, 855 | { 856 | _CTL_NAME(6) 857 | .procname = "hashsize", 858 | .mode = 0444, /* readonly */ 859 | .data = &hashsize, 860 | .maxlen = sizeof(int), 861 | .proc_handler = &proc_dointvec, 862 | }, 863 | { 864 | _CTL_NAME(7) 865 | .procname = "bufsize", 866 | .mode = 0444, /* readonly */ 867 | .data = &bufsize, 868 | .maxlen = sizeof(int), 869 | .proc_handler = &proc_dointvec, 870 | }, 871 | { 872 | _CTL_NAME(8) 873 | .procname = "purge_time", 874 | .mode = 0644, 875 | .data = &purgetime, 876 | .maxlen = sizeof(int), 877 | .proc_handler = &proc_dointvec, 878 | }, 879 | {} 880 | }; 881 | 882 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) 883 | static struct ctl_table tcpprobe_sysctl_root[] = { 884 | { 885 | _CTL_NAME(33) 886 | .procname = PROC_SYSCTL_TCPPROBE, 887 | .mode = 0555, 888 | .child = tcpprobe_sysctl_table, 889 | }, 890 | { } 891 | }; 892 | 893 | static struct ctl_table tcpprobe_net_table[] = { 894 | { 895 | .ctl_name = CTL_NET, 896 | .procname = "net", 897 | .mode = 0555, 898 | .child = tcpprobe_sysctl_root, 899 | }, 900 | { } 901 | }; 902 | #else /* >= 2.6.25 */ 903 | static struct ctl_path tcpprobe_sysctl_path[] = { 904 | { 905 | .procname = "net", 906 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,33) 907 | .ctl_name = CTL_NET 908 | #endif 909 | }, 910 | { .procname = PROC_SYSCTL_TCPPROBE }, 911 | { } 912 | }; 913 | #endif /* 2.6.25 */ 914 | 915 | static const struct file_operations tcpprobe_fops = { 916 | .owner = THIS_MODULE, 917 | .open = tcpprobe_open, 918 | .read = tcpprobe_read, 919 | }; 920 | 921 | static const struct file_operations tcpprobe_stat_fops = { 922 | .owner = THIS_MODULE, 923 | .open = tcpprobe_seq_open, 924 | .read = seq_read, 925 | .llseek = seq_lseek, 926 | .release = single_release, 927 | }; 928 | 929 | static __init int tcpprobe_init(void) 930 | { 931 | int ret = -ENOMEM; 932 | struct proc_dir_entry *proc_stat; 933 | 934 | init_waitqueue_head(&tcp_probe.wait); 935 | spin_lock_init(&tcp_probe.lock); 936 | 937 | if (bufsize == 0) { 938 | pr_err("Bufsize is 0\n"); 939 | return -EINVAL; 940 | } 941 | 942 | /* Hashtable initialization */ 943 | get_random_bytes(&tcp_hash_rnd, 4); 944 | 945 | /* determine hash size (idea from nf_conntrack_core.c) */ 946 | if (!hashsize) { 947 | hashsize = (((totalram_pages << PAGE_SHIFT) / 16384) 948 | / sizeof(struct hlist_head)); 949 | if (totalram_pages > (1024 * 1024 * 1024 / PAGE_SIZE)) { 950 | hashsize = 16384; 951 | } 952 | } 953 | if (hashsize < 32) { 954 | hashsize = 32; 955 | } 956 | pr_info("Hashtable initialized with %u buckets\n", hashsize); 957 | 958 | tcp_hash_size = hashsize; 959 | tcp_hash = alloc_hashtable(tcp_hash_size); 960 | if (!tcp_hash) { 961 | pr_err("Unable to create tcp hashtable\n"); 962 | goto err; 963 | } 964 | tcp_flow_cachep = kmem_cache_create("tcp_flow", 965 | sizeof(struct tcp_hash_flow), 0, 0, NULL 966 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2, 6, 23) 967 | , NULL 968 | #endif 969 | ); 970 | if (!tcp_flow_cachep) { 971 | pr_err("Unable to create tcp_flow slab cache\n"); 972 | goto err_free_hash; 973 | } 974 | setup_timer(&purge_timer, purge_timer_run, 0); 975 | mod_timer(&purge_timer, jiffies + (HZ * purgetime)); 976 | 977 | 978 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,25) 979 | tcpprobe_sysctl_header = register_sysctl_table(tcpprobe_net_table 980 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,21) 981 | ,0 /* insert_at_head */ 982 | #endif 983 | ); 984 | #else /* 2.6.25 */ 985 | tcpprobe_sysctl_header = register_sysctl_paths(tcpprobe_sysctl_path, tcpprobe_sysctl_table); 986 | #endif 987 | if (!tcpprobe_sysctl_header) { 988 | pr_err("tcpprobe: can't register to sysctl\n"); 989 | goto err0; 990 | } else { 991 | pr_info("tcpprobe: registered: sysclt net.%s\n", PROC_SYSCTL_TCPPROBE); 992 | } 993 | 994 | //create_proc_entry has been deprecated by proc_create since 3.10 995 | proc_stat = proc_create(PROC_STAT_TCPPROBE, S_IRUGO, INIT_NET(proc_net_stat), &tcpprobe_stat_fops); 996 | 997 | if (!proc_stat) { 998 | pr_err("Unable to create /proc/net/stat/%s entry \n", PROC_STAT_TCPPROBE); 999 | goto err_free_sysctl; 1000 | } 1001 | 1002 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,30) 1003 | proc_stat->owner = THIS_MODULE; 1004 | #endif 1005 | pr_info("tcpprobe: registered: /proc/net/stat/%s\n", PROC_STAT_TCPPROBE); 1006 | 1007 | 1008 | bufsize = roundup_pow_of_two(bufsize); 1009 | tcp_probe.log = kcalloc(bufsize, sizeof(struct tcp_log), GFP_KERNEL); 1010 | if (!tcp_probe.log) { 1011 | pr_err("Unable to allocate tcp_log memory.\n"); 1012 | goto err_free_proc_stat; 1013 | } 1014 | 1015 | //proc_net_fops_create has been deprecated by proc_create since 3.10 1016 | if (!proc_create(PROC_TCPPROBE, S_IRUSR, INIT_NET(proc_net), &tcpprobe_fops)) { 1017 | pr_err("Unable to create /proc/net/tcpprobe\n"); 1018 | goto err_free_proc_stat; 1019 | } 1020 | 1021 | ret = register_jprobe(&tcp_jprobe); 1022 | if (ret) { 1023 | pr_err("Unable to register jprobe.\n"); 1024 | goto err1; 1025 | } 1026 | 1027 | 1028 | #if LINUX_VERSION_CODE < KERNEL_VERSION(2,6,22) 1029 | pr_info("Not registering jprobe on tcp_done as it is an inline method in this kernel version.\n"); 1030 | #else 1031 | ret = register_jprobe(&tcp_jprobe_done); 1032 | if (ret) { 1033 | pr_err("Unable to register jprobe on tcp_done.\n"); 1034 | goto err_tcpdone; 1035 | } 1036 | #endif 1037 | 1038 | pr_info("TCP probe registered (port=%d) bufsize=%u probetime=%d maxflows=%u\n", 1039 | port, bufsize, probetime, maxflows); 1040 | PRINT_DEBUG("Sizes tcp_hash_flow: %zu, hlist_head = %zu tcp_hash = %zu\n", 1041 | sizeof(struct tcp_hash_flow), sizeof(struct hlist_head), sizeof(tcp_hash)); 1042 | PRINT_DEBUG("Sizes hlist_node = %zu list_head = %zu, ktime_t = %zu tcp_tuple = %zu\n", 1043 | sizeof(struct hlist_node), sizeof(struct list_head), sizeof(ktime_t), sizeof(struct tcp_tuple)); 1044 | PRINT_DEBUG("Sizes tcp_log = %zu\n", sizeof (struct tcp_log)); 1045 | return 0; 1046 | err_tcpdone: 1047 | unregister_jprobe(&tcp_jprobe); 1048 | err1: 1049 | remove_proc_entry(PROC_TCPPROBE, INIT_NET(proc_net)); 1050 | err_free_proc_stat: 1051 | remove_proc_entry(PROC_STAT_TCPPROBE, INIT_NET(proc_net_stat)); 1052 | err_free_sysctl: 1053 | unregister_sysctl_table(tcpprobe_sysctl_header); 1054 | err0: 1055 | del_timer_sync(&purge_timer); 1056 | kfree(tcp_probe.log); 1057 | kmem_cache_destroy(tcp_flow_cachep); 1058 | err_free_hash: 1059 | vfree(tcp_hash); 1060 | err: 1061 | return ret; 1062 | } 1063 | module_init(tcpprobe_init); 1064 | 1065 | static __exit void tcpprobe_exit(void) 1066 | { 1067 | remove_proc_entry(PROC_TCPPROBE, INIT_NET(proc_net)); 1068 | remove_proc_entry(PROC_STAT_TCPPROBE, INIT_NET(proc_net_stat)); 1069 | unregister_sysctl_table(tcpprobe_sysctl_header); 1070 | unregister_jprobe(&tcp_jprobe); 1071 | 1072 | #if LINUX_VERSION_CODE >= KERNEL_VERSION(2,6,22) 1073 | unregister_jprobe(&tcp_jprobe_done); 1074 | #endif 1075 | 1076 | kfree(tcp_probe.log); 1077 | del_timer_sync(&purge_timer); 1078 | /* tcp flow table memory */ 1079 | purge_all_flows(); 1080 | kmem_cache_destroy(tcp_flow_cachep); 1081 | vfree(tcp_hash); 1082 | pr_info("TCP probe unregistered.\n"); 1083 | } 1084 | module_exit(tcpprobe_exit); 1085 | --------------------------------------------------------------------------------