From patchwork Tue Jun 27 11:48:14 2017 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Thomas Huth X-Patchwork-Id: 9811493 Return-Path: Received: from mail.wl.linuxfoundation.org (pdx-wl-mail.web.codeaurora.org [172.30.200.125]) by pdx-korg-patchwork.web.codeaurora.org (Postfix) with ESMTP id 09ACA60329 for ; Tue, 27 Jun 2017 11:50:49 +0000 (UTC) Received: from mail.wl.linuxfoundation.org (localhost [127.0.0.1]) by mail.wl.linuxfoundation.org (Postfix) with ESMTP id F1DE12861B for ; Tue, 27 Jun 2017 11:50:48 +0000 (UTC) Received: by mail.wl.linuxfoundation.org (Postfix, from userid 486) id E680828641; Tue, 27 Jun 2017 11:50:48 +0000 (UTC) X-Spam-Checker-Version: SpamAssassin 3.3.1 (2010-03-16) on pdx-wl-mail.web.codeaurora.org X-Spam-Level: X-Spam-Status: No, score=-6.4 required=2.0 tests=BAYES_00, RCVD_IN_DNSWL_HI, RCVD_IN_SORBS_SPAM autolearn=ham version=3.3.1 Received: from lists.gnu.org (lists.gnu.org [208.118.235.17]) (using TLSv1 with cipher AES256-SHA (256/256 bits)) (No client certificate requested) by mail.wl.linuxfoundation.org (Postfix) with ESMTPS id 1AD032861B for ; Tue, 27 Jun 2017 11:50:47 +0000 (UTC) Received: from localhost ([::1]:51843 helo=lists.gnu.org) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dPp14-0000YU-W2 for patchwork-qemu-devel@patchwork.kernel.org; Tue, 27 Jun 2017 07:50:47 -0400 Received: from eggs.gnu.org ([2001:4830:134:3::10]:54901) by lists.gnu.org with esmtp (Exim 4.71) (envelope-from ) id 1dPozD-0008A7-40 for qemu-devel@nongnu.org; Tue, 27 Jun 2017 07:48:56 -0400 Received: from Debian-exim by eggs.gnu.org with spam-scanned (Exim 4.71) (envelope-from ) id 1dPozB-0003sG-KT for qemu-devel@nongnu.org; Tue, 27 Jun 2017 07:48:51 -0400 Received: from mx1.redhat.com ([209.132.183.28]:46440) by eggs.gnu.org with esmtps (TLS1.0:DHE_RSA_AES_256_CBC_SHA1:32) (Exim 4.71) (envelope-from ) id 1dPozB-0003rn-C6 for qemu-devel@nongnu.org; Tue, 27 Jun 2017 07:48:49 -0400 Received: from smtp.corp.redhat.com (int-mx03.intmail.prod.int.phx2.redhat.com [10.5.11.13]) (using TLSv1.2 with cipher AECDH-AES256-SHA (256/256 bits)) (No client certificate requested) by mx1.redhat.com (Postfix) with ESMTPS id 4AC9F7F3E5; Tue, 27 Jun 2017 11:48:48 +0000 (UTC) DMARC-Filter: OpenDMARC Filter v1.3.2 mx1.redhat.com 4AC9F7F3E5 Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; dmarc=none (p=none dis=none) header.from=redhat.com Authentication-Results: ext-mx01.extmail.prod.ext.phx2.redhat.com; spf=pass smtp.mailfrom=thuth@redhat.com DKIM-Filter: OpenDKIM Filter v2.11.0 mx1.redhat.com 4AC9F7F3E5 Received: from thh440s.redhat.com (ovpn-116-48.ams2.redhat.com [10.36.116.48]) by smtp.corp.redhat.com (Postfix) with ESMTP id 676F418EEE; Tue, 27 Jun 2017 11:48:46 +0000 (UTC) From: Thomas Huth To: qemu-devel@nongnu.org, Christian Borntraeger Date: Tue, 27 Jun 2017 13:48:14 +0200 Message-Id: <1498564100-10045-9-git-send-email-thuth@redhat.com> In-Reply-To: <1498564100-10045-1-git-send-email-thuth@redhat.com> References: <1498564100-10045-1-git-send-email-thuth@redhat.com> X-Scanned-By: MIMEDefang 2.79 on 10.5.11.13 X-Greylist: Sender IP whitelisted, not delayed by milter-greylist-4.5.16 (mx1.redhat.com [10.5.110.25]); Tue, 27 Jun 2017 11:48:48 +0000 (UTC) X-detected-operating-system: by eggs.gnu.org: GNU/Linux 2.2.x-3.x [generic] [fuzzy] X-Received-From: 209.132.183.28 Subject: [Qemu-devel] [RFC PATCH 08/14] libnet: Refactor some code of netload() into a separate function X-BeenThere: qemu-devel@nongnu.org X-Mailman-Version: 2.1.21 Precedence: list List-Id: List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , Cc: Eric Farman , Farhan Ali , Alexander Graf , Jens Freimann , David Hildenbrand Errors-To: qemu-devel-bounces+patchwork-qemu-devel=patchwork.kernel.org@nongnu.org Sender: "Qemu-devel" X-Virus-Scanned: ClamAV using ClamSMTP netload() is a huge function, it's easy to lose track here. So let's refactor the TFTP-related loading and error printing code into a separate function instead. Signed-off-by: Thomas Huth --- pc-bios/s390-ccw/libnet/netload.c | 177 ++++++++++++++++++++------------------ 1 file changed, 93 insertions(+), 84 deletions(-) diff --git a/pc-bios/s390-ccw/libnet/netload.c b/pc-bios/s390-ccw/libnet/netload.c index 8afe341..f872884 100644 --- a/pc-bios/s390-ccw/libnet/netload.c +++ b/pc-bios/s390-ccw/libnet/netload.c @@ -403,13 +403,101 @@ static void seed_rng(uint8_t mac[]) srand(seed); } +static int tftp_load(filename_ip_t *fnip, unsigned char *buffer, int len, + unsigned int retries, int ip_vers) +{ + tftp_err_t tftp_err; + int rc; + + rc = tftp(fnip, buffer, len, retries, &tftp_err, 1, 1428, ip_vers); + + if (rc > 0) { + printf(" TFTP: Received %s (%d KBytes)\n", fnip->filename, + rc / 1024); + } else if (rc == -1) { + netload_error(0x3003, "unknown TFTP error"); + return -103; + } else if (rc == -2) { + netload_error(0x3004, "TFTP buffer of %d bytes " + "is too small for %s", + len, fnip->filename); + return -104; + } else if (rc == -3) { + netload_error(0x3009, "file not found: %s", + fnip->filename); + return -108; + } else if (rc == -4) { + netload_error(0x3010, "TFTP access violation"); + return -109; + } else if (rc == -5) { + netload_error(0x3011, "illegal TFTP operation"); + return -110; + } else if (rc == -6) { + netload_error(0x3012, "unknown TFTP transfer ID"); + return -111; + } else if (rc == -7) { + netload_error(0x3013, "no such TFTP user"); + return -112; + } else if (rc == -8) { + netload_error(0x3017, "TFTP blocksize negotiation failed"); + return -116; + } else if (rc == -9) { + netload_error(0x3018, "file exceeds maximum TFTP transfer size"); + return -117; + } else if (rc <= -10 && rc >= -15) { + char *icmp_err_str; + switch (rc) { + case -ICMP_NET_UNREACHABLE - 10: + icmp_err_str = "net unreachable"; + break; + case -ICMP_HOST_UNREACHABLE - 10: + icmp_err_str = "host unreachable"; + break; + case -ICMP_PROTOCOL_UNREACHABLE - 10: + icmp_err_str = "protocol unreachable"; + break; + case -ICMP_PORT_UNREACHABLE - 10: + icmp_err_str = "port unreachable"; + break; + case -ICMP_FRAGMENTATION_NEEDED - 10: + icmp_err_str = "fragmentation needed and DF set"; + break; + case -ICMP_SOURCE_ROUTE_FAILED - 10: + icmp_err_str = "source route failed"; + break; + default: + icmp_err_str = " UNKNOWN"; + break; + } + netload_error(0x3005, "ICMP ERROR \"%s\"", icmp_err_str); + return -105; + } else if (rc == -40) { + netload_error(0x3014, "TFTP error occurred after " + "%d bad packets received", + tftp_err.bad_tftp_packets); + return -113; + } else if (rc == -41) { + netload_error(0x3015, "TFTP error occurred after " + "missing %d responses", + tftp_err.no_packets); + return -114; + } else if (rc == -42) { + netload_error(0x3016, "TFTP error missing block %d, " + "expected block was %d", + tftp_err.blocks_missed, + tftp_err.blocks_received); + return -115; + } + + return rc; +} + int netload(char *buffer, int len, char *ret_buffer, int huge_load, int block_size, char *args_fs, int alen) { int rc; filename_ip_t fn_ip; int fd_device; - tftp_err_t tftp_err; obp_tftp_args_t obp_tftp_args; char null_ip[4] = { 0x00, 0x00, 0x00, 0x00 }; char null_ip6[16] = { 0x00, 0x00, 0x00, 0x00, @@ -633,94 +721,15 @@ int netload(char *buffer, int len, char *ret_buffer, int huge_load, printf("%s\n", ip6_str); } - // accept at most 20 bad packets - // wait at most for 40 packets - rc = tftp(&fn_ip, (unsigned char *) buffer, - len, obp_tftp_args.tftp_retries, - &tftp_err, huge_load, block_size, ip_version); + /* Do the TFTP load and print error message if necessary */ + rc = tftp_load(&fn_ip, (unsigned char *)buffer, len, + obp_tftp_args.tftp_retries, ip_version); - if(obp_tftp_args.ip_init == IP_INIT_DHCP) + if (obp_tftp_args.ip_init == IP_INIT_DHCP) dhcp_send_release(fn_ip.fd); close(fn_ip.fd); - if (rc > 0) { - printf(" TFTP: Received %s (%d KBytes)\n", fn_ip.filename, - rc / 1024); - } else if (rc == -1) { - netload_error(0x3003, "unknown TFTP error"); - return -103; - } else if (rc == -2) { - netload_error(0x3004, "TFTP buffer of %d bytes " - "is too small for %s", - len, fn_ip.filename); - return -104; - } else if (rc == -3) { - netload_error(0x3009, "file not found: %s", - fn_ip.filename); - return -108; - } else if (rc == -4) { - netload_error(0x3010, "TFTP access violation"); - return -109; - } else if (rc == -5) { - netload_error(0x3011, "illegal TFTP operation"); - return -110; - } else if (rc == -6) { - netload_error(0x3012, "unknown TFTP transfer ID"); - return -111; - } else if (rc == -7) { - netload_error(0x3013, "no such TFTP user"); - return -112; - } else if (rc == -8) { - netload_error(0x3017, "TFTP blocksize negotiation failed"); - return -116; - } else if (rc == -9) { - netload_error(0x3018, "file exceeds maximum TFTP transfer size"); - return -117; - } else if (rc <= -10 && rc >= -15) { - char *icmp_err_str; - switch (rc) { - case -ICMP_NET_UNREACHABLE - 10: - icmp_err_str = "net unreachable"; - break; - case -ICMP_HOST_UNREACHABLE - 10: - icmp_err_str = "host unreachable"; - break; - case -ICMP_PROTOCOL_UNREACHABLE - 10: - icmp_err_str = "protocol unreachable"; - break; - case -ICMP_PORT_UNREACHABLE - 10: - icmp_err_str = "port unreachable"; - break; - case -ICMP_FRAGMENTATION_NEEDED - 10: - icmp_err_str = "fragmentation needed and DF set"; - break; - case -ICMP_SOURCE_ROUTE_FAILED - 10: - icmp_err_str = "source route failed"; - break; - default: - icmp_err_str = " UNKNOWN"; - break; - } - netload_error(0x3005, "ICMP ERROR \"%s\"", icmp_err_str); - return -105; - } else if (rc == -40) { - netload_error(0x3014, "TFTP error occurred after " - "%d bad packets received", - tftp_err.bad_tftp_packets); - return -113; - } else if (rc == -41) { - netload_error(0x3015, "TFTP error occurred after " - "missing %d responses", - tftp_err.no_packets); - return -114; - } else if (rc == -42) { - netload_error(0x3016, "TFTP error missing block %d, " - "expected block was %d", - tftp_err.blocks_missed, - tftp_err.blocks_received); - return -115; - } return rc; }