datapath.c 46 KB

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