#!/usr/bin/perl
##############################################################
# Description:  Handle the ECN feature - enable, disable
#               and show status
# Author:
#   Vlad Ivanov
#
# 1.02 - Two colons were removed before shared functions and variables.
# 1.01 - POD section was added; the usage is displayed by the
#        common function - print_pod_usage() now.
# 1.00 - Initial version
##############################################################

=pod

=head1 NAME

B<ecnconfig> - tool to configure the ECN feature.

=head1 SYNOPSIS

B<ecnconfig> [B<help>]|[B<on>]|[B<off>]|[B<show>]

=head1 OPTIONS

=over 4

=item B<help>

Print this help message and exit.

=item B<on>

Enable the ECN feature.

=item B<off>

Disable the ECN feature.

=item B<show>

Show the ECN feature status.

=item B<NOTE that the setting is not persistant and it will be reset to the original value upon a server restart.>

=back

=head1 DESCRIPTION

B<ecnconfig> tool is used to handle the ECN feature state. It can switch 
this feature on or off and show its status.

=head1 EXAMPLES

=over 4

=item B<$ ecnconfig> on

 You have to have the root privilege to execute this command.
 Password for root:
 ECN feature is set to 'on' state.

=item B<$ ecnconfig> show

 You have to have the root privilege to execute this command.
 Password for root:
 ECN feature is on.

=item B<$ ecnconfig> off

 You have to have the root privilege to execute this command.
 Password for root:
 ECN feature is set to 'off' state.

=back

=head1 CAVEATS

B<ecnconfig> can be run only under Nortel authorized accounts.

=cut

use lib "/opt/nortel/base/lib";

use base_sysctl;
use common_variables;
use common_functions;
use baselog;

use warnings;
use strict;

#ecn system parameter
my $ecn_param = "net.ipv4.tcp_ecn";

sub main();

exit(main());

###----------------------------------------------------------------------------
# Description:
#
#  INPUT:  'on' or 'off' to enable or disable ECN feature accordingly
#
#  OUTPUT: $OK    (0) - no errors
#          $ERROR (1) - work does not finished properly.
###----------------------------------------------------------------------------
sub main()
{
   if (warning_if_not_root_uid())
   {
      return $ERROR;
   }
   my $filename = substr $0, 1 + rindex($0, '/');
   my $retval = $OK;
   if (0 == $#ARGV)
   {
      if ($ARGV[0] =~ /^show$/)
      {
         unless (show_ecn())
         {
            print "Could not determine ECN feature status.\n";
            $retval = $ERROR;
         }
      } ## end if ($ARGV[0] =~ /^show$/...
      elsif ($ARGV[0] =~ /help$/)
      {
         print_pod_usage();
         $retval = $OK;
      }
      elsif ($ARGV[0] =~ /^(on|off)$/)
      {
         if (set_ecn($ARGV[0]))
         {
            print "ECN feature is set to '$ARGV[0]' state.\n";
         }
         else
         {
            print "ECN feature is not set properly.\n";
            $retval = $ERROR;
         }

      } ## end elsif ($ARGV[0] =~ /^(on|off)$/...
      else
      {
         print_pod_usage("Unsupported argument: '$ARGV[0]'");
         $retval = $ERROR;
      }
   } ## end if (0 == $#ARGV)
   else
   {
      print_pod_usage("Insufficient number of arguments");
      $retval = $ERROR;
   }
   return $retval;
}  ##end of main()

###------------------------------------------------------------------
# Description:
#  - Handle (on|off) the ECN feature.
#  INPUT:  "on" - to enable ECN feature; "off" - to disable
#
#  OUTPUT: $TRUE - ECN feature is changed successfully
#          $FALSE - an error occured
###------------------------------------------------------------------
sub set_ecn($)
{
   my $arg   = shift @_;
   my $value = undef;

   #Check the argument
   unless ($arg =~ /^(on|off)$/)
   {
      baselog($LOG_ERROR, "Unsupported argument '$arg'.");
      return $FALSE;
   }

   $value = ('on' eq $arg) ? "1" : "0";

   if (set_param($ecn_param, $value))
   {
      baselog($LOG_INFO, "ECN feature is set to '$arg'.");
      return $TRUE;
   }
   else
   {
      baselog($LOG_ERROR, "Could not set ECN feature.");
      return $FALSE;
   }
} ## end sub set_ecn($)

###------------------------------------------------------------------
# Description:
#  Show ECN feature status ('on' or 'off')
#  INPUT:  NONE
#
#  OUTPUT: $TRUE  - no errors
#          $FALSE - an error occured
###------------------------------------------------------------------
sub show_ecn()
{
   my $value = get_param($ecn_param);
   unless (defined($value))
   {
      baselog($LOG_ERROR, "Could not read $ecn_param parameter.");
      return $FALSE;
   }

   $value = ($value ne "0") ? "on" : "off";
   baselog($LOG_DEBUG, "ECN feature is '$value'.");
   print "ECN feature is $value.\n";
   return $TRUE;
} ## end sub show_ecn()
