[Linux-ha-dev] Patch to OCF Route

Poul Petersen petersen at strands.com
Thu Jul 10 11:47:34 MDT 2008


	This is my first adventure into OCF scripts, so I'm quite certain I have no
idea what I'm doing. However, the Route script recently posted was something that I've
been looking for recently, and needed a few tweaks to meet my requirements. In the spirit
of giving back, here are the changes I made - maybe they will be useful to someone.

	In summary, I wanted to be able to manipulate both traditional "routes" as
well as "policy routes". Now that I made these changes, I'm thinking it might have been
more appropriate to just create a new PolicyRoute script to make the distinction more
clear.

	In any event, I also wrote a wrapper script for heartbeat so I can now bring up
multiple routes and define a default gateway for each one, all from heartbeat. Which is
really neat.

Thanks!

-poul

--- Route.orig	2008-07-10 17:32:09.000000000 +0000
+++ Route	2008-07-10 17:34:59.000000000 +0000
@@ -63,7 +63,7 @@

  <parameters>

-<parameter name="destination" unique="1" required="1">
+<parameter name="destination" unique="1" >
  <longdesc lang="en">
  The destination network (or host) to be configured for the route.
  Specify the netmask suffix in CIDR notation (e.g. "/24").
@@ -93,12 +93,37 @@

  <parameter name="source" unique="1">
  <longdesc lang="en">
-The source IP address to be configured for the route.
+The source is the IP to prefer for routing outbound packets.
  </longdesc>
  <shortdesc lang="en">Source IP address</shortdesc>
  <content type="string" default="" />
  </parameter>

+<parameter name="from" unique="1">
+<longdesc lang="en">
+The "from" parameter is used by "ip rule" to define how to match
+packets based on their origin. This field should be in CIDR format: IP/NETMASK
+</longdesc>
+<shortdesc lang="en">From IP/MASK</shortdesc>
+<content type="string" default="" />
+</parameter>
+
+<parameter name="object" unique="1">
+<longdesc lang="en">
+The object type that is being manipulated: currently route or rule
+</longdesc>
+<shortdesc lang="en">Object Type</shortdesc>
+<content type="string" default="" />
+</parameter>
+
+<parameter name="table" unique="1">
+<longdesc lang="en">
+The route table in which to perform the action.
+</longdesc>
+<shortdesc lang="en">Table Integer</shortdesc>
+<content type="integer" default="" />
+</parameter>
+
  </parameters>

  <actions>
@@ -117,17 +142,40 @@
  #######################################################################

  create_route_spec() {
-    # Creates a route specification for use by "ip route (add|del|show)"
-    route_spec="to ${OCF_RESKEY_destination}"
+    # Creates a route specification for use by "ip ${OCF_RESKEY_object} (add|del|show)"
+
+    if [ -n "${OCF_RESKEY_destination}" ]; then
+    	route_spec="to ${OCF_RESKEY_destination}"
+    fi
+
+    case ${OCF_RESKEY_object} in
+	rule)
+
+    		if [ -n "${OCF_RESKEY_from}" ]; then
+			route_spec="${route_spec} from ${OCF_RESKEY_from}"
+    		fi
+        ;;
+
+  	route)
+
+    		if [ -n "${OCF_RESKEY_gateway}" ]; then
+			route_spec="${route_spec} via ${OCF_RESKEY_gateway}"
+    		fi
+
+		if [ -n "${OCF_RESKEY_source}" ]; then
+			route_spec="${route_spec} src ${OCF_RESKEY_source}"
+    		fi
+   	;;
+    esac
+
      if [ -n "${OCF_RESKEY_device}" ]; then
  	route_spec="${route_spec} dev ${OCF_RESKEY_device}"
      fi
-    if [ -n "${OCF_RESKEY_gateway}" ]; then
-	route_spec="${route_spec} via ${OCF_RESKEY_gateway}"
-    fi
-    if [ -n "${OCF_RESKEY_source}" ]; then
-	route_spec="${route_spec} src ${OCF_RESKEY_source}"
+
+    if [ -n "${OCF_RESKEY_table}" ]; then
+	route_spec="${route_spec} table ${OCF_RESKEY_table}"
      fi
