From patchwork Sun Oct 24 01:14:20 2010 Content-Type: text/plain; charset="utf-8" MIME-Version: 1.0 Content-Transfer-Encoding: 7bit X-Patchwork-Submitter: Marek Lindner X-Patchwork-Id: 456 Return-Path: Received: from nm19-vm0.bullet.mail.ukl.yahoo.com (nm19-vm0.bullet.mail.ukl.yahoo.com [217.146.183.113]) by open-mesh.org (Postfix) with SMTP id B73D01545DF for ; Sun, 24 Oct 2010 03:16:31 +0200 (CEST) Received: from [217.146.183.214] by nm19.bullet.mail.ukl.yahoo.com with NNFMP; 24 Oct 2010 01:16:31 -0000 Received: from [217.146.183.34] by tm7.bullet.mail.ukl.yahoo.com with NNFMP; 24 Oct 2010 01:16:30 -0000 Received: from [127.0.0.1] by omp1023.mail.ukl.yahoo.com with NNFMP; 24 Oct 2010 01:16:30 -0000 X-Yahoo-Newman-Id: 611914.63872.bm@omp1023.mail.ukl.yahoo.com Received: (qmail 28293 invoked from network); 24 Oct 2010 01:16:30 -0000 DomainKey-Signature: a=rsa-sha1; q=dns; c=nofws; s=s1024; d=yahoo.de; h=DKIM-Signature:Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References; b=OHYKiFtTZmLtgE3IMJBh7fYCM+EnlPk4pJ4b2DrLonRqGOrUGQQAGs8eyyt8dzgSKPlW5Rc3OghSxQW5K1iLE/8iAOFDLV2LnrjOkqJZcrTOEhetSIoJdSiRlMqBr1VEKhlMIlJn1fkWBotAWTH7Lk/t9QTxLQ1bo6SBW/gEmYw= ; DKIM-Signature: v=1; a=rsa-sha256; c=relaxed/relaxed; d=yahoo.de; s=s1024; t=1287882990; bh=VOOx8S4HZ/53EyCqUi12kuYwg72YjNYGyxH9wEvhOcE=; h=Received:X-Yahoo-SMTP:X-YMail-OSG:X-Yahoo-Newman-Property:From:To:Cc:Subject:Date:Message-Id:X-Mailer:In-Reply-To:References; b=FSDcrFaSPzfDNoTr/2rg/CBNszYBfEZhOQRMntOyQFb0n9nzbf041szWG0aUhRGYT9WUMiQdcW0GNgxSHvdxr6QqNEWAD05cw6HgG8bfYRqzjepjQKmrqQk0ZaHQGCgI9EaoHx99QoZblOKnziS76zlRe73EOTwmS97u69hBmYE= Received: from localhost (lindner_marek@78.225.40.81 with plain) by smtp126.mail.ukl.yahoo.com with SMTP; 23 Oct 2010 18:16:29 -0700 PDT X-Yahoo-SMTP: tW.h3tiswBBMXO2coYcbPigGD5Lt6zY_.Zc- X-YMail-OSG: myf_tuQVM1mbSQI1WRHBw_NiYsj_psNtKN4JQP8v1E9HJEN V5hky3ZUSvgUOm6h0vOQykf89NQBiCZrYXve6H0MNa9Eqj2z0QwBOJDmkqFM Pcwk7ZgJH_IbHU3cihe1N_SOlclD6.T.ts4zQuBZF28X9fRYahaD_D7vhrvG 9I6e3gdvOYO21zkWLlEM5d.X4A2F3QPKX7buxKa3f5kEsQ_wDJVmhBibzyzG BIqELuY_TgQXKfCRaOeq5qFUqW54sUqM3mM82PAQeEkxRq4Mz X-Yahoo-Newman-Property: ymail-3 From: Marek Lindner To: b.a.t.m.a.n@lists.open-mesh.org Date: Sun, 24 Oct 2010 03:14:20 +0200 Message-Id: <1287882863-11314-5-git-send-email-lindner_marek@yahoo.de> X-Mailer: git-send-email 1.7.1 In-Reply-To: <201010240313.35215.lindner_marek@yahoo.de> References: <201010240313.35215.lindner_marek@yahoo.de> Cc: Marek Lindner Subject: [B.A.T.M.A.N.] [PATCH 5/8] batman-adv: cleanup gw mode sysfs file to only accept one value X-BeenThere: b.a.t.m.a.n@lists.open-mesh.org X-Mailman-Version: 2.1.11 Precedence: list Reply-To: The list for a Better Approach To Mobile Ad-hoc Networking List-Id: The list for a Better Approach To Mobile Ad-hoc Networking List-Unsubscribe: , List-Archive: List-Post: List-Help: List-Subscribe: , X-List-Received-Date: Sun, 24 Oct 2010 01:16:32 -0000 The Linux kernel guidelines require each sysfs file to have a single value / purpose, therefore it is necessary to split up the sysfs gateway files. Signed-off-by: Marek Lindner --- batman-adv/bat_sysfs.c | 78 ++++++++++++---- batman-adv/gateway_common.c | 214 ------------------------------------------- batman-adv/gateway_common.h | 1 - 3 files changed, 59 insertions(+), 234 deletions(-) diff --git a/batman-adv/bat_sysfs.c b/batman-adv/bat_sysfs.c index 436b8da..bac2ff0 100644 --- a/batman-adv/bat_sysfs.c +++ b/batman-adv/bat_sysfs.c @@ -262,30 +262,17 @@ static ssize_t show_gw_mode(struct kobject *kobj, struct attribute *attr, char *buff) { struct bat_priv *bat_priv = kobj_to_batpriv(kobj); - int down, up, bytes_written; - int gw_class, gw_mode = atomic_read(&bat_priv->gw_mode); + int bytes_written; - switch (gw_mode) { + switch (atomic_read(&bat_priv->gw_mode)) { case GW_MODE_CLIENT: - gw_class = atomic_read(&bat_priv->gw_sel_class); - bytes_written = sprintf(buff, "%s (gw_class: %i)\n", - GW_MODE_CLIENT_NAME, gw_class); + bytes_written = sprintf(buff, "%s\n", GW_MODE_CLIENT_NAME); break; case GW_MODE_SERVER: - gw_class = atomic_read(&bat_priv->gw_bandwidth); - gw_bandwidth_to_kbit(gw_class, &down, &up); - bytes_written = sprintf(buff, - "%s (gw_class: %i " - "-> propagating: %i%s/%i%s)\n", - GW_MODE_SERVER_NAME, gw_class, - (down > 2048 ? down / 1024 : down), - (down > 2048 ? "MBit" : "KBit"), - (up > 2048 ? up / 1024 : up), - (up > 2048 ? "MBit" : "KBit")); + bytes_written = sprintf(buff, "%s\n", GW_MODE_SERVER_NAME); break; default: - bytes_written = sprintf(buff, "%s\n", - GW_MODE_OFF_NAME); + bytes_written = sprintf(buff, "%s\n", GW_MODE_OFF_NAME); break; } @@ -296,7 +283,60 @@ static ssize_t store_gw_mode(struct kobject *kobj, struct attribute *attr, char *buff, size_t count) { struct net_device *net_dev = kobj_to_netdev(kobj); - return gw_mode_set(net_dev, buff, count); + struct bat_priv *bat_priv = netdev_priv(net_dev); + char *curr_gw_mode_str; + int gw_mode_tmp = -1; + + if (buff[count - 1] == '\n') + buff[count - 1] = '\0'; + + if (strncmp(buff, GW_MODE_OFF_NAME, strlen(GW_MODE_OFF_NAME)) == 0) + gw_mode_tmp = GW_MODE_OFF; + + if (strncmp(buff, GW_MODE_CLIENT_NAME, + strlen(GW_MODE_CLIENT_NAME)) == 0) + gw_mode_tmp = GW_MODE_CLIENT; + + if (strncmp(buff, GW_MODE_SERVER_NAME, + strlen(GW_MODE_SERVER_NAME)) == 0) + gw_mode_tmp = GW_MODE_SERVER; + + if (gw_mode_tmp < 0) { + bat_info(net_dev, + "Invalid parameter for 'gw mode' setting received: " + "%s\n", buff); + return -EINVAL; + } + + if (atomic_read(&bat_priv->gw_mode) == gw_mode_tmp) + return count; + + switch (atomic_read(&bat_priv->gw_mode)) { + case GW_MODE_CLIENT: + curr_gw_mode_str = GW_MODE_CLIENT_NAME; + break; + case GW_MODE_SERVER: + curr_gw_mode_str = GW_MODE_SERVER_NAME; + break; + default: + curr_gw_mode_str = GW_MODE_OFF_NAME; + break; + } + + bat_info(net_dev, "Changing gw mode from: %s to: %s\n", + curr_gw_mode_str, buff); + + if ((gw_mode_tmp == GW_MODE_CLIENT) && + (atomic_read(&bat_priv->gw_sel_class) == 0)) + atomic_set(&bat_priv->gw_sel_class, 20); + + if ((gw_mode_tmp == GW_MODE_SERVER) && + (atomic_read(&bat_priv->gw_bandwidth) == 0)) + atomic_set(&bat_priv->gw_bandwidth, 41); + + gw_deselect(bat_priv); + atomic_set(&bat_priv->gw_mode, (unsigned)gw_mode_tmp); + return count; } static ssize_t show_gw_bwidth(struct kobject *kobj, struct attribute *attr, diff --git a/batman-adv/gateway_common.c b/batman-adv/gateway_common.c index fc197be..c71420d 100644 --- a/batman-adv/gateway_common.c +++ b/batman-adv/gateway_common.c @@ -76,220 +76,6 @@ void gw_bandwidth_to_kbit(uint8_t gw_srv_class, int *down, int *up) *up = ((upart + 1) * (*down)) / 8; } -static bool parse_gw_mode_tok(struct net_device *net_dev, - char *tokptr, long *gw_mode_tmp, - char **gw_mode_tmp_str, long *gw_class_tmp, - long *up, long *down) -{ - int ret, multi; - char *slash_ptr, *tmp_ptr; - - switch (*gw_mode_tmp) { - case GW_MODE_CLIENT: - ret = strict_strtoul(tokptr, 10, gw_class_tmp); - if (ret) { - bat_err(net_dev, "Client class of gateway mode invalid:" - " %s\n", tokptr); - return false; - } - - if (*gw_class_tmp > TQ_MAX_VALUE) { - bat_err(net_dev, - "Client class of gateway mode greater than %i: " - "%ld\n", TQ_MAX_VALUE, *gw_class_tmp); - return false; - } - - break; - case GW_MODE_SERVER: - slash_ptr = strchr(tokptr, '/'); - if (slash_ptr) - *slash_ptr = 0; - - multi = 1; - - if (strlen(tokptr) > 4) { - tmp_ptr = tokptr + strlen(tokptr) - 4; - - if (strnicmp(tmp_ptr, "mbit", 4) == 0) - multi = 1024; - - if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || - (multi > 1)) - *tmp_ptr = '\0'; - } - - ret = strict_strtoul(tokptr, 10, down); - if (ret) { - bat_err(net_dev, - "Download speed of gateway mode invalid: %s\n", - tokptr); - return false; - } - - *down *= multi; - - /* we also got some upload info */ - if (slash_ptr) { - multi = 1; - - if (strlen(slash_ptr + 1) > 4) { - tmp_ptr = slash_ptr + 1 - 4 - + strlen(slash_ptr + 1); - - if (strnicmp(tmp_ptr, "mbit", 4) == 0) - multi = 1024; - - if ((strnicmp(tmp_ptr, "kbit", 4) == 0) || - (multi > 1)) - *tmp_ptr = '\0'; - } - - ret = strict_strtoul(slash_ptr + 1, 10, up); - if (ret) { - bat_err(net_dev, - "Upload speed of gateway mode invalid: " - "%s\n", slash_ptr + 1); - return false; - } - - *up *= multi; - } - - break; - default: - if (strcmp(tokptr, GW_MODE_OFF_NAME) == 0) { - *gw_mode_tmp = GW_MODE_OFF; - *gw_mode_tmp_str = GW_MODE_OFF_NAME; - } - - if (strcmp(tokptr, GW_MODE_CLIENT_NAME) == 0) { - *gw_mode_tmp = GW_MODE_CLIENT; - *gw_mode_tmp_str = GW_MODE_CLIENT_NAME; - } - - if (strcmp(tokptr, GW_MODE_SERVER_NAME) == 0) { - *gw_mode_tmp = GW_MODE_SERVER; - *gw_mode_tmp_str = GW_MODE_SERVER_NAME; - } - } - - return true; -} - -ssize_t gw_mode_set(struct net_device *net_dev, char *buff, size_t count) -{ - char *tokptr, *cp, finished; - char *gw_mode_curr_str, *gw_mode_tmp_str = NULL; - long gw_mode_curr, gw_mode_tmp = GW_MODE_OFF; - long gw_class_tmp = 0, up = 0, down = 0; - bool ret; - struct bat_priv *bat_priv = netdev_priv(net_dev); - - tokptr = buff; - gw_mode_curr = atomic_read(&bat_priv->gw_mode); - - for (cp = buff, finished = 0; !finished; cp++) { - switch (*cp) { - case 0: - finished = 1; - case ' ': - case '\n': - case '\t': - *cp = 0; - - if (strlen(tokptr) == 0) - goto next; - - ret = parse_gw_mode_tok(net_dev, tokptr, &gw_mode_tmp, - &gw_mode_tmp_str, &gw_class_tmp, - &up, &down); - - if (!ret) - goto end; - -next: - tokptr = cp + 1; - break; - default: - break; - } - } - - if (!gw_mode_tmp_str) { - bat_info(net_dev, "Gateway mode can only be set to: '%s', '%s' " - "or '%s' - given value: %s\n", GW_MODE_OFF_NAME, - GW_MODE_CLIENT_NAME, GW_MODE_SERVER_NAME, buff); - goto end; - } - - switch (gw_mode_curr) { - case GW_MODE_CLIENT: - gw_mode_curr_str = GW_MODE_CLIENT_NAME; - break; - case GW_MODE_SERVER: - gw_mode_curr_str = GW_MODE_SERVER_NAME; - break; - default: - gw_mode_curr_str = GW_MODE_OFF_NAME; - break; - } - - switch (gw_mode_tmp) { - case GW_MODE_CLIENT: - gw_class_tmp = atomic_read(&bat_priv->gw_sel_class); - if ((gw_mode_tmp == GW_MODE_CLIENT) && (!gw_class_tmp)) - gw_class_tmp = 20; - - bat_info(net_dev, "Changing gateway mode from: '%s' to: '%s' " - "(gw_sel_class: %ld)\n", - gw_mode_curr_str, gw_mode_tmp_str, gw_class_tmp); - - atomic_set(&bat_priv->gw_sel_class, gw_class_tmp); - break; - case GW_MODE_SERVER: - if (!down) - down = 2000; - - if (!up) - up = down / 5; - - kbit_to_gw_bandwidth(down, up, &gw_class_tmp); - - /** - * the gw class we guessed above might not match the given - * speeds, hence we need to calculate it back to show the - * number that is going to be propagated - **/ - gw_bandwidth_to_kbit((uint8_t)gw_class_tmp, - (int *)&down, (int *)&up); - - gw_deselect(bat_priv); - bat_info(net_dev, "Changing gateway mode from: '%s' to: '%s' " - "(gw_class: %ld -> propagating: %ld%s/%ld%s)\n", - gw_mode_curr_str, gw_mode_tmp_str, gw_class_tmp, - (down > 2048 ? down / 1024 : down), - (down > 2048 ? "MBit" : "KBit"), - (up > 2048 ? up / 1024 : up), - (up > 2048 ? "MBit" : "KBit")); - - atomic_set(&bat_priv->gw_bandwidth, gw_class_tmp); - break; - default: - bat_info(net_dev, "Changing gateway mode from: '%s' to: '%s'\n", - gw_mode_curr_str, gw_mode_tmp_str); - break; - } - - atomic_set(&bat_priv->gw_mode, gw_mode_tmp); - - if (gw_class_tmp == 0) - gw_deselect(bat_priv); - -end: - return count; -} - static bool parse_gw_bandwidth(struct net_device *net_dev, char *buff, long *up, long *down) { diff --git a/batman-adv/gateway_common.h b/batman-adv/gateway_common.h index 181a306..5e728d0 100644 --- a/batman-adv/gateway_common.h +++ b/batman-adv/gateway_common.h @@ -33,7 +33,6 @@ enum gw_modes { #define GW_MODE_SERVER_NAME "server" void gw_bandwidth_to_kbit(uint8_t gw_class, int *down, int *up); -ssize_t gw_mode_set(struct net_device *net_dev, char *buff, size_t count); ssize_t gw_bandwidth_set(struct net_device *net_dev, char *buff, size_t count); #endif /* _NET_BATMAN_ADV_GATEWAY_COMMON_H_ */