diff options
author | Tom Gundersen <teg@jklm.no> | 2014-03-30 20:36:44 +0200 |
---|---|---|
committer | Tom Gundersen <teg@jklm.no> | 2014-03-30 20:46:40 +0200 |
commit | 2ad7561f9f658f8dee168a76654c7d918e2260c7 (patch) | |
tree | 23f5067cf37d0c80eddbcd01ffbd7393026aee99 | |
parent | 6e34949d7207f9dff4e2b01a3037a0af88e1c25c (diff) |
sd-dhcp: avoid checksum calculation if possible
When receiving lots of packets that are not meant for us, we waste a relatively large amount
of cpu time computing their checksums before discarding them. Move the checksum calculation last
so we never compute it for packets which would otherwise be discarded.
-rw-r--r-- | src/libsystemd-network/dhcp-packet.c | 28 |
1 files changed, 16 insertions, 12 deletions
diff --git a/src/libsystemd-network/dhcp-packet.c b/src/libsystemd-network/dhcp-packet.c index 3b62c2572f..4f90c283a2 100644 --- a/src/libsystemd-network/dhcp-packet.c +++ b/src/libsystemd-network/dhcp-packet.c @@ -155,11 +155,6 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) { return -EINVAL; } - if (dhcp_packet_checksum(&packet->ip, hdrlen)) { - log_dhcp_client(client, "ignoring packet: invalid IP checksum"); - return -EINVAL; - } - /* UDP */ if (packet->ip.protocol != IPPROTO_UDP) { @@ -181,6 +176,22 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) { return -EINVAL; } + if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) { + log_dhcp_client(client, "ignoring packet: to port %u, which " + "is not the DHCP client port (%u)", + be16toh(packet->udp.dest), DHCP_PORT_CLIENT); + return -EINVAL; + } + + /* checksums - computing these is relatively expensive, so only do it + if all the other checks have passed + */ + + if (dhcp_packet_checksum(&packet->ip, hdrlen)) { + log_dhcp_client(client, "ignoring packet: invalid IP checksum"); + return -EINVAL; + } + if (checksum && packet->udp.check) { packet->ip.check = packet->udp.len; packet->ip.ttl = 0; @@ -192,12 +203,5 @@ int dhcp_packet_verify_headers(DHCPPacket *packet, size_t len, bool checksum) { } } - if (be16toh(packet->udp.dest) != DHCP_PORT_CLIENT) { - log_dhcp_client(client, "ignoring packet: to port %u, which " - "is not the DHCP client port (%u)", - be16toh(packet->udp.dest), DHCP_PORT_CLIENT); - return -EINVAL; - } - return 0; } |