netdev.vger.kernel.org archive mirror
 help / color / mirror / Atom feed
From: Ben Hutchings <bhutchings@solarflare.com>
To: <netdev@vger.kernel.org>
Cc: <linux-net-drivers@solarflare.com>,
	Alexander Duyck <alexander.h.duyck@intel.com>
Subject: [PATCH ethtool 18/21] rxclass: Replace global rmgr with automatic variable/parameter
Date: Tue, 1 Nov 2011 23:22:00 +0000	[thread overview]
Message-ID: <1320189720.2758.49.camel@bwh-desktop> (raw)
In-Reply-To: <1320186901.2758.30.camel@bwh-desktop>

Signed-off-by: Ben Hutchings <bhutchings@solarflare.com>
---
 rxclass.c |   78 ++++++++++++++++++++++++++----------------------------------
 1 files changed, 34 insertions(+), 44 deletions(-)

diff --git a/rxclass.c b/rxclass.c
index 0c7b916..3b23a33 100644
--- a/rxclass.c
+++ b/rxclass.c
@@ -299,30 +299,28 @@ struct rmgr_ctrl {
 	__u32			size;
 };
 
-static struct rmgr_ctrl rmgr;
-static int rmgr_init_done = 0;
-
-static int rmgr_ins(__u32 loc)
+static int rmgr_ins(struct rmgr_ctrl *rmgr, __u32 loc)
 {
 	/* verify location is in rule manager range */
-	if (loc >= rmgr.size) {
+	if (loc >= rmgr->size) {
 		fprintf(stderr, "rmgr: Location out of range\n");
 		return -1;
 	}
 
 	/* set bit for the rule */
-	set_bit(loc, rmgr.slot);
+	set_bit(loc, rmgr->slot);
 
 	return 0;
 }
 
-static int rmgr_find_empty_slot(struct ethtool_rx_flow_spec *fsp)
+static int rmgr_find_empty_slot(struct rmgr_ctrl *rmgr,
+				struct ethtool_rx_flow_spec *fsp)
 {
 	__u32 loc;
 	__u32 slot_num;
 
 	/* start at the end of the list since it is lowest priority */
-	loc = rmgr.size - 1;
+	loc = rmgr->size - 1;
 
 	/* locate the first slot a rule can be placed in */
 	slot_num = loc / BITS_PER_LONG;
@@ -333,10 +331,10 @@ static int rmgr_find_empty_slot(struct ethtool_rx_flow_spec *fsp)
 	 * moving 1 + loc % BITS_PER_LONG we align ourselves to the last bit
 	 * in the previous word.
 	 *
-	 * If loc rolls over it should be greater than or equal to rmgr.size
+	 * If loc rolls over it should be greater than or equal to rmgr->size
 	 * and as such we know we have reached the end of the list.
 	 */
-	if (!~(rmgr.slot[slot_num] | (~1UL << rmgr.size % BITS_PER_LONG))) {
+	if (!~(rmgr->slot[slot_num] | (~1UL << rmgr->size % BITS_PER_LONG))) {
 		loc -= 1 + (loc % BITS_PER_LONG);
 		slot_num--;
 	}
@@ -345,7 +343,7 @@ static int rmgr_find_empty_slot(struct ethtool_rx_flow_spec *fsp)
 	 * Now that we are aligned with the last bit in each long we can just
 	 * go though and eliminate all the longs with no free bits
 	 */
-	while (loc < rmgr.size && !~(rmgr.slot[slot_num])) {
+	while (loc < rmgr->size && !~(rmgr->slot[slot_num])) {
 		loc -= BITS_PER_LONG;
 		slot_num--;
 	}
@@ -354,13 +352,13 @@ static int rmgr_find_empty_slot(struct ethtool_rx_flow_spec *fsp)
 	 * If we still are inside the range, test individual bits as one is
 	 * likely available for our use.
 	 */
-	while (loc < rmgr.size && test_bit(loc, rmgr.slot))
+	while (loc < rmgr->size && test_bit(loc, rmgr->slot))
 		loc--;
 
 	/* location found, insert rule */
-	if (loc < rmgr.size) {
+	if (loc < rmgr->size) {
 		fsp->location = loc;
-		return rmgr_ins(loc);
+		return rmgr_ins(rmgr, loc);
 	}
 
 	/* No space to add this rule */
@@ -369,25 +367,22 @@ static int rmgr_find_empty_slot(struct ethtool_rx_flow_spec *fsp)
 	return -1;
 }
 
-static int rmgr_init(struct cmd_context *ctx)
+static int rmgr_init(struct cmd_context *ctx, struct rmgr_ctrl *rmgr)
 {
 	struct ethtool_rxnfc *nfccmd;
 	int err, i;
 	__u32 *rule_locs;
 
-	if (rmgr_init_done)
-		return 0;
-
 	/* clear rule manager settings */
-	memset(&rmgr, 0, sizeof(struct rmgr_ctrl));
+	memset(rmgr, 0, sizeof(*rmgr));
 
-	/* request count and store in rmgr.n_rules */
-	err = rxclass_get_count(ctx, &rmgr.n_rules);
+	/* request count and store in rmgr->n_rules */
+	err = rxclass_get_count(ctx, &rmgr->n_rules);
 	if (err < 0)
 		return err;
 
 	/* alloc memory for request of location list */
-	nfccmd = calloc(1, sizeof(*nfccmd) + (rmgr.n_rules * sizeof(__u32)));
+	nfccmd = calloc(1, sizeof(*nfccmd) + (rmgr->n_rules * sizeof(__u32)));
 	if (!nfccmd) {
 		perror("rmgr: Cannot allocate memory for"
 		       " RX class rule locations");
@@ -396,7 +391,7 @@ static int rmgr_init(struct cmd_context *ctx)
 
 	/* request location list */
 	nfccmd->cmd = ETHTOOL_GRXCLSRLALL;
-	nfccmd->rule_cnt = rmgr.n_rules;
+	nfccmd->rule_cnt = rmgr->n_rules;
 	err = send_ioctl(ctx, nfccmd);
 	if (err < 0) {
 		perror("rmgr: Cannot get RX class rules");
@@ -405,61 +400,56 @@ static int rmgr_init(struct cmd_context *ctx)
 	}
 
 	/* make certain the table size is valid */
-	rmgr.size = nfccmd->data;
-	if (rmgr.size == 0 || rmgr.size < rmgr.n_rules) {
+	rmgr->size = nfccmd->data;
+	if (rmgr->size == 0 || rmgr->size < rmgr->n_rules) {
 		perror("rmgr: Invalid RX class rules table size");
 		return -1;
 	}
 
 	/* initialize bitmap for storage of valid locations */
-	rmgr.slot = calloc(1, BITS_TO_LONGS(rmgr.size) * sizeof(long));
-	if (!rmgr.slot) {
+	rmgr->slot = calloc(1, BITS_TO_LONGS(rmgr->size) * sizeof(long));
+	if (!rmgr->slot) {
 		perror("rmgr: Cannot allocate memory for RX class rules");
 		return -1;
 	}
 
 	/* write locations to bitmap */
 	rule_locs = nfccmd->rule_locs;
-	for (i = 0; i < rmgr.n_rules; i++) {
-		err = rmgr_ins(rule_locs[i]);
+	for (i = 0; i < rmgr->n_rules; i++) {
+		err = rmgr_ins(rmgr, rule_locs[i]);
 		if (err < 0)
 			break;
 	}
 
-	/* free memory and set flag to avoid reinit */
 	free(nfccmd);
-	rmgr_init_done = 1;
 
 	return err;
 }
 
-static void rmgr_cleanup(void)
+static void rmgr_cleanup(struct rmgr_ctrl *rmgr)
 {
-	if (!rmgr_init_done)
-		return;
-
-	rmgr_init_done = 0;
-
-	free(rmgr.slot);
-	rmgr.slot = NULL;
-	rmgr.size = 0;
+	free(rmgr->slot);
+	rmgr->slot = NULL;
+	rmgr->size = 0;
 }
 
 static int rmgr_set_location(struct cmd_context *ctx,
 			     struct ethtool_rx_flow_spec *fsp)
 {
+	struct rmgr_ctrl rmgr;
 	int err;
 
 	/* init table of available rules */
-	err = rmgr_init(ctx);
+	err = rmgr_init(ctx, &rmgr);
 	if (err < 0)
-		return err;
+		goto out;
 
 	/* verify rule location */
-	err = rmgr_find_empty_slot(fsp);
+	err = rmgr_find_empty_slot(&rmgr, fsp);
 
+out:
 	/* cleanup table and free resources */
-	rmgr_cleanup();
+	rmgr_cleanup(&rmgr);
 
 	return err;
 }
-- 
1.7.4.4



-- 
Ben Hutchings, Staff Engineer, Solarflare
Not speaking for my employer; that's the marketing department's job.
They asked us to note that Solarflare product names are trademarked.

  parent reply	other threads:[~2011-11-01 23:22 UTC|newest]

Thread overview: 24+ messages / expand[flat|nested]  mbox.gz  Atom feed  top
2011-11-01 22:35 [PATCH ethtool 00/21] ethtool refactoring and misc changes Ben Hutchings
2011-11-01 23:13 ` [PATCH ethtool 01/21] Report pause frame autonegotiation result Ben Hutchings
2011-11-01 23:13 ` [PATCH ethtool 02/21] ethtool.8: Fix initial blank line/page Ben Hutchings
2011-11-01 23:14 ` [PATCH ethtool 03/21] Combine ethtool-{bitops,util}.h into internal.h Ben Hutchings
2011-11-01 23:14 ` [PATCH ethtool 04/21] Fix type of bit-number parameter to set_bit() and clear_bit() Ben Hutchings
2011-11-01 23:15 ` [PATCH ethtool 05/21] ethtool.8: Change device name metavariable from 'ethX' to 'devname' Ben Hutchings
2011-11-01 23:15 ` [PATCH ethtool 06/21] ethtool.8: Allow line-break in description of parameters after -N Ben Hutchings
2011-11-01 23:15 ` [PATCH ethtool 07/21] Fix format of help text for -f option Ben Hutchings
2011-11-01 23:15 ` [PATCH ethtool 08/21] Use standard indentation for definition of struct option args Ben Hutchings
2011-11-01 23:16 ` [PATCH ethtool 09/21] Encapsulate command context in a structure Ben Hutchings
2011-11-01 23:17 ` [PATCH ethtool 10/21] Add test cases for command-line parsing Ben Hutchings
2011-11-01 23:17 ` [PATCH ethtool 11/21] Add more " Ben Hutchings
2011-11-01 23:18 ` [PATCH ethtool 12/21] Move argument parsing to sub-command functions Ben Hutchings
2011-11-01 23:18 ` [PATCH ethtool 13/21] Support arbitrary numbers of option names for each mode Ben Hutchings
2011-11-01 23:18 ` [PATCH ethtool 14/21] Fix reference to cmdline_ring in do_schannels() Ben Hutchings
2011-11-01 23:19 ` [PATCH ethtool 15/21] Convert cmdline_msglvl into array of named flags; convert back at run-time Ben Hutchings
2011-11-01 23:20 ` [PATCH ethtool 16/21] Replace global devname variable with a field in struct cmd_context Ben Hutchings
2011-11-01 23:21 ` [PATCH ethtool 17/21] Change most static global variables into automatic variables Ben Hutchings
2011-11-01 23:22 ` Ben Hutchings [this message]
2011-11-01 23:22 ` [PATCH ethtool 19/21] Declare static variables const as appropriate Ben Hutchings
2011-11-01 23:23 ` [PATCH ethtool 20/21] Run tests in-process Ben Hutchings
2011-11-02 20:25   ` Ben Hutchings
2011-11-01 23:24 ` [PATCH ethtool 21/21] Rearrange definitions and remove unnecessary forward declarations Ben Hutchings
2011-11-03 19:17 ` [PATCH ethtool 00/21] ethtool refactoring and misc changes Ben Hutchings

Reply instructions:

You may reply publicly to this message via plain-text email
using any one of the following methods:

* Save the following mbox file, import it into your mail client,
  and reply-to-all from there: mbox

  Avoid top-posting and favor interleaved quoting:
  https://en.wikipedia.org/wiki/Posting_style#Interleaved_style

* Reply using the --to, --cc, and --in-reply-to
  switches of git-send-email(1):

  git send-email \
    --in-reply-to=1320189720.2758.49.camel@bwh-desktop \
    --to=bhutchings@solarflare.com \
    --cc=alexander.h.duyck@intel.com \
    --cc=linux-net-drivers@solarflare.com \
    --cc=netdev@vger.kernel.org \
    /path/to/YOUR_REPLY

  https://kernel.org/pub/software/scm/git/docs/git-send-email.html

* If your mail client supports setting the In-Reply-To header
  via mailto: links, try the mailto: link
Be sure your reply has a Subject: header at the top and a blank line before the message body.
This is a public inbox, see mirroring instructions
for how to clone and mirror all data and code used for this inbox;
as well as URLs for NNTP newsgroup(s).