datapath.c 45 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495969798991001011021031041051061071081091101111121131141151161171181191201211221231241251261271281291301311321331341351361371381391401411421431441451461471481491501511521531541551561571581591601611621631641651661671681691701711721731741751761771781791801811821831841851861871881891901911921931941951961971981992002012022032042052062072082092102112122132142152162172182192202212222232242252262272282292302312322332342352362372382392402412422432442452462472482492502512522532542552562572582592602612622632642652662672682692702712722732742752762772782792802812822832842852862872882892902912922932942952962972982993003013023033043053063073083093103113123133143153163173183193203213223233243253263273283293303313323333343353363373383393403413423433443453463473483493503513523533543553563573583593603613623633643653663673683693703713723733743753763773783793803813823833843853863873883893903913923933943953963973983994004014024034044054064074084094104114124134144154164174184194204214224234244254264274284294304314324334344354364374384394404414424434444454464474484494504514524534544554564574584594604614624634644654664674684694704714724734744754764774784794804814824834844854864874884894904914924934944954964974984995005015025035045055065075085095105115125135145155165175185195205215225235245255265275285295305315325335345355365375385395405415425435445455465475485495505515525535545555565575585595605615625635645655665675685695705715725735745755765775785795805815825835845855865875885895905915925935945955965975985996006016026036046056066076086096106116126136146156166176186196206216226236246256266276286296306316326336346356366376386396406416426436446456466476486496506516526536546556566576586596606616626636646656666676686696706716726736746756766776786796806816826836846856866876886896906916926936946956966976986997007017027037047057067077087097107117127137147157167177187197207217227237247257267277287297307317327337347357367377387397407417427437447457467477487497507517527537547557567577587597607617627637647657667677687697707717727737747757767777787797807817827837847857867877887897907917927937947957967977987998008018028038048058068078088098108118128138148158168178188198208218228238248258268278288298308318328338348358368378388398408418428438448458468478488498508518528538548558568578588598608618628638648658668678688698708718728738748758768778788798808818828838848858868878888898908918928938948958968978988999009019029039049059069079089099109119129139149159169179189199209219229239249259269279289299309319329339349359369379389399409419429439449459469479489499509519529539549559569579589599609619629639649659669679689699709719729739749759769779789799809819829839849859869879889899909919929939949959969979989991000100110021003100410051006100710081009101010111012101310141015101610171018101910201021102210231024102510261027102810291030103110321033103410351036103710381039104010411042104310441045104610471048104910501051105210531054105510561057105810591060106110621063106410651066106710681069107010711072107310741075107610771078107910801081108210831084108510861087108810891090109110921093109410951096109710981099110011011102110311041105110611071108110911101111111211131114111511161117111811191120112111221123112411251126112711281129113011311132113311341135113611371138113911401141114211431144114511461147114811491150115111521153115411551156115711581159116011611162116311641165116611671168116911701171117211731174117511761177117811791180118111821183118411851186118711881189119011911192119311941195119611971198119912001201120212031204120512061207120812091210121112121213121412151216121712181219122012211222122312241225122612271228122912301231123212331234123512361237123812391240124112421243124412451246124712481249125012511252125312541255125612571258125912601261126212631264126512661267126812691270127112721273127412751276127712781279128012811282128312841285128612871288128912901291129212931294129512961297129812991300130113021303130413051306130713081309131013111312131313141315131613171318131913201321132213231324132513261327132813291330133113321333133413351336133713381339134013411342134313441345134613471348134913501351135213531354135513561357135813591360136113621363136413651366136713681369137013711372137313741375137613771378137913801381138213831384138513861387138813891390139113921393139413951396139713981399140014011402140314041405140614071408140914101411141214131414141514161417141814191420142114221423142414251426142714281429143014311432143314341435143614371438143914401441144214431444144514461447144814491450145114521453145414551456145714581459146014611462146314641465146614671468146914701471147214731474147514761477147814791480148114821483148414851486148714881489149014911492149314941495149614971498149915001501150215031504150515061507150815091510151115121513151415151516151715181519152015211522152315241525152615271528152915301531153215331534153515361537153815391540154115421543154415451546154715481549155015511552155315541555155615571558155915601561156215631564156515661567156815691570157115721573157415751576157715781579158015811582158315841585158615871588158915901591159215931594159515961597159815991600160116021603160416051606160716081609161016111612161316141615161616171618161916201621162216231624162516261627162816291630163116321633163416351636163716381639164016411642164316441645164616471648164916501651165216531654165516561657165816591660166116621663166416651666166716681669167016711672167316741675167616771678167916801681168216831684168516861687168816891690169116921693169416951696169716981699170017011702170317041705170617071708170917101711171217131714171517161717171817191720172117221723172417251726172717281729173017311732173317341735173617371738173917401741174217431744174517461747174817491750175117521753175417551756175717581759176017611762176317641765176617671768176917701771177217731774177517761777177817791780178117821783178417851786178717881789179017911792179317941795179617971798179918001801180218031804180518061807180818091810181118121813181418151816181718181819182018211822182318241825182618271828182918301831183218331834183518361837183818391840184118421843184418451846184718481849185018511852185318541855185618571858185918601861186218631864186518661867186818691870187118721873187418751876187718781879188018811882188318841885188618871888188918901891189218931894189518961897189818991900190119021903190419051906190719081909191019111912
  1. /*
  2. * Copyright (c) 2007-2011 Nicira Networks.
  3. *
  4. * This program is free software; you can redistribute it and/or
  5. * modify it under the terms of version 2 of the GNU General Public
  6. * License as published by the Free Software Foundation.
  7. *
  8. * This program is distributed in the hope that it will be useful, but
  9. * WITHOUT ANY WARRANTY; without even the implied warranty of
  10. * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
  11. * General Public License for more details.
  12. *
  13. * You should have received a copy of the GNU General Public License
  14. * along with this program; if not, write to the Free Software
  15. * Foundation, Inc., 51 Franklin Street, Fifth Floor, Boston, MA
  16. * 02110-1301, USA
  17. */
  18. #define pr_fmt(fmt) KBUILD_MODNAME ": " fmt
  19. #include <linux/init.h>
  20. #include <linux/module.h>
  21. #include <linux/if_arp.h>
  22. #include <linux/if_vlan.h>
  23. #include <linux/in.h>
  24. #include <linux/ip.h>
  25. #include <linux/jhash.h>
  26. #include <linux/delay.h>
  27. #include <linux/time.h>
  28. #include <linux/etherdevice.h>
  29. #include <linux/genetlink.h>
  30. #include <linux/kernel.h>
  31. #include <linux/kthread.h>
  32. #include <linux/mutex.h>
  33. #include <linux/percpu.h>
  34. #include <linux/rcupdate.h>
  35. #include <linux/tcp.h>
  36. #include <linux/udp.h>
  37. #include <linux/version.h>
  38. #include <linux/ethtool.h>
  39. #include <linux/wait.h>
  40. #include <asm/system.h>
  41. #include <asm/div64.h>
  42. #include <linux/highmem.h>
  43. #include <linux/netfilter_bridge.h>
  44. #include <linux/netfilter_ipv4.h>
  45. #include <linux/inetdevice.h>
  46. #include <linux/list.h>
  47. #include <linux/openvswitch.h>
  48. #include <linux/rculist.h>
  49. #include <linux/dmi.h>
  50. #include <linux/workqueue.h>
  51. #include <net/genetlink.h>
  52. #include "datapath.h"
  53. #include "flow.h"
  54. #include "vport-internal_dev.h"
  55. /**
  56. * DOC: Locking:
  57. *
  58. * Writes to device state (add/remove datapath, port, set operations on vports,
  59. * etc.) are protected by RTNL.
  60. *
  61. * Writes to other state (flow table modifications, set miscellaneous datapath
  62. * parameters, etc.) are protected by genl_mutex. The RTNL lock nests inside
  63. * genl_mutex.
  64. *
  65. * Reads are protected by RCU.
  66. *
  67. * There are a few special cases (mostly stats) that have their own
  68. * synchronization but they nest under all of above and don't interact with
  69. * each other.
  70. */
  71. /* Global list of datapaths to enable dumping them all out.
  72. * Protected by genl_mutex.
  73. */
  74. static LIST_HEAD(dps);
  75. #define REHASH_FLOW_INTERVAL (10 * 60 * HZ)
  76. static void rehash_flow_table(struct work_struct *work);
  77. static DECLARE_DELAYED_WORK(rehash_flow_wq, rehash_flow_table);
  78. static struct vport *new_vport(const struct vport_parms *);
  79. static int queue_gso_packets(int dp_ifindex, struct sk_buff *,
  80. const struct dp_upcall_info *);
  81. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *,
  82. const struct dp_upcall_info *);
  83. /* Must be called with rcu_read_lock, genl_mutex, or RTNL lock. */
  84. static struct datapath *get_dp(int dp_ifindex)
  85. {
  86. struct datapath *dp = NULL;
  87. struct net_device *dev;
  88. rcu_read_lock();
  89. dev = dev_get_by_index_rcu(&init_net, dp_ifindex);
  90. if (dev) {
  91. struct vport *vport = ovs_internal_dev_get_vport(dev);
  92. if (vport)
  93. dp = vport->dp;
  94. }
  95. rcu_read_unlock();
  96. return dp;
  97. }
  98. /* Must be called with rcu_read_lock or RTNL lock. */
  99. const char *ovs_dp_name(const struct datapath *dp)
  100. {
  101. struct vport *vport = rcu_dereference_rtnl(dp->ports[OVSP_LOCAL]);
  102. return vport->ops->get_name(vport);
  103. }
  104. static int get_dpifindex(struct datapath *dp)
  105. {
  106. struct vport *local;
  107. int ifindex;
  108. rcu_read_lock();
  109. local = rcu_dereference(dp->ports[OVSP_LOCAL]);
  110. if (local)
  111. ifindex = local->ops->get_ifindex(local);
  112. else
  113. ifindex = 0;
  114. rcu_read_unlock();
  115. return ifindex;
  116. }
  117. static void destroy_dp_rcu(struct rcu_head *rcu)
  118. {
  119. struct datapath *dp = container_of(rcu, struct datapath, rcu);
  120. ovs_flow_tbl_destroy((__force struct flow_table *)dp->table);
  121. free_percpu(dp->stats_percpu);
  122. kfree(dp);
  123. }
  124. /* Called with RTNL lock and genl_lock. */
  125. static struct vport *new_vport(const struct vport_parms *parms)
  126. {
  127. struct vport *vport;
  128. vport = ovs_vport_add(parms);
  129. if (!IS_ERR(vport)) {
  130. struct datapath *dp = parms->dp;
  131. rcu_assign_pointer(dp->ports[parms->port_no], vport);
  132. list_add(&vport->node, &dp->port_list);
  133. }
  134. return vport;
  135. }
  136. /* Called with RTNL lock. */
  137. void ovs_dp_detach_port(struct vport *p)
  138. {
  139. ASSERT_RTNL();
  140. /* First drop references to device. */
  141. list_del(&p->node);
  142. rcu_assign_pointer(p->dp->ports[p->port_no], NULL);
  143. /* Then destroy it. */
  144. ovs_vport_del(p);
  145. }
  146. /* Must be called with rcu_read_lock. */
  147. void ovs_dp_process_received_packet(struct vport *p, struct sk_buff *skb)
  148. {
  149. struct datapath *dp = p->dp;
  150. struct sw_flow *flow;
  151. struct dp_stats_percpu *stats;
  152. struct sw_flow_key key;
  153. u64 *stats_counter;
  154. int error;
  155. int key_len;
  156. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  157. /* Extract flow from 'skb' into 'key'. */
  158. error = ovs_flow_extract(skb, p->port_no, &key, &key_len);
  159. if (unlikely(error)) {
  160. kfree_skb(skb);
  161. return;
  162. }
  163. /* Look up flow. */
  164. flow = ovs_flow_tbl_lookup(rcu_dereference(dp->table), &key, key_len);
  165. if (unlikely(!flow)) {
  166. struct dp_upcall_info upcall;
  167. upcall.cmd = OVS_PACKET_CMD_MISS;
  168. upcall.key = &key;
  169. upcall.userdata = NULL;
  170. upcall.pid = p->upcall_pid;
  171. ovs_dp_upcall(dp, skb, &upcall);
  172. consume_skb(skb);
  173. stats_counter = &stats->n_missed;
  174. goto out;
  175. }
  176. OVS_CB(skb)->flow = flow;
  177. stats_counter = &stats->n_hit;
  178. ovs_flow_used(OVS_CB(skb)->flow, skb);
  179. ovs_execute_actions(dp, skb);
  180. out:
  181. /* Update datapath statistics. */
  182. u64_stats_update_begin(&stats->sync);
  183. (*stats_counter)++;
  184. u64_stats_update_end(&stats->sync);
  185. }
  186. static struct genl_family dp_packet_genl_family = {
  187. .id = GENL_ID_GENERATE,
  188. .hdrsize = sizeof(struct ovs_header),
  189. .name = OVS_PACKET_FAMILY,
  190. .version = OVS_PACKET_VERSION,
  191. .maxattr = OVS_PACKET_ATTR_MAX
  192. };
  193. int ovs_dp_upcall(struct datapath *dp, struct sk_buff *skb,
  194. const struct dp_upcall_info *upcall_info)
  195. {
  196. struct dp_stats_percpu *stats;
  197. int dp_ifindex;
  198. int err;
  199. if (upcall_info->pid == 0) {
  200. err = -ENOTCONN;
  201. goto err;
  202. }
  203. dp_ifindex = get_dpifindex(dp);
  204. if (!dp_ifindex) {
  205. err = -ENODEV;
  206. goto err;
  207. }
  208. if (!skb_is_gso(skb))
  209. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  210. else
  211. err = queue_gso_packets(dp_ifindex, skb, upcall_info);
  212. if (err)
  213. goto err;
  214. return 0;
  215. err:
  216. stats = per_cpu_ptr(dp->stats_percpu, smp_processor_id());
  217. u64_stats_update_begin(&stats->sync);
  218. stats->n_lost++;
  219. u64_stats_update_end(&stats->sync);
  220. return err;
  221. }
  222. static int queue_gso_packets(int dp_ifindex, struct sk_buff *skb,
  223. const struct dp_upcall_info *upcall_info)
  224. {
  225. struct dp_upcall_info later_info;
  226. struct sw_flow_key later_key;
  227. struct sk_buff *segs, *nskb;
  228. int err;
  229. segs = skb_gso_segment(skb, NETIF_F_SG | NETIF_F_HW_CSUM);
  230. if (IS_ERR(skb))
  231. return PTR_ERR(skb);
  232. /* Queue all of the segments. */
  233. skb = segs;
  234. do {
  235. err = queue_userspace_packet(dp_ifindex, skb, upcall_info);
  236. if (err)
  237. break;
  238. if (skb == segs && skb_shinfo(skb)->gso_type & SKB_GSO_UDP) {
  239. /* The initial flow key extracted by ovs_flow_extract()
  240. * in this case is for a first fragment, so we need to
  241. * properly mark later fragments.
  242. */
  243. later_key = *upcall_info->key;
  244. later_key.ip.frag = OVS_FRAG_TYPE_LATER;
  245. later_info = *upcall_info;
  246. later_info.key = &later_key;
  247. upcall_info = &later_info;
  248. }
  249. } while ((skb = skb->next));
  250. /* Free all of the segments. */
  251. skb = segs;
  252. do {
  253. nskb = skb->next;
  254. if (err)
  255. kfree_skb(skb);
  256. else
  257. consume_skb(skb);
  258. } while ((skb = nskb));
  259. return err;
  260. }
  261. static int queue_userspace_packet(int dp_ifindex, struct sk_buff *skb,
  262. const struct dp_upcall_info *upcall_info)
  263. {
  264. struct ovs_header *upcall;
  265. struct sk_buff *nskb = NULL;
  266. struct sk_buff *user_skb; /* to be queued to userspace */
  267. struct nlattr *nla;
  268. unsigned int len;
  269. int err;
  270. if (vlan_tx_tag_present(skb)) {
  271. nskb = skb_clone(skb, GFP_ATOMIC);
  272. if (!nskb)
  273. return -ENOMEM;
  274. nskb = __vlan_put_tag(nskb, vlan_tx_tag_get(nskb));
  275. if (!skb)
  276. return -ENOMEM;
  277. nskb->vlan_tci = 0;
  278. skb = nskb;
  279. }
  280. if (nla_attr_size(skb->len) > USHRT_MAX) {
  281. err = -EFBIG;
  282. goto out;
  283. }
  284. len = sizeof(struct ovs_header);
  285. len += nla_total_size(skb->len);
  286. len += nla_total_size(FLOW_BUFSIZE);
  287. if (upcall_info->cmd == OVS_PACKET_CMD_ACTION)
  288. len += nla_total_size(8);
  289. user_skb = genlmsg_new(len, GFP_ATOMIC);
  290. if (!user_skb) {
  291. err = -ENOMEM;
  292. goto out;
  293. }
  294. upcall = genlmsg_put(user_skb, 0, 0, &dp_packet_genl_family,
  295. 0, upcall_info->cmd);
  296. upcall->dp_ifindex = dp_ifindex;
  297. nla = nla_nest_start(user_skb, OVS_PACKET_ATTR_KEY);
  298. ovs_flow_to_nlattrs(upcall_info->key, user_skb);
  299. nla_nest_end(user_skb, nla);
  300. if (upcall_info->userdata)
  301. nla_put_u64(user_skb, OVS_PACKET_ATTR_USERDATA,
  302. nla_get_u64(upcall_info->userdata));
  303. nla = __nla_reserve(user_skb, OVS_PACKET_ATTR_PACKET, skb->len);
  304. skb_copy_and_csum_dev(skb, nla_data(nla));
  305. err = genlmsg_unicast(&init_net, user_skb, upcall_info->pid);
  306. out:
  307. kfree_skb(nskb);
  308. return err;
  309. }
  310. /* Called with genl_mutex. */
  311. static int flush_flows(int dp_ifindex)
  312. {
  313. struct flow_table *old_table;
  314. struct flow_table *new_table;
  315. struct datapath *dp;
  316. dp = get_dp(dp_ifindex);
  317. if (!dp)
  318. return -ENODEV;
  319. old_table = genl_dereference(dp->table);
  320. new_table = ovs_flow_tbl_alloc(TBL_MIN_BUCKETS);
  321. if (!new_table)
  322. return -ENOMEM;
  323. rcu_assign_pointer(dp->table, new_table);
  324. ovs_flow_tbl_deferred_destroy(old_table);
  325. return 0;
  326. }
  327. static int validate_actions(const struct nlattr *attr,
  328. const struct sw_flow_key *key, int depth);
  329. static int validate_sample(const struct nlattr *attr,
  330. const struct sw_flow_key *key, int depth)
  331. {
  332. const struct nlattr *attrs[OVS_SAMPLE_ATTR_MAX + 1];
  333. const struct nlattr *probability, *actions;
  334. const struct nlattr *a;
  335. int rem;
  336. memset(attrs, 0, sizeof(attrs));
  337. nla_for_each_nested(a, attr, rem) {
  338. int type = nla_type(a);
  339. if (!type || type > OVS_SAMPLE_ATTR_MAX || attrs[type])
  340. return -EINVAL;
  341. attrs[type] = a;
  342. }
  343. if (rem)
  344. return -EINVAL;
  345. probability = attrs[OVS_SAMPLE_ATTR_PROBABILITY];
  346. if (!probability || nla_len(probability) != sizeof(u32))
  347. return -EINVAL;
  348. actions = attrs[OVS_SAMPLE_ATTR_ACTIONS];
  349. if (!actions || (nla_len(actions) && nla_len(actions) < NLA_HDRLEN))
  350. return -EINVAL;
  351. return validate_actions(actions, key, depth + 1);
  352. }
  353. static int validate_set(const struct nlattr *a,
  354. const struct sw_flow_key *flow_key)
  355. {
  356. const struct nlattr *ovs_key = nla_data(a);
  357. int key_type = nla_type(ovs_key);
  358. /* There can be only one key in a action */
  359. if (nla_total_size(nla_len(ovs_key)) != nla_len(a))
  360. return -EINVAL;
  361. if (key_type > OVS_KEY_ATTR_MAX ||
  362. nla_len(ovs_key) != ovs_key_lens[key_type])
  363. return -EINVAL;
  364. switch (key_type) {
  365. const struct ovs_key_ipv4 *ipv4_key;
  366. case OVS_KEY_ATTR_PRIORITY:
  367. case OVS_KEY_ATTR_ETHERNET:
  368. break;
  369. case OVS_KEY_ATTR_IPV4:
  370. if (flow_key->eth.type != htons(ETH_P_IP))
  371. return -EINVAL;
  372. if (!flow_key->ipv4.addr.src || !flow_key->ipv4.addr.dst)
  373. return -EINVAL;
  374. ipv4_key = nla_data(ovs_key);
  375. if (ipv4_key->ipv4_proto != flow_key->ip.proto)
  376. return -EINVAL;
  377. if (ipv4_key->ipv4_frag != flow_key->ip.frag)
  378. return -EINVAL;
  379. break;
  380. case OVS_KEY_ATTR_TCP:
  381. if (flow_key->ip.proto != IPPROTO_TCP)
  382. return -EINVAL;
  383. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  384. return -EINVAL;
  385. break;
  386. case OVS_KEY_ATTR_UDP:
  387. if (flow_key->ip.proto != IPPROTO_UDP)
  388. return -EINVAL;
  389. if (!flow_key->ipv4.tp.src || !flow_key->ipv4.tp.dst)
  390. return -EINVAL;
  391. break;
  392. default:
  393. return -EINVAL;
  394. }
  395. return 0;
  396. }
  397. static int validate_userspace(const struct nlattr *attr)
  398. {
  399. static const struct nla_policy userspace_policy[OVS_USERSPACE_ATTR_MAX + 1] = {
  400. [OVS_USERSPACE_ATTR_PID] = {.type = NLA_U32 },
  401. [OVS_USERSPACE_ATTR_USERDATA] = {.type = NLA_U64 },
  402. };
  403. struct nlattr *a[OVS_USERSPACE_ATTR_MAX + 1];
  404. int error;
  405. error = nla_parse_nested(a, OVS_USERSPACE_ATTR_MAX,
  406. attr, userspace_policy);
  407. if (error)
  408. return error;
  409. if (!a[OVS_USERSPACE_ATTR_PID] ||
  410. !nla_get_u32(a[OVS_USERSPACE_ATTR_PID]))
  411. return -EINVAL;
  412. return 0;
  413. }
  414. static int validate_actions(const struct nlattr *attr,
  415. const struct sw_flow_key *key, int depth)
  416. {
  417. const struct nlattr *a;
  418. int rem, err;
  419. if (depth >= SAMPLE_ACTION_DEPTH)
  420. return -EOVERFLOW;
  421. nla_for_each_nested(a, attr, rem) {
  422. /* Expected argument lengths, (u32)-1 for variable length. */
  423. static const u32 action_lens[OVS_ACTION_ATTR_MAX + 1] = {
  424. [OVS_ACTION_ATTR_OUTPUT] = sizeof(u32),
  425. [OVS_ACTION_ATTR_USERSPACE] = (u32)-1,
  426. [OVS_ACTION_ATTR_PUSH_VLAN] = sizeof(struct ovs_action_push_vlan),
  427. [OVS_ACTION_ATTR_POP_VLAN] = 0,
  428. [OVS_ACTION_ATTR_SET] = (u32)-1,
  429. [OVS_ACTION_ATTR_SAMPLE] = (u32)-1
  430. };
  431. const struct ovs_action_push_vlan *vlan;
  432. int type = nla_type(a);
  433. if (type > OVS_ACTION_ATTR_MAX ||
  434. (action_lens[type] != nla_len(a) &&
  435. action_lens[type] != (u32)-1))
  436. return -EINVAL;
  437. switch (type) {
  438. case OVS_ACTION_ATTR_UNSPEC:
  439. return -EINVAL;
  440. case OVS_ACTION_ATTR_USERSPACE:
  441. err = validate_userspace(a);
  442. if (err)
  443. return err;
  444. break;
  445. case OVS_ACTION_ATTR_OUTPUT:
  446. if (nla_get_u32(a) >= DP_MAX_PORTS)
  447. return -EINVAL;
  448. break;
  449. case OVS_ACTION_ATTR_POP_VLAN:
  450. break;
  451. case OVS_ACTION_ATTR_PUSH_VLAN:
  452. vlan = nla_data(a);
  453. if (vlan->vlan_tpid != htons(ETH_P_8021Q))
  454. return -EINVAL;
  455. if (!(vlan->vlan_tci & htons(VLAN_TAG_PRESENT)))
  456. return -EINVAL;
  457. break;
  458. case OVS_ACTION_ATTR_SET:
  459. err = validate_set(a, key);
  460. if (err)
  461. return err;
  462. break;
  463. case OVS_ACTION_ATTR_SAMPLE:
  464. err = validate_sample(a, key, depth);
  465. if (err)
  466. return err;
  467. break;
  468. default:
  469. return -EINVAL;
  470. }
  471. }
  472. if (rem > 0)
  473. return -EINVAL;
  474. return 0;
  475. }
  476. static void clear_stats(struct sw_flow *flow)
  477. {
  478. flow->used = 0;
  479. flow->tcp_flags = 0;
  480. flow->packet_count = 0;
  481. flow->byte_count = 0;
  482. }
  483. static int ovs_packet_cmd_execute(struct sk_buff *skb, struct genl_info *info)
  484. {
  485. struct ovs_header *ovs_header = info->userhdr;
  486. struct nlattr **a = info->attrs;
  487. struct sw_flow_actions *acts;
  488. struct sk_buff *packet;
  489. struct sw_flow *flow;
  490. struct datapath *dp;
  491. struct ethhdr *eth;
  492. int len;
  493. int err;
  494. int key_len;
  495. err = -EINVAL;
  496. if (!a[OVS_PACKET_ATTR_PACKET] || !a[OVS_PACKET_ATTR_KEY] ||
  497. !a[OVS_PACKET_ATTR_ACTIONS] ||
  498. nla_len(a[OVS_PACKET_ATTR_PACKET]) < ETH_HLEN)
  499. goto err;
  500. len = nla_len(a[OVS_PACKET_ATTR_PACKET]);
  501. packet = __dev_alloc_skb(NET_IP_ALIGN + len, GFP_KERNEL);
  502. err = -ENOMEM;
  503. if (!packet)
  504. goto err;
  505. skb_reserve(packet, NET_IP_ALIGN);
  506. memcpy(__skb_put(packet, len), nla_data(a[OVS_PACKET_ATTR_PACKET]), len);
  507. skb_reset_mac_header(packet);
  508. eth = eth_hdr(packet);
  509. /* Normally, setting the skb 'protocol' field would be handled by a
  510. * call to eth_type_trans(), but it assumes there's a sending
  511. * device, which we may not have. */
  512. if (ntohs(eth->h_proto) >= 1536)
  513. packet->protocol = eth->h_proto;
  514. else
  515. packet->protocol = htons(ETH_P_802_2);
  516. /* Build an sw_flow for sending this packet. */
  517. flow = ovs_flow_alloc();
  518. err = PTR_ERR(flow);
  519. if (IS_ERR(flow))
  520. goto err_kfree_skb;
  521. err = ovs_flow_extract(packet, -1, &flow->key, &key_len);
  522. if (err)
  523. goto err_flow_free;
  524. err = ovs_flow_metadata_from_nlattrs(&flow->key.phy.priority,
  525. &flow->key.phy.in_port,
  526. a[OVS_PACKET_ATTR_KEY]);
  527. if (err)
  528. goto err_flow_free;
  529. err = validate_actions(a[OVS_PACKET_ATTR_ACTIONS], &flow->key, 0);
  530. if (err)
  531. goto err_flow_free;
  532. flow->hash = ovs_flow_hash(&flow->key, key_len);
  533. acts = ovs_flow_actions_alloc(a[OVS_PACKET_ATTR_ACTIONS]);
  534. err = PTR_ERR(acts);
  535. if (IS_ERR(acts))
  536. goto err_flow_free;
  537. rcu_assign_pointer(flow->sf_acts, acts);
  538. OVS_CB(packet)->flow = flow;
  539. packet->priority = flow->key.phy.priority;
  540. rcu_read_lock();
  541. dp = get_dp(ovs_header->dp_ifindex);
  542. err = -ENODEV;
  543. if (!dp)
  544. goto err_unlock;
  545. local_bh_disable();
  546. err = ovs_execute_actions(dp, packet);
  547. local_bh_enable();
  548. rcu_read_unlock();
  549. ovs_flow_free(flow);
  550. return err;
  551. err_unlock:
  552. rcu_read_unlock();
  553. err_flow_free:
  554. ovs_flow_free(flow);
  555. err_kfree_skb:
  556. kfree_skb(packet);
  557. err:
  558. return err;
  559. }
  560. static const struct nla_policy packet_policy[OVS_PACKET_ATTR_MAX + 1] = {
  561. [OVS_PACKET_ATTR_PACKET] = { .type = NLA_UNSPEC },
  562. [OVS_PACKET_ATTR_KEY] = { .type = NLA_NESTED },
  563. [OVS_PACKET_ATTR_ACTIONS] = { .type = NLA_NESTED },
  564. };
  565. static struct genl_ops dp_packet_genl_ops[] = {
  566. { .cmd = OVS_PACKET_CMD_EXECUTE,
  567. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  568. .policy = packet_policy,
  569. .doit = ovs_packet_cmd_execute
  570. }
  571. };
  572. static void get_dp_stats(struct datapath *dp, struct ovs_dp_stats *stats)
  573. {
  574. int i;
  575. struct flow_table *table = genl_dereference(dp->table);
  576. stats->n_flows = ovs_flow_tbl_count(table);
  577. stats->n_hit = stats->n_missed = stats->n_lost = 0;
  578. for_each_possible_cpu(i) {
  579. const struct dp_stats_percpu *percpu_stats;
  580. struct dp_stats_percpu local_stats;
  581. unsigned int start;
  582. percpu_stats = per_cpu_ptr(dp->stats_percpu, i);
  583. do {
  584. start = u64_stats_fetch_begin_bh(&percpu_stats->sync);
  585. local_stats = *percpu_stats;
  586. } while (u64_stats_fetch_retry_bh(&percpu_stats->sync, start));
  587. stats->n_hit += local_stats.n_hit;
  588. stats->n_missed += local_stats.n_missed;
  589. stats->n_lost += local_stats.n_lost;
  590. }
  591. }
  592. static const struct nla_policy flow_policy[OVS_FLOW_ATTR_MAX + 1] = {
  593. [OVS_FLOW_ATTR_KEY] = { .type = NLA_NESTED },
  594. [OVS_FLOW_ATTR_ACTIONS] = { .type = NLA_NESTED },
  595. [OVS_FLOW_ATTR_CLEAR] = { .type = NLA_FLAG },
  596. };
  597. static struct genl_family dp_flow_genl_family = {
  598. .id = GENL_ID_GENERATE,
  599. .hdrsize = sizeof(struct ovs_header),
  600. .name = OVS_FLOW_FAMILY,
  601. .version = OVS_FLOW_VERSION,
  602. .maxattr = OVS_FLOW_ATTR_MAX
  603. };
  604. static struct genl_multicast_group ovs_dp_flow_multicast_group = {
  605. .name = OVS_FLOW_MCGROUP
  606. };
  607. /* Called with genl_lock. */
  608. static int ovs_flow_cmd_fill_info(struct sw_flow *flow, struct datapath *dp,
  609. struct sk_buff *skb, u32 pid,
  610. u32 seq, u32 flags, u8 cmd)
  611. {
  612. const int skb_orig_len = skb->len;
  613. const struct sw_flow_actions *sf_acts;
  614. struct ovs_flow_stats stats;
  615. struct ovs_header *ovs_header;
  616. struct nlattr *nla;
  617. unsigned long used;
  618. u8 tcp_flags;
  619. int err;
  620. sf_acts = rcu_dereference_protected(flow->sf_acts,
  621. lockdep_genl_is_held());
  622. ovs_header = genlmsg_put(skb, pid, seq, &dp_flow_genl_family, flags, cmd);
  623. if (!ovs_header)
  624. return -EMSGSIZE;
  625. ovs_header->dp_ifindex = get_dpifindex(dp);
  626. nla = nla_nest_start(skb, OVS_FLOW_ATTR_KEY);
  627. if (!nla)
  628. goto nla_put_failure;
  629. err = ovs_flow_to_nlattrs(&flow->key, skb);
  630. if (err)
  631. goto error;
  632. nla_nest_end(skb, nla);
  633. spin_lock_bh(&flow->lock);
  634. used = flow->used;
  635. stats.n_packets = flow->packet_count;
  636. stats.n_bytes = flow->byte_count;
  637. tcp_flags = flow->tcp_flags;
  638. spin_unlock_bh(&flow->lock);
  639. if (used)
  640. NLA_PUT_U64(skb, OVS_FLOW_ATTR_USED, ovs_flow_used_time(used));
  641. if (stats.n_packets)
  642. NLA_PUT(skb, OVS_FLOW_ATTR_STATS,
  643. sizeof(struct ovs_flow_stats), &stats);
  644. if (tcp_flags)
  645. NLA_PUT_U8(skb, OVS_FLOW_ATTR_TCP_FLAGS, tcp_flags);
  646. /* If OVS_FLOW_ATTR_ACTIONS doesn't fit, skip dumping the actions if
  647. * this is the first flow to be dumped into 'skb'. This is unusual for
  648. * Netlink but individual action lists can be longer than
  649. * NLMSG_GOODSIZE and thus entirely undumpable if we didn't do this.
  650. * The userspace caller can always fetch the actions separately if it
  651. * really wants them. (Most userspace callers in fact don't care.)
  652. *
  653. * This can only fail for dump operations because the skb is always
  654. * properly sized for single flows.
  655. */
  656. err = nla_put(skb, OVS_FLOW_ATTR_ACTIONS, sf_acts->actions_len,
  657. sf_acts->actions);
  658. if (err < 0 && skb_orig_len)
  659. goto error;
  660. return genlmsg_end(skb, ovs_header);
  661. nla_put_failure:
  662. err = -EMSGSIZE;
  663. error:
  664. genlmsg_cancel(skb, ovs_header);
  665. return err;
  666. }
  667. static struct sk_buff *ovs_flow_cmd_alloc_info(struct sw_flow *flow)
  668. {
  669. const struct sw_flow_actions *sf_acts;
  670. int len;
  671. sf_acts = rcu_dereference_protected(flow->sf_acts,
  672. lockdep_genl_is_held());
  673. /* OVS_FLOW_ATTR_KEY */
  674. len = nla_total_size(FLOW_BUFSIZE);
  675. /* OVS_FLOW_ATTR_ACTIONS */
  676. len += nla_total_size(sf_acts->actions_len);
  677. /* OVS_FLOW_ATTR_STATS */
  678. len += nla_total_size(sizeof(struct ovs_flow_stats));
  679. /* OVS_FLOW_ATTR_TCP_FLAGS */
  680. len += nla_total_size(1);
  681. /* OVS_FLOW_ATTR_USED */
  682. len += nla_total_size(8);
  683. len += NLMSG_ALIGN(sizeof(struct ovs_header));
  684. return genlmsg_new(len, GFP_KERNEL);
  685. }
  686. static struct sk_buff *ovs_flow_cmd_build_info(struct sw_flow *flow,
  687. struct datapath *dp,
  688. u32 pid, u32 seq, u8 cmd)
  689. {
  690. struct sk_buff *skb;
  691. int retval;
  692. skb = ovs_flow_cmd_alloc_info(flow);
  693. if (!skb)
  694. return ERR_PTR(-ENOMEM);
  695. retval = ovs_flow_cmd_fill_info(flow, dp, skb, pid, seq, 0, cmd);
  696. BUG_ON(retval < 0);
  697. return skb;
  698. }
  699. static int ovs_flow_cmd_new_or_set(struct sk_buff *skb, struct genl_info *info)
  700. {
  701. struct nlattr **a = info->attrs;
  702. struct ovs_header *ovs_header = info->userhdr;
  703. struct sw_flow_key key;
  704. struct sw_flow *flow;
  705. struct sk_buff *reply;
  706. struct datapath *dp;
  707. struct flow_table *table;
  708. int error;
  709. int key_len;
  710. /* Extract key. */
  711. error = -EINVAL;
  712. if (!a[OVS_FLOW_ATTR_KEY])
  713. goto error;
  714. error = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  715. if (error)
  716. goto error;
  717. /* Validate actions. */
  718. if (a[OVS_FLOW_ATTR_ACTIONS]) {
  719. error = validate_actions(a[OVS_FLOW_ATTR_ACTIONS], &key, 0);
  720. if (error)
  721. goto error;
  722. } else if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW) {
  723. error = -EINVAL;
  724. goto error;
  725. }
  726. dp = get_dp(ovs_header->dp_ifindex);
  727. error = -ENODEV;
  728. if (!dp)
  729. goto error;
  730. table = genl_dereference(dp->table);
  731. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  732. if (!flow) {
  733. struct sw_flow_actions *acts;
  734. /* Bail out if we're not allowed to create a new flow. */
  735. error = -ENOENT;
  736. if (info->genlhdr->cmd == OVS_FLOW_CMD_SET)
  737. goto error;
  738. /* Expand table, if necessary, to make room. */
  739. if (ovs_flow_tbl_need_to_expand(table)) {
  740. struct flow_table *new_table;
  741. new_table = ovs_flow_tbl_expand(table);
  742. if (!IS_ERR(new_table)) {
  743. rcu_assign_pointer(dp->table, new_table);
  744. ovs_flow_tbl_deferred_destroy(table);
  745. table = genl_dereference(dp->table);
  746. }
  747. }
  748. /* Allocate flow. */
  749. flow = ovs_flow_alloc();
  750. if (IS_ERR(flow)) {
  751. error = PTR_ERR(flow);
  752. goto error;
  753. }
  754. flow->key = key;
  755. clear_stats(flow);
  756. /* Obtain actions. */
  757. acts = ovs_flow_actions_alloc(a[OVS_FLOW_ATTR_ACTIONS]);
  758. error = PTR_ERR(acts);
  759. if (IS_ERR(acts))
  760. goto error_free_flow;
  761. rcu_assign_pointer(flow->sf_acts, acts);
  762. /* Put flow in bucket. */
  763. flow->hash = ovs_flow_hash(&key, key_len);
  764. ovs_flow_tbl_insert(table, flow);
  765. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  766. info->snd_seq,
  767. OVS_FLOW_CMD_NEW);
  768. } else {
  769. /* We found a matching flow. */
  770. struct sw_flow_actions *old_acts;
  771. struct nlattr *acts_attrs;
  772. /* Bail out if we're not allowed to modify an existing flow.
  773. * We accept NLM_F_CREATE in place of the intended NLM_F_EXCL
  774. * because Generic Netlink treats the latter as a dump
  775. * request. We also accept NLM_F_EXCL in case that bug ever
  776. * gets fixed.
  777. */
  778. error = -EEXIST;
  779. if (info->genlhdr->cmd == OVS_FLOW_CMD_NEW &&
  780. info->nlhdr->nlmsg_flags & (NLM_F_CREATE | NLM_F_EXCL))
  781. goto error;
  782. /* Update actions. */
  783. old_acts = rcu_dereference_protected(flow->sf_acts,
  784. lockdep_genl_is_held());
  785. acts_attrs = a[OVS_FLOW_ATTR_ACTIONS];
  786. if (acts_attrs &&
  787. (old_acts->actions_len != nla_len(acts_attrs) ||
  788. memcmp(old_acts->actions, nla_data(acts_attrs),
  789. old_acts->actions_len))) {
  790. struct sw_flow_actions *new_acts;
  791. new_acts = ovs_flow_actions_alloc(acts_attrs);
  792. error = PTR_ERR(new_acts);
  793. if (IS_ERR(new_acts))
  794. goto error;
  795. rcu_assign_pointer(flow->sf_acts, new_acts);
  796. ovs_flow_deferred_free_acts(old_acts);
  797. }
  798. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  799. info->snd_seq, OVS_FLOW_CMD_NEW);
  800. /* Clear stats. */
  801. if (a[OVS_FLOW_ATTR_CLEAR]) {
  802. spin_lock_bh(&flow->lock);
  803. clear_stats(flow);
  804. spin_unlock_bh(&flow->lock);
  805. }
  806. }
  807. if (!IS_ERR(reply))
  808. genl_notify(reply, genl_info_net(info), info->snd_pid,
  809. ovs_dp_flow_multicast_group.id, info->nlhdr,
  810. GFP_KERNEL);
  811. else
  812. netlink_set_err(init_net.genl_sock, 0,
  813. ovs_dp_flow_multicast_group.id, PTR_ERR(reply));
  814. return 0;
  815. error_free_flow:
  816. ovs_flow_free(flow);
  817. error:
  818. return error;
  819. }
  820. static int ovs_flow_cmd_get(struct sk_buff *skb, struct genl_info *info)
  821. {
  822. struct nlattr **a = info->attrs;
  823. struct ovs_header *ovs_header = info->userhdr;
  824. struct sw_flow_key key;
  825. struct sk_buff *reply;
  826. struct sw_flow *flow;
  827. struct datapath *dp;
  828. struct flow_table *table;
  829. int err;
  830. int key_len;
  831. if (!a[OVS_FLOW_ATTR_KEY])
  832. return -EINVAL;
  833. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  834. if (err)
  835. return err;
  836. dp = get_dp(ovs_header->dp_ifindex);
  837. if (!dp)
  838. return -ENODEV;
  839. table = genl_dereference(dp->table);
  840. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  841. if (!flow)
  842. return -ENOENT;
  843. reply = ovs_flow_cmd_build_info(flow, dp, info->snd_pid,
  844. info->snd_seq, OVS_FLOW_CMD_NEW);
  845. if (IS_ERR(reply))
  846. return PTR_ERR(reply);
  847. return genlmsg_reply(reply, info);
  848. }
  849. static int ovs_flow_cmd_del(struct sk_buff *skb, struct genl_info *info)
  850. {
  851. struct nlattr **a = info->attrs;
  852. struct ovs_header *ovs_header = info->userhdr;
  853. struct sw_flow_key key;
  854. struct sk_buff *reply;
  855. struct sw_flow *flow;
  856. struct datapath *dp;
  857. struct flow_table *table;
  858. int err;
  859. int key_len;
  860. if (!a[OVS_FLOW_ATTR_KEY])
  861. return flush_flows(ovs_header->dp_ifindex);
  862. err = ovs_flow_from_nlattrs(&key, &key_len, a[OVS_FLOW_ATTR_KEY]);
  863. if (err)
  864. return err;
  865. dp = get_dp(ovs_header->dp_ifindex);
  866. if (!dp)
  867. return -ENODEV;
  868. table = genl_dereference(dp->table);
  869. flow = ovs_flow_tbl_lookup(table, &key, key_len);
  870. if (!flow)
  871. return -ENOENT;
  872. reply = ovs_flow_cmd_alloc_info(flow);
  873. if (!reply)
  874. return -ENOMEM;
  875. ovs_flow_tbl_remove(table, flow);
  876. err = ovs_flow_cmd_fill_info(flow, dp, reply, info->snd_pid,
  877. info->snd_seq, 0, OVS_FLOW_CMD_DEL);
  878. BUG_ON(err < 0);
  879. ovs_flow_deferred_free(flow);
  880. genl_notify(reply, genl_info_net(info), info->snd_pid,
  881. ovs_dp_flow_multicast_group.id, info->nlhdr, GFP_KERNEL);
  882. return 0;
  883. }
  884. static int ovs_flow_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  885. {
  886. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  887. struct datapath *dp;
  888. struct flow_table *table;
  889. dp = get_dp(ovs_header->dp_ifindex);
  890. if (!dp)
  891. return -ENODEV;
  892. table = genl_dereference(dp->table);
  893. for (;;) {
  894. struct sw_flow *flow;
  895. u32 bucket, obj;
  896. bucket = cb->args[0];
  897. obj = cb->args[1];
  898. flow = ovs_flow_tbl_next(table, &bucket, &obj);
  899. if (!flow)
  900. break;
  901. if (ovs_flow_cmd_fill_info(flow, dp, skb,
  902. NETLINK_CB(cb->skb).pid,
  903. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  904. OVS_FLOW_CMD_NEW) < 0)
  905. break;
  906. cb->args[0] = bucket;
  907. cb->args[1] = obj;
  908. }
  909. return skb->len;
  910. }
  911. static struct genl_ops dp_flow_genl_ops[] = {
  912. { .cmd = OVS_FLOW_CMD_NEW,
  913. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  914. .policy = flow_policy,
  915. .doit = ovs_flow_cmd_new_or_set
  916. },
  917. { .cmd = OVS_FLOW_CMD_DEL,
  918. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  919. .policy = flow_policy,
  920. .doit = ovs_flow_cmd_del
  921. },
  922. { .cmd = OVS_FLOW_CMD_GET,
  923. .flags = 0, /* OK for unprivileged users. */
  924. .policy = flow_policy,
  925. .doit = ovs_flow_cmd_get,
  926. .dumpit = ovs_flow_cmd_dump
  927. },
  928. { .cmd = OVS_FLOW_CMD_SET,
  929. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  930. .policy = flow_policy,
  931. .doit = ovs_flow_cmd_new_or_set,
  932. },
  933. };
  934. static const struct nla_policy datapath_policy[OVS_DP_ATTR_MAX + 1] = {
  935. [OVS_DP_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  936. [OVS_DP_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  937. };
  938. static struct genl_family dp_datapath_genl_family = {
  939. .id = GENL_ID_GENERATE,
  940. .hdrsize = sizeof(struct ovs_header),
  941. .name = OVS_DATAPATH_FAMILY,
  942. .version = OVS_DATAPATH_VERSION,
  943. .maxattr = OVS_DP_ATTR_MAX
  944. };
  945. static struct genl_multicast_group ovs_dp_datapath_multicast_group = {
  946. .name = OVS_DATAPATH_MCGROUP
  947. };
  948. static int ovs_dp_cmd_fill_info(struct datapath *dp, struct sk_buff *skb,
  949. u32 pid, u32 seq, u32 flags, u8 cmd)
  950. {
  951. struct ovs_header *ovs_header;
  952. struct ovs_dp_stats dp_stats;
  953. int err;
  954. ovs_header = genlmsg_put(skb, pid, seq, &dp_datapath_genl_family,
  955. flags, cmd);
  956. if (!ovs_header)
  957. goto error;
  958. ovs_header->dp_ifindex = get_dpifindex(dp);
  959. rcu_read_lock();
  960. err = nla_put_string(skb, OVS_DP_ATTR_NAME, ovs_dp_name(dp));
  961. rcu_read_unlock();
  962. if (err)
  963. goto nla_put_failure;
  964. get_dp_stats(dp, &dp_stats);
  965. NLA_PUT(skb, OVS_DP_ATTR_STATS, sizeof(struct ovs_dp_stats), &dp_stats);
  966. return genlmsg_end(skb, ovs_header);
  967. nla_put_failure:
  968. genlmsg_cancel(skb, ovs_header);
  969. error:
  970. return -EMSGSIZE;
  971. }
  972. static struct sk_buff *ovs_dp_cmd_build_info(struct datapath *dp, u32 pid,
  973. u32 seq, u8 cmd)
  974. {
  975. struct sk_buff *skb;
  976. int retval;
  977. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_KERNEL);
  978. if (!skb)
  979. return ERR_PTR(-ENOMEM);
  980. retval = ovs_dp_cmd_fill_info(dp, skb, pid, seq, 0, cmd);
  981. if (retval < 0) {
  982. kfree_skb(skb);
  983. return ERR_PTR(retval);
  984. }
  985. return skb;
  986. }
  987. /* Called with genl_mutex and optionally with RTNL lock also. */
  988. static struct datapath *lookup_datapath(struct ovs_header *ovs_header,
  989. struct nlattr *a[OVS_DP_ATTR_MAX + 1])
  990. {
  991. struct datapath *dp;
  992. if (!a[OVS_DP_ATTR_NAME])
  993. dp = get_dp(ovs_header->dp_ifindex);
  994. else {
  995. struct vport *vport;
  996. rcu_read_lock();
  997. vport = ovs_vport_locate(nla_data(a[OVS_DP_ATTR_NAME]));
  998. dp = vport && vport->port_no == OVSP_LOCAL ? vport->dp : NULL;
  999. rcu_read_unlock();
  1000. }
  1001. return dp ? dp : ERR_PTR(-ENODEV);
  1002. }
  1003. static int ovs_dp_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1004. {
  1005. struct nlattr **a = info->attrs;
  1006. struct vport_parms parms;
  1007. struct sk_buff *reply;
  1008. struct datapath *dp;
  1009. struct vport *vport;
  1010. int err;
  1011. err = -EINVAL;
  1012. if (!a[OVS_DP_ATTR_NAME] || !a[OVS_DP_ATTR_UPCALL_PID])
  1013. goto err;
  1014. rtnl_lock();
  1015. err = -ENODEV;
  1016. if (!try_module_get(THIS_MODULE))
  1017. goto err_unlock_rtnl;
  1018. err = -ENOMEM;
  1019. dp = kzalloc(sizeof(*dp), GFP_KERNEL);
  1020. if (dp == NULL)
  1021. goto err_put_module;
  1022. INIT_LIST_HEAD(&dp->port_list);
  1023. /* Allocate table. */
  1024. err = -ENOMEM;
  1025. rcu_assign_pointer(dp->table, ovs_flow_tbl_alloc(TBL_MIN_BUCKETS));
  1026. if (!dp->table)
  1027. goto err_free_dp;
  1028. dp->stats_percpu = alloc_percpu(struct dp_stats_percpu);
  1029. if (!dp->stats_percpu) {
  1030. err = -ENOMEM;
  1031. goto err_destroy_table;
  1032. }
  1033. /* Set up our datapath device. */
  1034. parms.name = nla_data(a[OVS_DP_ATTR_NAME]);
  1035. parms.type = OVS_VPORT_TYPE_INTERNAL;
  1036. parms.options = NULL;
  1037. parms.dp = dp;
  1038. parms.port_no = OVSP_LOCAL;
  1039. parms.upcall_pid = nla_get_u32(a[OVS_DP_ATTR_UPCALL_PID]);
  1040. vport = new_vport(&parms);
  1041. if (IS_ERR(vport)) {
  1042. err = PTR_ERR(vport);
  1043. if (err == -EBUSY)
  1044. err = -EEXIST;
  1045. goto err_destroy_percpu;
  1046. }
  1047. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1048. info->snd_seq, OVS_DP_CMD_NEW);
  1049. err = PTR_ERR(reply);
  1050. if (IS_ERR(reply))
  1051. goto err_destroy_local_port;
  1052. list_add_tail(&dp->list_node, &dps);
  1053. rtnl_unlock();
  1054. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1055. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1056. GFP_KERNEL);
  1057. return 0;
  1058. err_destroy_local_port:
  1059. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1060. err_destroy_percpu:
  1061. free_percpu(dp->stats_percpu);
  1062. err_destroy_table:
  1063. ovs_flow_tbl_destroy(genl_dereference(dp->table));
  1064. err_free_dp:
  1065. kfree(dp);
  1066. err_put_module:
  1067. module_put(THIS_MODULE);
  1068. err_unlock_rtnl:
  1069. rtnl_unlock();
  1070. err:
  1071. return err;
  1072. }
  1073. static int ovs_dp_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1074. {
  1075. struct vport *vport, *next_vport;
  1076. struct sk_buff *reply;
  1077. struct datapath *dp;
  1078. int err;
  1079. rtnl_lock();
  1080. dp = lookup_datapath(info->userhdr, info->attrs);
  1081. err = PTR_ERR(dp);
  1082. if (IS_ERR(dp))
  1083. goto exit_unlock;
  1084. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1085. info->snd_seq, OVS_DP_CMD_DEL);
  1086. err = PTR_ERR(reply);
  1087. if (IS_ERR(reply))
  1088. goto exit_unlock;
  1089. list_for_each_entry_safe(vport, next_vport, &dp->port_list, node)
  1090. if (vport->port_no != OVSP_LOCAL)
  1091. ovs_dp_detach_port(vport);
  1092. list_del(&dp->list_node);
  1093. ovs_dp_detach_port(rtnl_dereference(dp->ports[OVSP_LOCAL]));
  1094. /* rtnl_unlock() will wait until all the references to devices that
  1095. * are pending unregistration have been dropped. We do it here to
  1096. * ensure that any internal devices (which contain DP pointers) are
  1097. * fully destroyed before freeing the datapath.
  1098. */
  1099. rtnl_unlock();
  1100. call_rcu(&dp->rcu, destroy_dp_rcu);
  1101. module_put(THIS_MODULE);
  1102. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1103. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1104. GFP_KERNEL);
  1105. return 0;
  1106. exit_unlock:
  1107. rtnl_unlock();
  1108. return err;
  1109. }
  1110. static int ovs_dp_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1111. {
  1112. struct sk_buff *reply;
  1113. struct datapath *dp;
  1114. int err;
  1115. dp = lookup_datapath(info->userhdr, info->attrs);
  1116. if (IS_ERR(dp))
  1117. return PTR_ERR(dp);
  1118. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1119. info->snd_seq, OVS_DP_CMD_NEW);
  1120. if (IS_ERR(reply)) {
  1121. err = PTR_ERR(reply);
  1122. netlink_set_err(init_net.genl_sock, 0,
  1123. ovs_dp_datapath_multicast_group.id, err);
  1124. return 0;
  1125. }
  1126. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1127. ovs_dp_datapath_multicast_group.id, info->nlhdr,
  1128. GFP_KERNEL);
  1129. return 0;
  1130. }
  1131. static int ovs_dp_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1132. {
  1133. struct sk_buff *reply;
  1134. struct datapath *dp;
  1135. dp = lookup_datapath(info->userhdr, info->attrs);
  1136. if (IS_ERR(dp))
  1137. return PTR_ERR(dp);
  1138. reply = ovs_dp_cmd_build_info(dp, info->snd_pid,
  1139. info->snd_seq, OVS_DP_CMD_NEW);
  1140. if (IS_ERR(reply))
  1141. return PTR_ERR(reply);
  1142. return genlmsg_reply(reply, info);
  1143. }
  1144. static int ovs_dp_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1145. {
  1146. struct datapath *dp;
  1147. int skip = cb->args[0];
  1148. int i = 0;
  1149. list_for_each_entry(dp, &dps, list_node) {
  1150. if (i < skip)
  1151. continue;
  1152. if (ovs_dp_cmd_fill_info(dp, skb, NETLINK_CB(cb->skb).pid,
  1153. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1154. OVS_DP_CMD_NEW) < 0)
  1155. break;
  1156. i++;
  1157. }
  1158. cb->args[0] = i;
  1159. return skb->len;
  1160. }
  1161. static struct genl_ops dp_datapath_genl_ops[] = {
  1162. { .cmd = OVS_DP_CMD_NEW,
  1163. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1164. .policy = datapath_policy,
  1165. .doit = ovs_dp_cmd_new
  1166. },
  1167. { .cmd = OVS_DP_CMD_DEL,
  1168. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1169. .policy = datapath_policy,
  1170. .doit = ovs_dp_cmd_del
  1171. },
  1172. { .cmd = OVS_DP_CMD_GET,
  1173. .flags = 0, /* OK for unprivileged users. */
  1174. .policy = datapath_policy,
  1175. .doit = ovs_dp_cmd_get,
  1176. .dumpit = ovs_dp_cmd_dump
  1177. },
  1178. { .cmd = OVS_DP_CMD_SET,
  1179. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1180. .policy = datapath_policy,
  1181. .doit = ovs_dp_cmd_set,
  1182. },
  1183. };
  1184. static const struct nla_policy vport_policy[OVS_VPORT_ATTR_MAX + 1] = {
  1185. [OVS_VPORT_ATTR_NAME] = { .type = NLA_NUL_STRING, .len = IFNAMSIZ - 1 },
  1186. [OVS_VPORT_ATTR_STATS] = { .len = sizeof(struct ovs_vport_stats) },
  1187. [OVS_VPORT_ATTR_PORT_NO] = { .type = NLA_U32 },
  1188. [OVS_VPORT_ATTR_TYPE] = { .type = NLA_U32 },
  1189. [OVS_VPORT_ATTR_UPCALL_PID] = { .type = NLA_U32 },
  1190. [OVS_VPORT_ATTR_OPTIONS] = { .type = NLA_NESTED },
  1191. };
  1192. static struct genl_family dp_vport_genl_family = {
  1193. .id = GENL_ID_GENERATE,
  1194. .hdrsize = sizeof(struct ovs_header),
  1195. .name = OVS_VPORT_FAMILY,
  1196. .version = OVS_VPORT_VERSION,
  1197. .maxattr = OVS_VPORT_ATTR_MAX
  1198. };
  1199. struct genl_multicast_group ovs_dp_vport_multicast_group = {
  1200. .name = OVS_VPORT_MCGROUP
  1201. };
  1202. /* Called with RTNL lock or RCU read lock. */
  1203. static int ovs_vport_cmd_fill_info(struct vport *vport, struct sk_buff *skb,
  1204. u32 pid, u32 seq, u32 flags, u8 cmd)
  1205. {
  1206. struct ovs_header *ovs_header;
  1207. struct ovs_vport_stats vport_stats;
  1208. int err;
  1209. ovs_header = genlmsg_put(skb, pid, seq, &dp_vport_genl_family,
  1210. flags, cmd);
  1211. if (!ovs_header)
  1212. return -EMSGSIZE;
  1213. ovs_header->dp_ifindex = get_dpifindex(vport->dp);
  1214. NLA_PUT_U32(skb, OVS_VPORT_ATTR_PORT_NO, vport->port_no);
  1215. NLA_PUT_U32(skb, OVS_VPORT_ATTR_TYPE, vport->ops->type);
  1216. NLA_PUT_STRING(skb, OVS_VPORT_ATTR_NAME, vport->ops->get_name(vport));
  1217. NLA_PUT_U32(skb, OVS_VPORT_ATTR_UPCALL_PID, vport->upcall_pid);
  1218. ovs_vport_get_stats(vport, &vport_stats);
  1219. NLA_PUT(skb, OVS_VPORT_ATTR_STATS, sizeof(struct ovs_vport_stats),
  1220. &vport_stats);
  1221. err = ovs_vport_get_options(vport, skb);
  1222. if (err == -EMSGSIZE)
  1223. goto error;
  1224. return genlmsg_end(skb, ovs_header);
  1225. nla_put_failure:
  1226. err = -EMSGSIZE;
  1227. error:
  1228. genlmsg_cancel(skb, ovs_header);
  1229. return err;
  1230. }
  1231. /* Called with RTNL lock or RCU read lock. */
  1232. struct sk_buff *ovs_vport_cmd_build_info(struct vport *vport, u32 pid,
  1233. u32 seq, u8 cmd)
  1234. {
  1235. struct sk_buff *skb;
  1236. int retval;
  1237. skb = nlmsg_new(NLMSG_DEFAULT_SIZE, GFP_ATOMIC);
  1238. if (!skb)
  1239. return ERR_PTR(-ENOMEM);
  1240. retval = ovs_vport_cmd_fill_info(vport, skb, pid, seq, 0, cmd);
  1241. if (retval < 0) {
  1242. kfree_skb(skb);
  1243. return ERR_PTR(retval);
  1244. }
  1245. return skb;
  1246. }
  1247. /* Called with RTNL lock or RCU read lock. */
  1248. static struct vport *lookup_vport(struct ovs_header *ovs_header,
  1249. struct nlattr *a[OVS_VPORT_ATTR_MAX + 1])
  1250. {
  1251. struct datapath *dp;
  1252. struct vport *vport;
  1253. if (a[OVS_VPORT_ATTR_NAME]) {
  1254. vport = ovs_vport_locate(nla_data(a[OVS_VPORT_ATTR_NAME]));
  1255. if (!vport)
  1256. return ERR_PTR(-ENODEV);
  1257. return vport;
  1258. } else if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1259. u32 port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1260. if (port_no >= DP_MAX_PORTS)
  1261. return ERR_PTR(-EFBIG);
  1262. dp = get_dp(ovs_header->dp_ifindex);
  1263. if (!dp)
  1264. return ERR_PTR(-ENODEV);
  1265. vport = rcu_dereference_rtnl(dp->ports[port_no]);
  1266. if (!vport)
  1267. return ERR_PTR(-ENOENT);
  1268. return vport;
  1269. } else
  1270. return ERR_PTR(-EINVAL);
  1271. }
  1272. static int ovs_vport_cmd_new(struct sk_buff *skb, struct genl_info *info)
  1273. {
  1274. struct nlattr **a = info->attrs;
  1275. struct ovs_header *ovs_header = info->userhdr;
  1276. struct vport_parms parms;
  1277. struct sk_buff *reply;
  1278. struct vport *vport;
  1279. struct datapath *dp;
  1280. u32 port_no;
  1281. int err;
  1282. err = -EINVAL;
  1283. if (!a[OVS_VPORT_ATTR_NAME] || !a[OVS_VPORT_ATTR_TYPE] ||
  1284. !a[OVS_VPORT_ATTR_UPCALL_PID])
  1285. goto exit;
  1286. rtnl_lock();
  1287. dp = get_dp(ovs_header->dp_ifindex);
  1288. err = -ENODEV;
  1289. if (!dp)
  1290. goto exit_unlock;
  1291. if (a[OVS_VPORT_ATTR_PORT_NO]) {
  1292. port_no = nla_get_u32(a[OVS_VPORT_ATTR_PORT_NO]);
  1293. err = -EFBIG;
  1294. if (port_no >= DP_MAX_PORTS)
  1295. goto exit_unlock;
  1296. vport = rtnl_dereference(dp->ports[port_no]);
  1297. err = -EBUSY;
  1298. if (vport)
  1299. goto exit_unlock;
  1300. } else {
  1301. for (port_no = 1; ; port_no++) {
  1302. if (port_no >= DP_MAX_PORTS) {
  1303. err = -EFBIG;
  1304. goto exit_unlock;
  1305. }
  1306. vport = rtnl_dereference(dp->ports[port_no]);
  1307. if (!vport)
  1308. break;
  1309. }
  1310. }
  1311. parms.name = nla_data(a[OVS_VPORT_ATTR_NAME]);
  1312. parms.type = nla_get_u32(a[OVS_VPORT_ATTR_TYPE]);
  1313. parms.options = a[OVS_VPORT_ATTR_OPTIONS];
  1314. parms.dp = dp;
  1315. parms.port_no = port_no;
  1316. parms.upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1317. vport = new_vport(&parms);
  1318. err = PTR_ERR(vport);
  1319. if (IS_ERR(vport))
  1320. goto exit_unlock;
  1321. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1322. OVS_VPORT_CMD_NEW);
  1323. if (IS_ERR(reply)) {
  1324. err = PTR_ERR(reply);
  1325. ovs_dp_detach_port(vport);
  1326. goto exit_unlock;
  1327. }
  1328. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1329. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1330. exit_unlock:
  1331. rtnl_unlock();
  1332. exit:
  1333. return err;
  1334. }
  1335. static int ovs_vport_cmd_set(struct sk_buff *skb, struct genl_info *info)
  1336. {
  1337. struct nlattr **a = info->attrs;
  1338. struct sk_buff *reply;
  1339. struct vport *vport;
  1340. int err;
  1341. rtnl_lock();
  1342. vport = lookup_vport(info->userhdr, a);
  1343. err = PTR_ERR(vport);
  1344. if (IS_ERR(vport))
  1345. goto exit_unlock;
  1346. err = 0;
  1347. if (a[OVS_VPORT_ATTR_TYPE] &&
  1348. nla_get_u32(a[OVS_VPORT_ATTR_TYPE]) != vport->ops->type)
  1349. err = -EINVAL;
  1350. if (!err && a[OVS_VPORT_ATTR_OPTIONS])
  1351. err = ovs_vport_set_options(vport, a[OVS_VPORT_ATTR_OPTIONS]);
  1352. if (!err && a[OVS_VPORT_ATTR_UPCALL_PID])
  1353. vport->upcall_pid = nla_get_u32(a[OVS_VPORT_ATTR_UPCALL_PID]);
  1354. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1355. OVS_VPORT_CMD_NEW);
  1356. if (IS_ERR(reply)) {
  1357. err = PTR_ERR(reply);
  1358. netlink_set_err(init_net.genl_sock, 0,
  1359. ovs_dp_vport_multicast_group.id, err);
  1360. return 0;
  1361. }
  1362. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1363. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1364. exit_unlock:
  1365. rtnl_unlock();
  1366. return err;
  1367. }
  1368. static int ovs_vport_cmd_del(struct sk_buff *skb, struct genl_info *info)
  1369. {
  1370. struct nlattr **a = info->attrs;
  1371. struct sk_buff *reply;
  1372. struct vport *vport;
  1373. int err;
  1374. rtnl_lock();
  1375. vport = lookup_vport(info->userhdr, a);
  1376. err = PTR_ERR(vport);
  1377. if (IS_ERR(vport))
  1378. goto exit_unlock;
  1379. if (vport->port_no == OVSP_LOCAL) {
  1380. err = -EINVAL;
  1381. goto exit_unlock;
  1382. }
  1383. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1384. OVS_VPORT_CMD_DEL);
  1385. err = PTR_ERR(reply);
  1386. if (IS_ERR(reply))
  1387. goto exit_unlock;
  1388. ovs_dp_detach_port(vport);
  1389. genl_notify(reply, genl_info_net(info), info->snd_pid,
  1390. ovs_dp_vport_multicast_group.id, info->nlhdr, GFP_KERNEL);
  1391. exit_unlock:
  1392. rtnl_unlock();
  1393. return err;
  1394. }
  1395. static int ovs_vport_cmd_get(struct sk_buff *skb, struct genl_info *info)
  1396. {
  1397. struct nlattr **a = info->attrs;
  1398. struct ovs_header *ovs_header = info->userhdr;
  1399. struct sk_buff *reply;
  1400. struct vport *vport;
  1401. int err;
  1402. rcu_read_lock();
  1403. vport = lookup_vport(ovs_header, a);
  1404. err = PTR_ERR(vport);
  1405. if (IS_ERR(vport))
  1406. goto exit_unlock;
  1407. reply = ovs_vport_cmd_build_info(vport, info->snd_pid, info->snd_seq,
  1408. OVS_VPORT_CMD_NEW);
  1409. err = PTR_ERR(reply);
  1410. if (IS_ERR(reply))
  1411. goto exit_unlock;
  1412. rcu_read_unlock();
  1413. return genlmsg_reply(reply, info);
  1414. exit_unlock:
  1415. rcu_read_unlock();
  1416. return err;
  1417. }
  1418. static int ovs_vport_cmd_dump(struct sk_buff *skb, struct netlink_callback *cb)
  1419. {
  1420. struct ovs_header *ovs_header = genlmsg_data(nlmsg_data(cb->nlh));
  1421. struct datapath *dp;
  1422. u32 port_no;
  1423. int retval;
  1424. dp = get_dp(ovs_header->dp_ifindex);
  1425. if (!dp)
  1426. return -ENODEV;
  1427. rcu_read_lock();
  1428. for (port_no = cb->args[0]; port_no < DP_MAX_PORTS; port_no++) {
  1429. struct vport *vport;
  1430. vport = rcu_dereference(dp->ports[port_no]);
  1431. if (!vport)
  1432. continue;
  1433. if (ovs_vport_cmd_fill_info(vport, skb, NETLINK_CB(cb->skb).pid,
  1434. cb->nlh->nlmsg_seq, NLM_F_MULTI,
  1435. OVS_VPORT_CMD_NEW) < 0)
  1436. break;
  1437. }
  1438. rcu_read_unlock();
  1439. cb->args[0] = port_no;
  1440. retval = skb->len;
  1441. return retval;
  1442. }
  1443. static void rehash_flow_table(struct work_struct *work)
  1444. {
  1445. struct datapath *dp;
  1446. genl_lock();
  1447. list_for_each_entry(dp, &dps, list_node) {
  1448. struct flow_table *old_table = genl_dereference(dp->table);
  1449. struct flow_table *new_table;
  1450. new_table = ovs_flow_tbl_rehash(old_table);
  1451. if (!IS_ERR(new_table)) {
  1452. rcu_assign_pointer(dp->table, new_table);
  1453. ovs_flow_tbl_deferred_destroy(old_table);
  1454. }
  1455. }
  1456. genl_unlock();
  1457. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1458. }
  1459. static struct genl_ops dp_vport_genl_ops[] = {
  1460. { .cmd = OVS_VPORT_CMD_NEW,
  1461. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1462. .policy = vport_policy,
  1463. .doit = ovs_vport_cmd_new
  1464. },
  1465. { .cmd = OVS_VPORT_CMD_DEL,
  1466. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1467. .policy = vport_policy,
  1468. .doit = ovs_vport_cmd_del
  1469. },
  1470. { .cmd = OVS_VPORT_CMD_GET,
  1471. .flags = 0, /* OK for unprivileged users. */
  1472. .policy = vport_policy,
  1473. .doit = ovs_vport_cmd_get,
  1474. .dumpit = ovs_vport_cmd_dump
  1475. },
  1476. { .cmd = OVS_VPORT_CMD_SET,
  1477. .flags = GENL_ADMIN_PERM, /* Requires CAP_NET_ADMIN privilege. */
  1478. .policy = vport_policy,
  1479. .doit = ovs_vport_cmd_set,
  1480. },
  1481. };
  1482. struct genl_family_and_ops {
  1483. struct genl_family *family;
  1484. struct genl_ops *ops;
  1485. int n_ops;
  1486. struct genl_multicast_group *group;
  1487. };
  1488. static const struct genl_family_and_ops dp_genl_families[] = {
  1489. { &dp_datapath_genl_family,
  1490. dp_datapath_genl_ops, ARRAY_SIZE(dp_datapath_genl_ops),
  1491. &ovs_dp_datapath_multicast_group },
  1492. { &dp_vport_genl_family,
  1493. dp_vport_genl_ops, ARRAY_SIZE(dp_vport_genl_ops),
  1494. &ovs_dp_vport_multicast_group },
  1495. { &dp_flow_genl_family,
  1496. dp_flow_genl_ops, ARRAY_SIZE(dp_flow_genl_ops),
  1497. &ovs_dp_flow_multicast_group },
  1498. { &dp_packet_genl_family,
  1499. dp_packet_genl_ops, ARRAY_SIZE(dp_packet_genl_ops),
  1500. NULL },
  1501. };
  1502. static void dp_unregister_genl(int n_families)
  1503. {
  1504. int i;
  1505. for (i = 0; i < n_families; i++)
  1506. genl_unregister_family(dp_genl_families[i].family);
  1507. }
  1508. static int dp_register_genl(void)
  1509. {
  1510. int n_registered;
  1511. int err;
  1512. int i;
  1513. n_registered = 0;
  1514. for (i = 0; i < ARRAY_SIZE(dp_genl_families); i++) {
  1515. const struct genl_family_and_ops *f = &dp_genl_families[i];
  1516. err = genl_register_family_with_ops(f->family, f->ops,
  1517. f->n_ops);
  1518. if (err)
  1519. goto error;
  1520. n_registered++;
  1521. if (f->group) {
  1522. err = genl_register_mc_group(f->family, f->group);
  1523. if (err)
  1524. goto error;
  1525. }
  1526. }
  1527. return 0;
  1528. error:
  1529. dp_unregister_genl(n_registered);
  1530. return err;
  1531. }
  1532. static int __init dp_init(void)
  1533. {
  1534. struct sk_buff *dummy_skb;
  1535. int err;
  1536. BUILD_BUG_ON(sizeof(struct ovs_skb_cb) > sizeof(dummy_skb->cb));
  1537. pr_info("Open vSwitch switching datapath\n");
  1538. err = ovs_flow_init();
  1539. if (err)
  1540. goto error;
  1541. err = ovs_vport_init();
  1542. if (err)
  1543. goto error_flow_exit;
  1544. err = register_netdevice_notifier(&ovs_dp_device_notifier);
  1545. if (err)
  1546. goto error_vport_exit;
  1547. err = dp_register_genl();
  1548. if (err < 0)
  1549. goto error_unreg_notifier;
  1550. schedule_delayed_work(&rehash_flow_wq, REHASH_FLOW_INTERVAL);
  1551. return 0;
  1552. error_unreg_notifier:
  1553. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1554. error_vport_exit:
  1555. ovs_vport_exit();
  1556. error_flow_exit:
  1557. ovs_flow_exit();
  1558. error:
  1559. return err;
  1560. }
  1561. static void dp_cleanup(void)
  1562. {
  1563. cancel_delayed_work_sync(&rehash_flow_wq);
  1564. rcu_barrier();
  1565. dp_unregister_genl(ARRAY_SIZE(dp_genl_families));
  1566. unregister_netdevice_notifier(&ovs_dp_device_notifier);
  1567. ovs_vport_exit();
  1568. ovs_flow_exit();
  1569. }
  1570. module_init(dp_init);
  1571. module_exit(dp_cleanup);
  1572. MODULE_DESCRIPTION("Open vSwitch switching datapath");
  1573. MODULE_LICENSE("GPL");