+
      echo "$route_spec"
  }

@@ -147,7 +195,7 @@
  	return $OCF_SUCCESS
      fi
      route_spec="$(create_route_spec)"
-    if ip route add $route_spec; then
+    if ip ${OCF_RESKEY_object} add $route_spec; then
  	ocf_log info "${OCF_RESOURCE_INSTANCE} Added network route: $route_spec"
  	return $OCF_SUCCESS
      else
@@ -162,7 +210,8 @@
      case $status in
  	$OCF_SUCCESS)
  	    route_spec="$(create_route_spec)"
-	    if ip route del $route_spec; then
+ocf_log "ip ${OCF_RESKEY_object} del $route_spec"
+	    if ip ${OCF_RESKEY_object} del $route_spec; then
  		ocf_log info "${OCF_RESOURCE_INSTANCE} Removed network route: $route_spec"
  		return $OCF_SUCCESS
  	    else
@@ -178,20 +227,34 @@
  }

  route_status() {
-    show_output="$(ip route show $(create_route_spec) 2>/dev/null)"
-    if [ $? -eq 0 ]; then
+
+    route_spec=$(create_route_spec)
+
+    case ${OCF_RESKEY_object} in
+	rule)
+		route_spec=`echo "$route_spec" | sed -e 's/table/lookup/g' -e 's/\/32//g' -e 's/^\ *//'`
+    		show_output="$(ip ${OCF_RESKEY_object} show | grep "$route_spec" 2>/dev/null)"
+	;;
+
+	route)
+	
+    		show_output="$(ip ${OCF_RESKEY_object} show $route_spec 2>/dev/null)"
+	;;
+    esac
+
+    if [ $? -eq 0 ] || [ "${OCF_RESKEY_object}" = "rule" ]; then
  	if [ -n "$show_output" ]; then
-	    # "ip route show" returned zero, and produced output on
+	    # "ip ${OCF_RESKEY_object} show" returned zero, and produced output on
  	    # stdout. That is what we expect.
  	    return $OCF_SUCCESS
  	else
-	    # "ip route show" returned zero, but produced no
+	    # "ip ${OCF_RESKEY_object} show" returned zero, but produced no
  	    # output on stdout. Assume the route was cleanly
  	    # unconfigured.
  	    return $OCF_NOT_RUNNING
  	fi
      else
-	# "ip route show" returned an error code. Assume something
+	# "ip ${OCF_RESKEY_object} show" returned an error code. Assume something
  	# went wrong.
  	return $OCF_ERR_GENERIC
      fi
@@ -199,6 +262,11 @@

  route_validate() {
      # If we're running as a clone, are the clone meta attrs OK?
+
+    if [ "${OCF_RESKEY_object}" = "rule" ]; then
+	return $OCF_SUCCESS
+    fi
+
      if [ "${OCF_RESKEY_CRM_meta_clone}" ]; then
  	if [ "${OCF_RESKEY_CRM_meta_clone_node_max}" -ne 1 ]; then
  	    ocf_log error "Misconfigured clone parameters. Must set meta attribute \"clone_node_max\" to 1, got ${OCF_RESKEY_CRM_meta_clone_node_max}."
@@ -235,7 +303,7 @@
      fi
      # If a gateway address has been configured, is it reachable?
      if [ -n "${OCF_RESKEY_gateway}" ]; then
-	if ! ip route get ${OCF_RESKEY_gateway} >/dev/null 2>&1; then
+	if ! ip ${OCF_RESKEY_object} get ${OCF_RESKEY_gateway} >/dev/null 2>&1; then
  	    ocf_log error "Gateway address ${OCF_RESKEY_gateway} is unreachable."
  	    # same reason as with _device:
  	    return $OCF_ERR_INSTALLED



More information about the Linux-HA-Dev mailing list